Skip to content

Build param defaults and custom type coercions from frozen objects - #3097

Merged
ericproulx merged 1 commit into
masterfrom
refactor/defaults-coercers-without-procs
Nov 21, 2026
Merged

ericproulx merged 1 commit into
masterfrom
refactor/defaults-coercers-without-procs

Conversation

@ericproulx

@ericproulx ericproulx commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Part of making Grape's internals readable from a non-main Ractor, without calling the Ractor API from lib/ (groundwork for #2978). Split from #3095, with the rest in #3096 and #3098.

DefaultValidator and CustomTypeCoercer built procs whose self was the validator or the coercer, closing over the default or the coercion method. Ractor.make_shareable refuses such a proc, so a compiled API declaring a default:, a coerce_with: or a custom type without parsed? could not be made shareable. They now build small Data objects answering call. A deep freeze reaches an object's fields in whatever order it walks, while a proc's captured values have to be shareable already.

Was Now
DefaultValidator proc { options }, proc { options.dup }, proc { options.call } Value, Copy, Deferred
CustomTypeCoercer lambdas symbolizing a Hash or a collection, and checking is_a? or an Enumerable type HashSymbolizer, CollectionSymbolizer, InstanceCheck, EnumerableCheck

A default lambda taking the params, and a coerce_with: lambda, are the user's own and stay as given. One written inside a params block has the ParamsScope as its self; that is for #2978.

Throughput

Ruby 4.0.6, 6 rounds from one checkout, alternating which side runs first, median of the per-round ratios, measured with the change this was split from. Within noise:

YJIT interpreter
custom coercions (Hash, Array[Integer] with coerce_with:, a custom type) −0.6% (4/6 rounds faster) +1.8% (2/6)
defaults ([], a value, a lambda) +0.4% (3/6) −1.9% (4/6)

🤖 Generated with Claude Code

DefaultValidator and CustomTypeCoercer built procs whose self was the
validator or the coercer, closing over the default or the coercion
method, so a compiled API holding one could not be frozen all the way
down. They build small Data objects answering call instead.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ericproulx
ericproulx force-pushed the refactor/defaults-coercers-without-procs branch from de5911e to 21ae0f5 Compare November 21, 2026 17:44
@ericproulx
ericproulx merged commit 19e5dad into master Nov 21, 2026
52 checks passed
@ericproulx
ericproulx deleted the refactor/defaults-coercers-without-procs branch November 21, 2026 17:50
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