Skip to content

Commit 55342e2

Browse files
authored
Remove intermediate strings from hint name calculation (#12077)
Got a new ARM laptop, thought I'd try a little item from my TODO list (#11788 (comment)), ended up worse than I ever expected.
2 parents ce522dd + 18ba0e9 commit 55342e2

File tree

4 files changed

+65
-7
lines changed

4 files changed

+65
-7
lines changed

src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/SourceGenerators/RazorSourceGenerator.Helpers.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4+
using System;
45
using System.Collections.Generic;
5-
using System.Text;
66
using Microsoft.AspNetCore.Mvc.Razor.Extensions;
77
using Microsoft.AspNetCore.Razor.Language;
8+
using Microsoft.AspNetCore.Razor.PooledObjects;
89
using Microsoft.CodeAnalysis;
910
using Microsoft.CodeAnalysis.Razor;
1011

1112
namespace Microsoft.NET.Sdk.Razor.SourceGenerators
1213
{
1314
public partial class RazorSourceGenerator
1415
{
15-
internal static string GetIdentifierFromPath(string filePath)
16+
internal static string GetIdentifierFromPath(ReadOnlySpan<char> filePath)
1617
{
17-
var builder = new StringBuilder(filePath.Length);
18+
using var _ = StringBuilderPool.GetPooledObject(out var builder);
1819

1920
for (var i = 0; i < filePath.Length; i++)
2021
{
@@ -31,6 +32,7 @@ internal static string GetIdentifierFromPath(string filePath)
3132
}
3233

3334
builder.Append(".g.cs");
35+
3436
return builder.ToString();
3537
}
3638

src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Extensions/TextDocumentExtensions.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System;
45
using System.Diagnostics.CodeAnalysis;
5-
using System.IO;
66
using Microsoft.AspNetCore.Razor;
77
using Microsoft.NET.Sdk.Razor.SourceGenerators;
88

@@ -25,8 +25,18 @@ public static bool TryComputeHintNameFromRazorDocument(this TextDocument razorDo
2525
return false;
2626
}
2727

28-
var projectBasePath = Path.GetDirectoryName(razorDocument.Project.FilePath).AssumeNotNull();
29-
var relativeDocumentPath = razorDocument.FilePath[projectBasePath.Length..].TrimStart('/', '\\');
28+
var filePath = razorDocument.FilePath.AsSpanOrDefault();
29+
var projectFilePath = razorDocument.Project.FilePath.AsSpanOrDefault();
30+
var projectBasePath = PathUtilities.GetDirectoryName(projectFilePath);
31+
if (filePath.Length <= projectBasePath.Length)
32+
{
33+
// File must be from outside the project directory
34+
hintName = null;
35+
return false;
36+
}
37+
38+
var relativeDocumentPath = filePath[projectBasePath.Length..].TrimStart(['/', '\\']);
39+
3040
hintName = RazorSourceGenerator.GetIdentifierFromPath(relativeDocumentPath);
3141

3242
return hintName is not null;

src/Razor/test/Microsoft.AspNetCore.Razor.Test.Common.Tooling/XunitDisposeHook.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,10 @@ public void Execute()
6363
// ret
6464
Marshal.WriteByte(functionPointer, 0xC3);
6565
break;
66-
66+
case Architecture.Arm64:
67+
// This place is not a place of honor. No highly esteemed deed is commemorated here.
68+
Marshal.WriteInt64(functionPointer, 0xD65F03C0);
69+
break;
6770
default:
6871
throw new NotSupportedException();
6972
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System.IO;
5+
using Microsoft.AspNetCore.Razor.Test.Common;
6+
using Microsoft.AspNetCore.Razor.Test.Common.VisualStudio;
7+
using Microsoft.CodeAnalysis;
8+
using Xunit;
9+
using Xunit.Abstractions;
10+
11+
namespace Microsoft.VisualStudio.LanguageServices.Razor.Test.Cohost;
12+
13+
public class TextDocumentExtensionsTest(ITestOutputHelper testOutput) : VisualStudioWorkspaceTestBase(testOutput)
14+
{
15+
[Theory]
16+
[InlineData(@"Pages\Index.razor")]
17+
[InlineData(@"Pages/Index.razor")]
18+
[InlineData(@"Pages.Index.razor")]
19+
public void TryComputeHintNameFromRazorDocument(string razorFilePath)
20+
{
21+
var projectId = ProjectId.CreateNewId();
22+
var projectInfo = ProjectInfo
23+
.Create(
24+
projectId,
25+
VersionStamp.Create(),
26+
name: "Project",
27+
assemblyName: "Project",
28+
LanguageNames.CSharp,
29+
TestProjectData.SomeProject.FilePath);
30+
31+
var documentId = DocumentId.CreateNewId(projectId);
32+
var solution = Workspace.CurrentSolution
33+
.AddProject(projectInfo)
34+
.AddAdditionalDocument(documentId, "File.razor", "", filePath: Path.Combine(TestProjectData.SomeProjectPath, razorFilePath));
35+
36+
var document = solution.GetAdditionalDocument(documentId);
37+
38+
Assert.NotNull(document);
39+
Assert.True(document.TryComputeHintNameFromRazorDocument(out var hintName));
40+
// This tests TryComputeHintNameFromRazorDocument and also neatly demonstrates a bug: https://github.com/dotnet/razor/issues/11578
41+
Assert.Equal("Pages_Index_razor.g.cs", hintName);
42+
}
43+
}

0 commit comments

Comments
 (0)