dotnet / dotnet/aspnetcore

Consider changing AlphaRouteConstraint to use IndexOfAnyValues instead of Regex

Open
#47,463 10 comments 0 reactions 0 assignees View on GitHub
area-mvc feature-routing Perf
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 10h
Merged PRs (30d)
281

Description

Currently the `AlphaRouteConstraint` implementation uses a compiled regex to check to see whether a route value is composed entirely of alpha characters.

We are making a few changes in other areas of the code base to make use of `IndexOfAnyValues` to determine whether a string is composed soley of a limited set of characters. It got me thinking what the performance difference would look like using `IndexOfAnyValues` instead of the compiled regex.

![c9486b08-cbf9-45ef-8a8b-9ee97329da54](https://user-images.githubusercontent.com/513398/228131431-482543ba-da03-4f16-b909-d72333da12ee.jpg)

Here is the corresponding code: https://gist.github.com/mitchdenny/3c6da9f9dd0589c424a5bdfd22239676

I brought this up in our teams chat and @JamesNK thought I should share it here and ping @stephentoub and @eerhardt.

> **NOTE**: The difference isn't as dramatic for very large string values (e.g. 400+ characters). I'm not sure if the lines ever intersect and Regex becomes faster or not - probably more of a case of the Regex overheads not mattering as much once you get to that size.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.