Coerce through dry-types' non-raising call - #2930
Closed
ericproulx wants to merge 1 commit into
Closed
ericproulx wants to merge 1 commit into
ericproulx wants to merge 1 commit into
Conversation
`DryTypeCoercer#call` coerced with `@coercer[val]` and rescued `Dry::Types::CoercionError` for a value it could not coerce. That raise is what cost: dry-types' `CoercionError.handle` re-raises with `backtrace: exception.backtrace` of the error underneath, which builds that whole backtrace as Strings. At request depth a rejected value cost about 30 µs, where an accepted one costs a few hundred nanoseconds. It is paid by every `types: [Integer, String]` param given a string, since each coercer tried before the one that accepts it fails first; by every 400 for a mistyped value; and by invalid dates, numbers and booleans. dry-types' documented block form of `Type#call` reports a failure by calling the block instead of raising, so the coercer now passes one that answers `InvalidValue`. The `rescue` stays for anything that raises regardless. A rejected Integer at request depth went from 31.2 to 4.4 µs. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
ericproulx
force-pushed
the
perf/coerce-without-raising
branch
from
September 11, 2026 08:32
b64b098 to
86deee6
Compare
Danger ReportErrors
Markdowns* [#XXXX](https://github.com/ruby-grape/grape/pull/XXXX): Coerce through dry-types' non-raising call so a rejected value no longer builds a backtrace - [@ericproulx](https://github.com/ericproulx).
does not include a pull request link |
5 tasks
Contributor
Author
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
DryTypeCoercer#callcoerced with@coercer[val]and rescuedDry::Types::CoercionErrorfor a value it could not coerce. The raise is what cost: dry-types'CoercionError.handlere-raises withbacktrace: exception.backtraceof the error underneath (theArgumentErrorfromInteger(),Float(),Date.parse, …), which builds that whole backtrace as Strings. At request depth a rejected value cost about 30 µs, where an accepted one costs a few hundred nanoseconds.It is paid by:
types: [Integer, String]param given a string, since each coercer tried before the one that accepts it fails first;?id=abcfor an Integer);dry-types' documented block form of
Type#call("When a block is passed,callwill never throw an exception on failed coercion, instead it will call the block") reports a failure by calling the block, so the coercer now passes one that answersInvalidValue. Therescuestays for anything that raises regardless.A rejected Integer at a request's stack depth: 31.2 → 4.4 µs.
Benchmarks
Median of interleaved subprocess rounds against master, Ruby 4.0.6 (7 rounds without JIT, 5 with YJIT):
types: [Integer, String]givenabc, plus anArray[Integer]withcoerce_with?id=abconrequires :id, type: IntegerBehaviour
Byte-identical to master over a 2,214-case coercer matrix, calling
Types.build_coercerdirectly: 27 types (every primitive,Numeric,Grape::API::Boolean,TrueClass/FalseClass,Hash,Array,Array[T],Set[T], multiple-type lists including nested collections), each coerced and strict, × 41 inputs (valid, malformed, blank,nil, arrays, hashes, symbols, invalid bytes). 1,640 of those cases are rejections, so the changed path is the one exercised. The request-level coercion and validation matrices are unchanged too.Test plan
nilfails 37 specs; answering the raw value fails 29. Reverting to the raising call changes nothing observable, which is the point; the benchmark pins that.🤖 Generated with Claude Code