Ray's Knowledge Base
Unlock

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 with append_cell equality. Strings use a string_view of 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: unique takes the first rows, sorts only the distinct values and dedupes with equal_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. nunique is the unique length. value_counts counts per id, then uses the existing stable sort. is_in on strings uses a string_view needle set and tests a dictionary once per entry. Frame unique combines 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::string keys.
  • 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