Repository navigation
Use a hash set in the unique filter - #1052
Merged
Merged
Conversation
unique checked every value against everything kept so far, so it was quadratic in the array length. Strings, bools, ints and integral floats now go through a hash set keyed so that 1 and 1.0 still collide. Maps, arrays and non-integral floats keep the linear scan, but only among themselves since they can't equal anything hashable.
patrick-immometrica
marked this pull request as ready for review
October 24, 2026 21:13
Keats
requested changes
Oct 26, 2026
|
|
||
| // Using a set cause issues with maps because we do impl some Ord which messes up things | ||
| let mut res = Vec::with_capacity(val.len()); | ||
| let mut seen = HashSet::with_capacity(val.len()); |
Owner
There was a problem hiding this comment.
i think it's ok to leave that with HashSet::new()?
Contributor
Author
There was a problem hiding this comment.
Yes, changed.
| Some(Key::U128(f as u128)) | ||
| } else { | ||
| None | ||
| }; |
Owner
There was a problem hiding this comment.
Use Number::as_integer instead of having the same logic in multiple places
Contributor
Author
There was a problem hiding this comment.
Done. All numbers go through as_number()?.as_integer() now. Values outside i128 (big u128s, and floats like 2^127) fall back to the linear scan, so Value::eq still decides for them. I added test_unique_numbers_outside_i128 for that case.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
uniquechecked each value withres.contains(v), so it was quadratic in the array length. A list of a few thousand ids took tens of milliseconds just for that filter.This puts strings, bools, ints and integral floats in a hash set instead. Integral floats are keyed as the integer they equal, so
1and1.0still count as duplicates the wayValue::eqsays. Maps, arrays and non-integral floats can't go in the set (maps all compare equal in ourOrd, which is why aBTreeSetwouldn't work either), but they can only ever equal each other, so they keep the linear scan among themselves only. Order of first occurrence is unchanged.I added a line to the
sort_and_uniquesnapshot that mixes ints, floats,-0.0, safe and normal strings, bools andnone. The snapshot was generated with the old implementation and passes unchanged with the new one.Before/after on
{{ arr | unique | length }}with half the values duplicated (release build, µs per render, range over two runs):