Skip to content

perf: Avoid name lookups and hashing for record fields in schema order - #753

Open
janwaleed09 wants to merge 3 commits into
apache:mainfrom
janwaleed09:record_field_lookup
Open

janwaleed09 wants to merge 3 commits into
apache:mainfrom
janwaleed09:record_field_lookup

Conversation

@janwaleed09

Copy link
Copy Markdown
Contributor

This builds on #752: its commit comes first here, so only the last two commits are new. I'll rebase once #752 is merged.

RecordSerializer looks up every field name in RecordSchema::lookup, a BTreeMap<String, usize>, and afterwards calls HashMap::remove on the buffer for out-of-order fields. remove hashes 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 calls remove when 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_key and serialize_entry). They pass on main too.

cargo bench --bench serde -- "serde way" on my machine (macOS, aarch64), compared with main:

  • big schema: −44 % for 10k records, −41 % to −46 % for 100 records, −8 % for one record
  • small schema: −35 % for 10k records, −28 % for 100 records, no change for one record

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 BTreeMap and a Vec indexed by position for the buffer. Both help when a few fields are out of order, but both are slower than main in other cases (the BTreeMap with thousands of shuffled fields, the Vec when 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 a RecordSchema built by hand whose lookup doesn't match its fields: fields that arrive in schema order are now found even if lookup misses them.

@janwaleed09

janwaleed09 commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor Author

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

@Kriskras99

Copy link
Copy Markdown
Contributor

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.
You have my approval, but I'll wait with marking it that way until #752 is merged to prevent accidentally merging this.

@janwaleed09

janwaleed09 commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor Author

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. You have my approval, but I'll wait with marking it that way until #752 is merged to prevent accidentally merging this.

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 HashMap::remove, even though the cache was empty

Good idea about the hasher. I tried it quickly: with a faster hasher (a small multiply-based one for the usize keys, and foldhash), records with fields out of order get about 20–35 % faster. Fields in schema order don't change since the cache isn't used then. The keys are just field positions, so HashDoS shouldn't matter here

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 foldhash or have a few lines without one?

@Kriskras99

Copy link
Copy Markdown
Contributor

I'd prefer rustc-hash, I've run the foldhash benchmark suite with it, and it comes out on top. It is also maintained by the Rust team, which is a nice bonus.

You can do it in this PR or another one, whatever you prefer.

@janwaleed09

janwaleed09 commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks rustc-hash it is. Since it doesn't depend on this PR or #752, I opened it separately as #754 so it doesnt have to wait for them

@martin-g martin-g added this to the 0.24.0 milestone Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants