Skip to content

Minor performance improvements#401

Open
bbatsov wants to merge 2 commits intomasterfrom
performance-improvements
Open

Minor performance improvements#401
bbatsov wants to merge 2 commits intomasterfrom
performance-improvements

Conversation

@bbatsov
Copy link
Copy Markdown
Contributor

@bbatsov bbatsov commented Mar 14, 2026

Summary

  • Fix ProcessedSource force_encoding for frozen string inputs(+source).force_encoding(...) was creating a mutable copy but discarding the result, so the encoding fix was silently a no-op when a frozen string was passed. Now assigns the result back.
  • Avoid intermediate array allocations in HashNode#each_key/#each_value — the old implementation called pairs.map(&:key).each(&block), allocating a full intermediate array on every call. Now yields directly from each_child_node.

Benchmark results

--- HashNode#each_key ---
  each_key (current):  1055691.9 i/s
  each_key (old):       625038.9 i/s - 1.69x slower

--- HashNode#each_value ---
  each_value (current):  1076484.9 i/s
  each_value (old):       591610.7 i/s - 1.82x slower

--- Allocation counts (100 calls) ---
  each_key: old=1301 allocs, new=600 allocs

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.

1 participant