Skip to content

Add Hash#sort_by#1548

Merged
matz merged 1 commit into
matz:masterfrom
ryanseys:rs-hash-sort-by
Jun 24, 2026
Merged

Add Hash#sort_by#1548
matz merged 1 commit into
matz:masterfrom
ryanseys:rs-hash-sort-by

Conversation

@ryanseys

Copy link
Copy Markdown
Contributor

Implements Hash#sort_by, which previously hit the unsupported-call diagnostic. It yields each |key, value| pair and returns the [key, value] pairs ordered by the block's value.

What

  • emit_hash_sort_by_expr walks the entries through the shared emit_hash_block_eval binder, building a [sort_key, pair] tuple per entry.
  • A new runtime sp_PolyArray_sort_by_first sorts those tuples by their comparable sort key (sp_poly_cmp on element 0) and projects out the pairs — a Schwartzian transform. GC roots cover the dup, the working array, and the result.
  • Inference reports the result as a poly array.

Testing

  • New test/hash_sort_by.rb covers sorting by value, by a negated value, by string length, by the key, by a key transform (k.to_s), and over a string-valued hash — through monomorphic method parameters. Expected generated from real ruby.
  • Generated C is ASAN-clean.

`sort_by` on a hash hit the unsupported-call diagnostic. It yields each
|key, value| pair and returns the `[key, value]` pairs ordered by the block's
value.

`emit_hash_sort_by_expr` walks the entries through the shared
`emit_hash_block_eval` binder, building a `[sort_key, pair]` tuple per entry,
then defers to a new runtime `sp_PolyArray_sort_by_first` that sorts the
tuples by their comparable sort key and projects out the pairs (a Schwartzian
transform). Inference reports the result as a poly array.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request implements support for Hash#sort_by by adding type inference, code generation, and runtime sorting helpers, along with corresponding tests. The feedback highlights two critical safety issues: first, declaring and rooting GC variables (_tup and _tpair) inside a loop in src/codegen_fold.c can lead to dangling stack pointers and GC root stack overflow; second, using standard qsort in lib/sp_runtime.h with a comparison function that can trigger Ruby method calls is unsafe due to potential longjmp exceptions and GC compaction.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread src/codegen_fold.c
Comment thread lib/sp_runtime.h
@ryanseys ryanseys marked this pull request as ready for review June 24, 2026 03:26
@matz matz merged commit 10e7142 into matz:master Jun 24, 2026
3 checks passed
@ryanseys ryanseys deleted the rs-hash-sort-by branch June 24, 2026 14:53
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.

2 participants