Skip to content

PERF: restore hash table pre-allocation in value_count#65027

Merged
mroeschke merged 1 commit intopandas-dev:mainfrom
jbrockmendel:regrs-7
Apr 3, 2026
Merged

PERF: restore hash table pre-allocation in value_count#65027
mroeschke merged 1 commit intopandas-dev:mainfrom
jbrockmendel:regrs-7

Conversation

@jbrockmendel
Copy link
Copy Markdown
Member

@jbrockmendel jbrockmendel commented Apr 2, 2026

NB: motivated by an asv regression vs 3.0

Summary

  • Restore kh_resize(table, n) for non-object dtypes in value_count, reverting the inadvertent change to n // 10 from PERF: Eliminate redundant kh_get calls in hashtable operations #64543
  • The n // 10 pre-allocation causes expensive hash table resizes during insertion for float64 (whose hash function uses murmur2 + NaN handling), resulting in ~2x regression in Series.mode() for float dtype
  • Object dtype keeps n // 10 as it was in v3.0

Benchmark (float, N=100000)

median (ms)
n // 10 (regressed) 19.97
n (this PR) 10.66

Test plan

  • pytest pandas/tests/test_algos.py — 494 passed
  • pytest pandas/tests/base/test_value_counts.py pandas/tests/frame/methods/test_value_counts.py pandas/tests/series/methods/test_value_counts.py pandas/tests/frame/methods/test_duplicated.py pandas/tests/series/methods/test_duplicated.py — 301 passed

🤖 Generated with Claude Code

@jbrockmendel jbrockmendel added the Performance Memory or execution speed performance label Apr 2, 2026
…bject dtypes

The n // 10 pre-allocation introduced in pandas-dev#64543 causes expensive hash table
resizes for float64 (and other non-object types), resulting in ~2x regression
in Series.mode() for float dtype. Restore the v3.0 pre-allocation of n for
non-object types while keeping n // 10 for object dtype.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@mroeschke mroeschke added this to the 3.1 milestone Apr 3, 2026
@mroeschke mroeschke merged commit 8004fc5 into pandas-dev:main Apr 3, 2026
45 checks passed
@mroeschke
Copy link
Copy Markdown
Member

Thanks @jbrockmendel

@jbrockmendel jbrockmendel deleted the regrs-7 branch April 3, 2026 21:42
Sharl0tteIsTaken added a commit to Sharl0tteIsTaken/pandas that referenced this pull request Apr 4, 2026
…h-origin

* upstream/main: (83 commits)
  PERF: minor optimizations in _libs.algos (pandas-dev#65054)
  TST: Add roundtrip regression test for to_csv with embedded newlines (pandas-dev#65058)
  DOC: fix typo in timeseries documentation (ns -> us) (pandas-dev#65070)
  PERF: use searchsorted in IntervalIndex.get_indexer for monotonic indexes (pandas-dev#64786)
  PERF : Speed up `merge(sort=False)` for range-like unique integer join keys (left/right joins) (pandas-dev#64148)
  BUG: exclude RangeIndex from _can_use_libjoin, fix _union sorting (pandas-dev#64797)
  PERF: read SAS page headers in Cython instead of Python (pandas-dev#64769)
  BUG: date_range with start==end and inclusive="left"/"right" returns empty (pandas-dev#65014)
  PERF: restore hash table pre-allocation in value_count (pandas-dev#65027)
  TST: Add regression test for concat with tz_convert MonthStart index (pandas-dev#65019)
  BUG: DatetimeIndex._is_comparable_dtype raises AttributeError on Arrow date types (pandas-dev#64953)
  PERF: avoid expensive DataFrame snapshot in loc.__setitem__ (pandas-dev#65028)
  BUG: Fix AssertionError in replace with out-of-bounds datetime (pandas-dev#65009)
  PERF: add no_nans fast path in nancorr (pandas-dev#65046)
  API/DOC: remove iterability requirement for file-like object + clarify requirements in IO docstrings (pandas-dev#64986)
  BUG: date_range with periods=1 raises for offsets that disallow n=0 (pandas-dev#65011)
  CLN: remove using_cow parameter from _iLocIndexer._align_series (pandas-dev#65036)
  PERF: avoid redundant category re-validation in Categorical._simple_new (pandas-dev#65042)
  PERF: fix O(n) linear scan in _bin_search for ObjectEngine.get_loc (pandas-dev#65016)
  CLN: py2 leftover comments mostly (pandas-dev#65025)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Performance Memory or execution speed performance

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants