Skip to content

Performance optimizations and release 5.1.1 - #23

Merged
jlsync merged 1 commit into
masterfrom
perf-optimizations-followup
Nov 20, 2026
Merged

jlsync merged 1 commit into
masterfrom
perf-optimizations-followup

Conversation

@jlsync

@jlsync jlsync commented Nov 19, 2026

Copy link
Copy Markdown
Collaborator

Summary

Follow-up non-invasive performance optimizations across path introspection, resource services, specifications, named route helpers, and controller helpers, releasing 5.1.1.

Key Changes

  • Path introspection (RequestPathIntrospection#remove_current_segment): Replaced per-request lookahead Regexp compilation with UTF-8-safe byte-indexed substring search (byterindex + byteslice), with a character-index fallback for segments containing /. Preserves fresh mutable return strings and original encoding.
  • Resource services (ResourceService / SingletonResourceService):
    • Call service.__send__(:find, ...) directly in ResourceService#find instead of falling back through super → method_missing.
    • Replaced ActiveSupport delegate wrappers with direct controller reader methods (preserving subclass overrides on both proxy methods and #controller).
    • Reused lazy SingletonSpecification#build_method_name in SingletonResourceService#new.
  • Specifications (Specification / SingletonSpecification):
    • Replaced options.assert_valid_keys(...) varargs allocation with a frozen VALID_OPTION_KEYS check.
    • Fast-pathed default foreign_key for simple snake_case names.
    • Added frozen/lazy plural_name (reused as a fresh mutable copy in #resources_name) and lazy params_method_name (reused in #resource_params when #resource_name is not overridden).
  • Named route helpers (NamedRouteHelper):
    • Precomputed class-level plural_route_name with an immutable plural_route_name_source snapshot (invalidated on class/instance overrides or in-place mutation).
    • Read name_prefix once per helper dispatch, guarded empty encl / args / single-element encl splats to avoid unnecessary concatarray allocations, and preserved eql? filtering semantics for enclosing route helpers.
  • Controller & view helpers (ResourceMethods / Helper):
    • Reduced redundant params lookups in #find_resource, #new_resource, and #destroy_resource, skipped duplicate param key lookup in #new_resource, forwarded anonymous & blocks, and removed .tap from Helper#form_for_resource_options.
  • Release packaging: Updated CHANGELOG and bumped ResourcesController::VERSION to 5.1.1.

Benchmark Highlights (Ruby 4.0.10 +YJIT, Rails 8.1.4)

  • remove_current_segment: 11–13× faster, -14 to -15 allocs/op
  • resources_name (fresh controller): ~16× faster, -5 allocs/op
  • ResourceService#find: ~3.3× faster, -1 alloc/op
  • SingletonResourceService#new: ~2.3× faster, -1 alloc/op
  • new_resource (empty params): ~1.3× faster

Verification

  • bundle exec rspec: 723 examples, 0 failures (22 new regression specs covering multibyte UTF-8 paths, overlapping multi-slash segments, duck-typed specifications, proxy overrides, eql? filtering, mutability/value invariants, and route_name overrides).
  • Reviewed across two independent passes by Claude and Codex with all actionable findings addressed.

- Replace per-request lookahead Regexp compilation in RequestPathIntrospection#remove_current_segment with UTF-8-safe byte-indexed substring search (with character-index fallback for multi-slash segments)
- Call service.__send__(:find, ...) directly in ResourceService#find, replace ActiveSupport delegate wrappers with lightweight controller readers, and reuse lazy SingletonSpecification#build_method_name
- Fast-path Specification option validation and default foreign_key derivation, and add lazy plural_name / params_method_name helpers reused by #resources_name and #resource_params
- Precompute class-level plural_route_name in resources_controller_for, read name_prefix once per NamedRouteHelper dispatch, guard empty enclosing/args splats, and preserve eql? enclosing-resource filtering
- Reduce redundant params lookups in ResourceMethods, forward anonymous blocks in #new_resource, and simplify Helper#form_for_resource_options
- Add regression specs covering all optimized paths and edge cases, update CHANGELOG, and bump version to 5.1.1

Co-authored-by: Antigravity (Gemini 4) <antigravity@google.com>
@jlsync
jlsync merged commit 32cdb80 into master Nov 20, 2026
6 checks passed
@jlsync
jlsync deleted the perf-optimizations-followup branch November 20, 2026 05:38
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