Skip to content

Route::match re-parses the route regexp on every successful match #1817

Description

@bigpresh

Status: confirmed and benchmarked

lib/Dancer2/Core/Route.pm:111-112:

my @token_or_splat =
  $self->regexp =~ /\(\?#((?:typed_)?token|(?:mega)?splat)\)/g;

The token/splat ordering is recovered by stringifying the compiled route regexp and scanning it for (?#...) comments — on every successful match.

Benchmark

re-scan regexp each match:  769,231/s  (~1.3us)
use precomputed arrayref: 2,000,000/s  (~0.5us)

Honest framing

~0.8us per matched route is small next to a full request, so this is tidy-up rather than a meaningful win. It is worth doing only because it is nearly free: _build_regexp_from_string (line 244) already computes this ordering at construction time to populate _params and _typed_params.

Suggested fix

Add a fourth slot to what _build_regexp_from_string returns, alongside _params / _typed_params / _should_capture — e.g. _token_order as an ArrayRef — and read it in match instead of scanning. See BUILDARGS at line 237 for the destructuring that needs extending.

Benchmark before and after rather than taking the above on faith.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions