Skip to content

Remove allocations in BaseUriHelper, improve resource resolution performance #10328

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Licensed to the .NET Foundation under one or more agreements.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

Expand Down Expand Up @@ -59,19 +59,15 @@ internal static List<string> LookupFolder(Uri uri)
// Perform a sanity check to make sure the assumption stays the same.
Debug.Assert(uri.IsAbsoluteUri && uri.Scheme == PackUriHelper.UriSchemePack && BaseUriHelper.IsPackApplicationUri(uri));

Assembly uriAssembly;
string escapedPath;

BaseUriHelper.GetAssemblyAndPartNameFromPackAppUri(uri, out uriAssembly, out escapedPath);

BaseUriHelper.GetAssemblyAndPartNameFromPackAppUri(uri, out Assembly uriAssembly, out ReadOnlySpan<char> escapedPath);
if (uriAssembly == null)
return null;

// If we added a fake filename to the uri, remove it from the escaped path.
if (isFolder)
{
Debug.Assert(escapedPath.EndsWith(FakeFileName, StringComparison.OrdinalIgnoreCase));
escapedPath = escapedPath.Substring(0, escapedPath.Length - FakeFileName.Length);
escapedPath = escapedPath.Slice(0, escapedPath.Length - FakeFileName.Length);
}

Dictionary<string, List<string>> folderResourceMap;
Expand All @@ -86,8 +82,7 @@ internal static List<string> LookupFolder(Uri uri)
}
}

List<string> ret;
folderResourceMap.TryGetValue(escapedPath, out ret);
folderResourceMap.GetAlternateLookup<ReadOnlySpan<char>>().TryGetValue(escapedPath, out List<string> ret);
return ret;
}

Expand Down Expand Up @@ -143,9 +138,8 @@ private static void AddResourceToFolderMap(Dictionary<string, List<string>> fold
folderResourceMap[resourceFullName].Add(String.Empty);
}

private static Dictionary<Assembly, Dictionary<string, List<string>>> _assemblyCaches
= new Dictionary<Assembly, Dictionary<string, List<string>>>(1);
// Set the initial capacity to 1 because a single entry assembly is the most common case.
private static readonly Dictionary<Assembly, Dictionary<string, List<string>>> _assemblyCaches = new(1);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,12 @@ namespace MS.Internal.Resources
// </summary>
internal static class ContentFileHelper
{
internal static bool IsContentFile(string partName)
internal static bool IsContentFile(ReadOnlySpan<char> partName)
{
if (_contentFiles == null)
s_contentFiles ??= GetContentFiles(BaseUriHelper.ResourceAssembly);
if (s_contentFiles is not null && s_contentFiles.Count > 0)
{
_contentFiles = GetContentFiles(BaseUriHelper.ResourceAssembly);
}

if (_contentFiles != null && _contentFiles.Count > 0)
{
if (_contentFiles.Contains(partName))
if (s_contentFiles.GetAlternateLookup<ReadOnlySpan<char>>().Contains(partName))
{
return true;
}
Expand Down Expand Up @@ -75,6 +71,6 @@ static internal HashSet<string> GetContentFiles(Assembly asm)
return contentFiles;
}

private static HashSet<string> _contentFiles;
private static HashSet<string> s_contentFiles;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,7 @@
<Compile Include="System\Windows\BinaryFormat\ListConverterHelper.cs" />
<Compile Include="System\Windows\BinaryFormat\FormatterConverterStub.cs" />
<Compile Include="System\Windows\BinaryFormat\SerializationExtensions.cs" />
<Compile Include="System\Windows\Navigation\AssemblyPackageInfo.cs" />
<Compile Include="System\Windows\Nrbf\SerializationRecordExtensions.cs" />
<Compile Include="System\Windows\BinaryFormat\MessageEnd.cs" />
<Compile Include="System\Windows\BinaryFormat\Record.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ private void UriSourcePropertyChangedHook(DependencyPropertyChangedEventArgs e)
{
if (!newUri.IsAbsoluteUri)
{
newUri = BaseUriHelper.GetResolvedUri(BaseUriHelper.BaseUri, newUri);
newUri = BaseUriHelper.GetResolvedUri(BaseUriHelper.PackAppBaseUri, newUri);
}

Debug.Assert(newUri.IsAbsoluteUri);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,14 @@ public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo c

if (!baseUri.IsAbsoluteUri)
{
baseUri = new Uri(BaseUriHelper.BaseUri, baseUri);
baseUri = new Uri(BaseUriHelper.PackAppBaseUri, baseUri);
}
}
else
{
// If we reach here, the base uri we got from IUriContext is "".
// Here we resolve it to application's base
baseUri = BaseUriHelper.BaseUri;
baseUri = BaseUriHelper.PackAppBaseUri;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

namespace System.Windows.Navigation
{
/// <summary>
/// Holds slices of pack/application URIs and exposes each slice through a property.
/// </summary>
/// <remarks>This should be dissected from relative URIs in format /AssemblyShortName{;Version]{;PublicKey];component/PackagePartName</remarks>
internal ref struct AssemblyPackageInfo
{
/// <summary>
/// Specifies the name of the package/resource.
/// </summary>
internal ReadOnlySpan<char> PackagePartName { get; set; }

/// <summary>
/// Holds Assembly.Name-like slice if specified, may be empty.
/// </summary>
/// <remarks>The slice is without leading forward slash ('/').</remarks>
internal ReadOnlySpan<char> AssemblyName { get; set; }
/// <summary>
/// Holds Assembly.Version if specified, may be empty.
/// </summary>
/// <remarks>The slice is without 'v' prefix.</remarks>
internal ReadOnlySpan<char> AssemblyVersion { get; set; }
/// <summary>
/// Holds Assembly.PublicKeyToken if specified, often is empty.
/// </summary>
internal ReadOnlySpan<char> AssemblyToken { get; set; }

}
}
Loading