From 80e1f03174f5aae5b01d6ebedbe340e81333d689 Mon Sep 17 00:00:00 2001 From: Eric Proulx Date: Thu, 10 Sep 2026 21:10:09 +0200 Subject: [PATCH] Skip the renamed-params lookup in `declared` when nothing is renamed MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `build_memo_key` looks every declared param up in the route's renamed params, keyed by the param's whole path. That key is built fresh each time, two Arrays and a String per declared param, on every `declared` call. Only an API using `as:` has anything in that table; everywhere else it is empty and every lookup misses. The lookup is now skipped when the table is empty: `declared` on five params went from 4.81 to 4.29 µs (6.16 to 5.59 µs with missing keys included). Co-Authored-By: Claude Opus 5 --- CHANGELOG.md | 1 + lib/grape/declared_params_handler.rb | 5 ++++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fdc7d3a3e..9dda411fc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ * [#2918](https://github.com/ruby-grape/grape/pull/2918): Skip the dry-types round trip when a value already is the declared type - [@ericproulx](https://github.com/ericproulx). * [#2917](https://github.com/ruby-grape/grape/pull/2917): Read path captures out of the router's union match instead of re-running the route's pattern - [@ericproulx](https://github.com/ericproulx). * [#2921](https://github.com/ruby-grape/grape/pull/2921): Pin the router's request-time isolation regressions through requests instead of its instance variables - [@ericproulx](https://github.com/ericproulx). +* [#2926](https://github.com/ruby-grape/grape/pull/2926): Skip the renamed-params lookup in `declared` when nothing is renamed - [@ericproulx](https://github.com/ericproulx). * Your contribution here. ### 4.0.0 (2026-09-07) diff --git a/lib/grape/declared_params_handler.rb b/lib/grape/declared_params_handler.rb index 5191a49d1..b8d57b4d3 100644 --- a/lib/grape/declared_params_handler.rb +++ b/lib/grape/declared_params_handler.rb @@ -87,8 +87,11 @@ def declare_leaf(passed_params, declared_param:, params_nested_path:, memo:, ren end end + # The lookup key is the param's whole path, built fresh -- two Arrays and a + # String per declared param -- and only an API using +as:+ has anything to + # find with it, so the common empty table is not asked. def build_memo_key(params_nested_path, declared_param, renamed_params) - renamed_param_name = renamed_params[nested_path_for(params_nested_path, declared_param)] + renamed_param_name = renamed_params[nested_path_for(params_nested_path, declared_param)] unless renamed_params.empty? param = renamed_param_name || declared_param @stringify ? param.to_s : param.to_sym end