Distinct-value operations are keyed by int32 value ids
DecisionVerified 4 Oct 2026Holds project: dftracer-utils
Decision. The context, the choice, why, and the options that were turned down.
Context#
unique, value_counts, is_in and frame unique sorted every row or built per-row std::string keys.
Decision#
- D1:
value_ids(col)gives an int32 id per row (first-seen order, null is one id) and the first row per id, keyed withappend_cellequality. Strings use astring_viewof the bytes in place (ankerl map). A dictionary is keyed once per entry with codes mapped. Fixed width uses raw bytes. It runs chunk by chunk with one shared map and no per-row allocation. - D2:
uniquetakes the first rows, sorts only the distinct values and dedupes withequal_at. Every float column (Float16, Float32, Float64) keeps the old full sort of all rows: NaN != NaN keeps each NaN row, and the sort decides whether 0.0 or -0.0 survives, so ids keyed by bits would change results.nuniqueis theuniquelength.value_countscounts per id, then uses the existing stable sort.is_inon strings uses astring_viewneedle set and tests a dictionary once per entry. Frameuniquecombines per-column ids through a (prev id, col id) uint64 map.
Why#
Measured on 1.3M rows: value_counts 168 -> 1.4 ms, frame unique 136 -> 4.1 ms, unique path 40 -> 1.0 ms, is_in 27 -> 1.0 ms.
Rejected options#
- Sort-based distinct.
- Per-row
std::stringkeys. - Changing join keys: measured within noise.
Also found by searching
- how are unique and value_counts keyed in dataframe
- value_counts is slow on string columns
- frame unique slow on many rows
- is_in on strings is slow