Improve performance and reduce allocations of TemplateSegment (#856)
This commit is contained in:
parent
0f90a15cf1
commit
2081160678
|
|
@ -1,6 +1,7 @@
|
|||
// 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.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
|
|
@ -18,7 +19,17 @@ namespace Microsoft.AspNetCore.Routing.Template
|
|||
|
||||
public TemplateSegment(RoutePatternPathSegment other)
|
||||
{
|
||||
Parts = new List<TemplatePart>(other.Parts.Select(s => new TemplatePart(s)));
|
||||
if (other == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(other));
|
||||
}
|
||||
|
||||
var partCount = other.Parts.Count;
|
||||
Parts = new List<TemplatePart>(partCount);
|
||||
for (var i = 0; i < partCount; i++)
|
||||
{
|
||||
Parts.Add(new TemplatePart(other.Parts[i]));
|
||||
}
|
||||
}
|
||||
|
||||
public bool IsSimple => Parts.Count == 1;
|
||||
|
|
|
|||
|
|
@ -0,0 +1,49 @@
|
|||
// 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 Microsoft.AspNetCore.Routing.Patterns;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.AspNetCore.Routing.Template
|
||||
{
|
||||
public class TemplateSegmentTest
|
||||
{
|
||||
[Fact]
|
||||
public void Ctor_RoutePatternPathSegment_ShouldThrowArgumentNullExceptionWhenOtherIsNull()
|
||||
{
|
||||
const RoutePatternPathSegment other = null;
|
||||
|
||||
var actual = Assert.ThrowsAny<ArgumentNullException>(() => new TemplateSegment(other));
|
||||
Assert.Equal(nameof(other), actual.ParamName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ToRoutePatternPathSegment()
|
||||
{
|
||||
// Arrange
|
||||
var literalPartA = RoutePatternFactory.LiteralPart("A");
|
||||
var paramPartB = RoutePatternFactory.ParameterPart("B");
|
||||
var paramPartC = RoutePatternFactory.ParameterPart("C");
|
||||
var paramPartD = RoutePatternFactory.ParameterPart("D");
|
||||
var separatorPartE = RoutePatternFactory.SeparatorPart("E");
|
||||
var templateSegment = new TemplateSegment(RoutePatternFactory.Segment(paramPartC, literalPartA, separatorPartE, paramPartB));
|
||||
|
||||
// Act
|
||||
var routePatternPathSegment = templateSegment.ToRoutePatternPathSegment();
|
||||
templateSegment.Parts[1] = new TemplatePart(RoutePatternFactory.ParameterPart("D"));
|
||||
templateSegment.Parts.RemoveAt(0);
|
||||
|
||||
// Assert
|
||||
Assert.Equal(4, routePatternPathSegment.Parts.Count);
|
||||
Assert.IsType<RoutePatternParameterPart>(routePatternPathSegment.Parts[0]);
|
||||
Assert.Equal(paramPartC.Name, ((RoutePatternParameterPart) routePatternPathSegment.Parts[0]).Name);
|
||||
Assert.IsType<RoutePatternLiteralPart>(routePatternPathSegment.Parts[1]);
|
||||
Assert.Equal(literalPartA.Content, ((RoutePatternLiteralPart) routePatternPathSegment.Parts[1]).Content);
|
||||
Assert.IsType<RoutePatternSeparatorPart>(routePatternPathSegment.Parts[2]);
|
||||
Assert.Equal(separatorPartE.Content, ((RoutePatternSeparatorPart) routePatternPathSegment.Parts[2]).Content);
|
||||
Assert.IsType<RoutePatternParameterPart>(routePatternPathSegment.Parts[3]);
|
||||
Assert.Equal(paramPartB.Name, ((RoutePatternParameterPart) routePatternPathSegment.Parts[3]).Name);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue