Repository navigation
perf: Avoid name lookups and hashing for record fields in schema order - #753
janwaleed09 wants to merge 3 commits into
Conversation
|
Hi since this builds on #752, it's probably easiest to look at that one first, the change there is only a few lines. I know that's quite a few PRs from me in one day, so thanks a lot for taking the time |
|
That's a good find, how did you find it? One further improvement would be to replace the Hasher for the hashmap with something that is faster than siphash. |
Thanks I found it while working on #750. That PR touches the binary serializers, so I benchmarked and profiled the binary path to make sure it doesn't get slower, and most of the time per field went to the name lookup and to hashing the key in Good idea about the hasher. I tried it quickly: with a faster hasher (a small multiply-based one for the I'd keep this PR as it is and open aseparate one for the hasher once this is merged. Would you rather add a dependency like |
|
I'd prefer You can do it in this PR or another one, whatever you prefer. |
This builds on #752: its commit comes first here, so only the last two commits are new. I'll rebase once #752 is merged.
RecordSerializerlooks up every field name inRecordSchema::lookup, aBTreeMap<String, usize>, and afterwards callsHashMap::removeon the buffer for out-of-order fields.removehashes the key even when the map is empty, which it nearly always is. Fields almost always arrive in schema order, so both are usually wasted work.This checks the field that should be written next before falling back to
lookup, and only callsremovewhen the buffer isn't empty. Out-of-order fields take the same path as before.The test commit only adds tests for the paths this touches that weren't covered yet (aliases, duplicate fields, unknown keys,
serialize_keyandserialize_entry). They pass on main too.cargo bench --bench serde -- "serde way"on my machine (macOS, aarch64), compared with main:Records with 64 to 2048 fields in schema order get 5 to 9 times faster. When the fields arrive in a different order, it's between 17 % faster and unchanged.
I also tried a
BTreeMapand aVecindexed by position for the buffer. Both help when a few fields are out of order, but both are slower than main in other cases (theBTreeMapwith thousands of shuffled fields, theVecwhen a field near the end of a wide record is out of order), so I left the buffer alone.To check that nothing else changes, I compared the output with and without the perf commit for 1.2 million random flat records and about 180,000 random nested schemas (unions, enums, maps, arrays), plus derived types with
flatten,skip, tagged enums and all writer settings: the bytes, the returned byte count and the errors are identical. The only difference is for aRecordSchemabuilt by hand whoselookupdoesn't match itsfields: fields that arrive in schema order are now found even iflookupmisses them.