| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- // Copyright (c) .NET Foundation. All rights reserved.
- // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
- using System;
- using System.Runtime.CompilerServices;
- using System.Runtime.InteropServices;
- using BenchmarkDotNet.Attributes;
- namespace Microsoft.AspNetCore.Routing.Matching
- {
- public class JumpTableSingleEntryBenchmark
- {
- private JumpTable _default;
- private JumpTable _trie;
- private JumpTable _vectorTrie;
- private JumpTable _ascii;
- private string[] _strings;
- private PathSegment[] _segments;
- [GlobalSetup]
- public void Setup()
- {
- _default = new SingleEntryJumpTable(0, -1, "hello-world", 1);
- _trie = new ILEmitTrieJumpTable(0, -1, new[] { ("hello-world", 1), }, vectorize: false, _default);
- _vectorTrie = new ILEmitTrieJumpTable(0, -1, new[] { ("hello-world", 1), }, vectorize: true, _default);
- _ascii = new SingleEntryAsciiJumpTable(0, -1, "hello-world", 1);
- _strings = new string[]
- {
- "index/foo/2",
- "index/hello-world1/2",
- "index/hello-world/2",
- "index//2",
- "index/hillo-goodbye/2",
- };
- _segments = new PathSegment[]
- {
- new PathSegment(6, 3),
- new PathSegment(6, 12),
- new PathSegment(6, 11),
- new PathSegment(6, 0),
- new PathSegment(6, 13),
- };
- }
- [Benchmark(Baseline = true, OperationsPerInvoke = 5)]
- public int Baseline()
- {
- var strings = _strings;
- var segments = _segments;
- int destination = 0;
- for (var i = 0; i < strings.Length; i++)
- {
- var @string = strings[i];
- var segment = segments[i];
- if (segment.Length == 0)
- {
- destination = -1;
- }
- else if (segment.Length != "hello-world".Length)
- {
- destination = 1;
- }
- else
- {
- destination = string.Compare(
- @string,
- segment.Start,
- "hello-world",
- 0,
- segment.Length,
- StringComparison.OrdinalIgnoreCase);
- }
- }
- return destination;
- }
- [Benchmark(OperationsPerInvoke = 5)]
- public int Default()
- {
- var strings = _strings;
- var segments = _segments;
- var destination = 0;
- for (var i = 0; i < strings.Length; i++)
- {
- destination = _default.GetDestination(strings[i], segments[i]);
- }
- return destination;
- }
- [Benchmark(OperationsPerInvoke = 5)]
- public int Ascii()
- {
- var strings = _strings;
- var segments = _segments;
- var destination = 0;
- for (var i = 0; i < strings.Length; i++)
- {
- destination = _ascii.GetDestination(strings[i], segments[i]);
- }
- return destination;
- }
- [Benchmark(OperationsPerInvoke = 5)]
- public int Trie()
- {
- var strings = _strings;
- var segments = _segments;
- var destination = 0;
- for (var i = 0; i < strings.Length; i++)
- {
- destination = _trie.GetDestination(strings[i], segments[i]);
- }
- return destination;
- }
- [Benchmark(OperationsPerInvoke = 5)]
- public int VectorTrie()
- {
- var strings = _strings;
- var segments = _segments;
- var destination = 0;
- for (var i = 0; i < strings.Length; i++)
- {
- destination = _vectorTrie.GetDestination(strings[i], segments[i]);
- }
- return destination;
- }
- }
- }
|