diff --git a/src/Http/Routing/perf/Microbenchmarks/Matching/JumpTableMultipleEntryBenchmark.cs b/src/Http/Routing/perf/Microbenchmarks/Matching/JumpTableMultipleEntryBenchmark.cs index 77dd0f85c0e0..340982a922f3 100644 --- a/src/Http/Routing/perf/Microbenchmarks/Matching/JumpTableMultipleEntryBenchmark.cs +++ b/src/Http/Routing/perf/Microbenchmarks/Matching/JumpTableMultipleEntryBenchmark.cs @@ -27,7 +27,7 @@ public void Setup() for (var i = 0; i < _strings.Length; i++) { - _segments[i] = new PathSegment(0, _strings[i].Length); + _segments[i] = new PathSegment(1, _strings[i].Length - 2); } var samples = new int[Count]; @@ -39,7 +39,9 @@ public void Setup() var entries = new List<(string text, int _)>(); for (var i = 0; i < samples.Length; i++) { - entries.Add((_strings[samples[i]], i)); + var sampleIndex = samples[i]; + var segment = _segments[sampleIndex]; + entries.Add((_strings[sampleIndex].Substring(segment.Start, segment.Length), i)); } _linearSearch = new LinearSearchJumpTable(0, -1, entries.ToArray()); diff --git a/src/Http/Routing/src/Matching/DictionaryJumpTable.cs b/src/Http/Routing/src/Matching/DictionaryJumpTable.cs index 18c7754bf83c..4e73cf840fde 100644 --- a/src/Http/Routing/src/Matching/DictionaryJumpTable.cs +++ b/src/Http/Routing/src/Matching/DictionaryJumpTable.cs @@ -12,6 +12,7 @@ internal sealed class DictionaryJumpTable : JumpTable private readonly int _defaultDestination; private readonly int _exitDestination; private readonly FrozenDictionary _dictionary; + private readonly FrozenDictionary.AlternateLookup> _lookup; public DictionaryJumpTable( int defaultDestination, @@ -22,6 +23,7 @@ public DictionaryJumpTable( _exitDestination = exitDestination; _dictionary = entries.ToFrozenDictionary(e => e.text, e => e.destination, StringComparer.OrdinalIgnoreCase); + _lookup = _dictionary.GetAlternateLookup>(); } public override int GetDestination(string path, PathSegment segment) @@ -31,8 +33,8 @@ public override int GetDestination(string path, PathSegment segment) return _exitDestination; } - var text = path.Substring(segment.Start, segment.Length); - if (_dictionary.TryGetValue(text, out var destination)) + var text = path.AsSpan(segment.Start, segment.Length); + if (_lookup.TryGetValue(text, out var destination)) { return destination; }