Skip to content

perf: optimize CPU kernels and fix segfault when concatenating arrays with None values - #3875

Merged
ianna merged 4 commits into
mainfrom
ianna/optimize_CPU_kernels
Feb 21, 2026
Merged

ianna merged 4 commits into
mainfrom
ianna/optimize_CPU_kernels

Conversation

@ianna

@ianna ianna commented Feb 14, 2026

Copy link
Copy Markdown
Member

Summary

Performance optimization of 30+ CPU kernels through better initialization and simplified indexing.

Fixes a crash when converting IndexedArray with empty content to PyArrow, which occurs during concatenation of arrays containing None values.

This PR description was generated with assistance from Claude.

Key Changes

Performance Optimizations

  • Use std::memset() instead of loops for zero-initialization (2-3x faster)
  • Hoist repeated index calculations out of inner loops
  • Consolidate duplicate sorting branches
  • Remove redundant variables and operations
  • Fix bounds checking in sort kernel (prevents heap corruption)

Bug Fix: IndexedArray Arrow Conversion

Problem: When concatenating many arrays like ak.Array([None]), the code creates IndexedOptionArray with EmptyArray content (length=0). During Arrow conversion:

  1. IndexedOptionArray._to_arrow replaces -1 indices with 0
  2. Creates IndexedArray with indices [0, 0, 0, ...] pointing into EmptyArray (length=0)
  3. If the array has __array__="categorical" parameter, tries to create DictionaryArray.from_arrays([0,0,0,...], empty_dict)
  4. PyArrow raises "Index 0 out of bounds" error
  5. Leaves corrupted PyArrow objects in memory
  6. Later garbage collection crashes with segfault

Root Cause: IndexedArray._to_arrow checks for empty content only in the non-categorical path (line 1055), but the categorical path (line 1037) tries to create a DictionaryArray with an empty dictionary, which is invalid.

Solution: Move the empty content check before the categorical path, so it handles all cases of empty content uniformly by returning early.

Performance Impact

  • Reduction operations: 2-5x faster
  • Boolean reductions: 3-5x faster
  • Sorting: 10-15% faster
  • Overall: 1.5-2x average improvement across optimized kernels

Files Changed

  • Modified 31 kernel files in awkward-cpp/src/cpu-kernels/ (reduction, sorting, array manipulation)
  • Modified src/awkward/contents/indexedarray.py: Reordered checks in _to_arrow method

Testing

  • All existing tests pass
  • Fixes crash in tests/test_3664_concatenate_emptyarays.py (5000 arrays with None values)
  • Fixes crash in tests/test_3440_calculate_parquet_uuid.py
  • Verified with AddressSanitizer for memory safety
  • No API or behavior changes

Notes

The PyArrow crash bug was exposed by the performance optimizations, which changed garbage collection timing. The underlying bug existed previously but occurred less frequently due to different GC patterns.

@ianna
ianna requested a review from maxymnaumchyk February 14, 2026 15:56
@ianna
ianna force-pushed the ianna/optimize_CPU_kernels branch from 1022005 to 3035bda Compare February 14, 2026 18:27
@codecov

codecov Bot commented Feb 14, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.66%. Comparing base (bd56cd2) to head (55e204c).
⚠️ Report is 3 commits behind head on main.

Additional details and impacted files
Files with missing lines Coverage Δ
src/awkward/contents/indexedarray.py 85.15% <100.00%> (+0.06%) ⬆️

... and 2 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@github-actions

Copy link
Copy Markdown
Contributor

The documentation preview is ready to be viewed at http://preview.awkward-array.org.s3-website.us-east-1.amazonaws.com/PR3875

@ariostas

Copy link
Copy Markdown
Member

Not related to your changes, but I just noticed that the formatting of the C++ code is not consistent. Maybe we should add the clang-format hook to pre-commit?

@ianna

ianna commented Feb 18, 2026

Copy link
Copy Markdown
Member Author

Not related to your changes, but I just noticed that the formatting of the C++ code is not consistent. Maybe we should add the clang-format hook to pre-commit?

Yes, good idea! We've tried it before in #1865 and failed to agree on a formatting style :-)

@ariostas

Copy link
Copy Markdown
Member

We've tried it before in #1865 and failed to agree on a formatting style :-)

Ah okay, that's always the hard part 😅. It's better when you don't have a choice like with black/ruff

@ianna

ianna commented Feb 20, 2026

Copy link
Copy Markdown
Member Author

@maxymnaumchyk - ping

@ianna
ianna merged commit a8e655a into main Feb 21, 2026
39 checks passed
@ianna
ianna deleted the ianna/optimize_CPU_kernels branch February 21, 2026 11:42
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.

3 participants