Skip to content

Frequent items: start the map iterator at slot 0, as Java, Go and Rust do - #607

Draft
stojkomilos wants to merge 2 commits into
apache:masterfrom
stojkomilos:milosh-stojko/fi-iterator-start-slot-0
Draft

stojkomilos wants to merge 2 commits into
apache:masterfrom
stojkomilos:milosh-stojko/fi-iterator-start-slot-0

Conversation

@stojkomilos

@stojkomilos stojkomilos commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

NOT READY FOR REVIEW YET

frequent_items_sketch walks its map with a golden-ratio stride. Java, Go and Rust start that walk at slot 0. C++ started at the first occupied slot, so its walk order could differ when slot 0 was empty. merge() inserts items in walk order, so a merge that purges could keep different items than Java does.

Changes

Side effects:

  • serialize() writes items in iterator order, so the C++ bytes change. The format does not change, and readers accept the bytes and get the same items and counts. The slot layout a reader rebuilds follows the byte order, as on master, so a later merge that purges can differ. The bytes still differ from Java, which writes in slot order. That is a separate difference, not part of this PR.
  • The pre-sort order of get_frequent_items() and to_string(true) changes. Their tie order still comes from std::sort, which is not stable.

Checks

  • Added fi/test/reverse_purge_hash_map_test.cpp:
    • iterator starts at slot 0 when slot 0 is empty: fails on master, which gives {1, 3, 4, 2}
    • iterator walks by stride from slot 0 when slot 0 is occupied: guards the walk when the change must not apply
    • begin equals end when the map is empty: an empty map must not loop
  • Added fi/test/frequent_items_sketch_test.cpp:
  • fi_test passes: 34 test cases, 186 assertions.
  • fi_test passes under ASan and UBSan.

This pull request and its description were written by Isaac.

stojkomilos and others added 2 commits December 1, 2026 15:01
Java, Go and Rust walk the map from slot 0 with the golden-ratio stride. C++ started at the first occupied slot, so merge inserted items in a different order and a purge could keep other items.

Co-authored-by: Isaac <no-reply@databricks.com>
Co-authored-by: Isaac <no-reply@databricks.com>

This branch has not been deployed

No deployments
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.

1 participant