-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Small memory allocation optimization in DefaultRazorTagHelperBinderPh… #22887
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -247,7 +247,8 @@ public ComponentDirectiveVisitor(string filePath, IReadOnlyList<TagHelperDescrip | |
| { | ||
| // If this is a child content tag helper, we want to add it if it's original type is in scope. | ||
| // E.g, if the type name is `Test.MyComponent.ChildContent`, we want to add it if `Test.MyComponent` is in scope. | ||
| TrySplitNamespaceAndType(typeName, out typeName, out var _); | ||
| TrySplitNamespaceAndType(typeName, out var typeNameTextSpan, out var _); | ||
| typeName = GetTextSpanContent(typeNameTextSpan, typeName); | ||
| } | ||
|
|
||
| if (currentNamespace != null && IsTypeInScope(typeName, currentNamespace)) | ||
|
|
@@ -336,7 +337,8 @@ public override void VisitRazorDirective(RazorDirectiveSyntax node) | |
| { | ||
| // If this is a child content tag helper, we want to add it if it's original type is in scope of the given namespace. | ||
| // E.g, if the type name is `Test.MyComponent.ChildContent`, we want to add it if `Test.MyComponent` is in this namespace. | ||
| TrySplitNamespaceAndType(typeName, out typeName, out var _); | ||
| TrySplitNamespaceAndType(typeName, out var typeNameTextSpan, out var _); | ||
| typeName = GetTextSpanContent(typeNameTextSpan, typeName); | ||
| } | ||
| if (typeName != null && IsTypeInNamespace(typeName, @namespace)) | ||
| { | ||
|
|
@@ -350,13 +352,13 @@ public override void VisitRazorDirective(RazorDirectiveSyntax node) | |
|
|
||
| internal static bool IsTypeInNamespace(string typeName, string @namespace) | ||
| { | ||
| if (!TrySplitNamespaceAndType(typeName, out var typeNamespace, out var _) || typeNamespace == string.Empty) | ||
| if (!TrySplitNamespaceAndType(typeName, out var typeNamespace, out var _) || typeNamespace.Length == 0) | ||
| { | ||
| // Either the typeName is not the full type name or this type is at the top level. | ||
| return true; | ||
| } | ||
|
|
||
| return typeNamespace.Equals(@namespace, StringComparison.Ordinal); | ||
| return @namespace.Length == typeNamespace.Length && 0 == string.CompareOrdinal(typeName, typeNamespace.Start, @namespace, 0, @namespace.Length); | ||
ajaybhargavb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| // Check if the given type is already in scope given the namespace of the current document. | ||
|
|
@@ -366,12 +368,13 @@ internal static bool IsTypeInNamespace(string typeName, string @namespace) | |
| // Whereas `MyComponents.SomethingElse.OtherComponent` is not in scope. | ||
| internal static bool IsTypeInScope(string typeName, string currentNamespace) | ||
| { | ||
| if (!TrySplitNamespaceAndType(typeName, out var typeNamespace, out var _) || typeNamespace == string.Empty) | ||
| if (!TrySplitNamespaceAndType(typeName, out var typeNamespaceTextSpan, out var _) || typeNamespaceTextSpan.Length == 0) | ||
| { | ||
| // Either the typeName is not the full type name or this type is at the top level. | ||
| return true; | ||
| } | ||
|
|
||
| var typeNamespace = GetTextSpanContent(typeNamespaceTextSpan, typeName); | ||
| var typeNamespaceSegments = typeNamespace.Split(NamespaceSeparators, StringSplitOptions.RemoveEmptyEntries); | ||
| var currentNamespaceSegments = currentNamespace.Split(NamespaceSeparators, StringSplitOptions.RemoveEmptyEntries); | ||
| if (typeNamespaceSegments.Length > currentNamespaceSegments.Length) | ||
|
|
@@ -399,21 +402,23 @@ internal static bool IsTagHelperFromMangledClass(TagHelperDescriptor tagHelper) | |
| { | ||
| // If this is a child content tag helper, we want to look at it's original type. | ||
| // E.g, if the type name is `Test.__generated__MyComponent.ChildContent`, we want to look at `Test.__generated__MyComponent`. | ||
| TrySplitNamespaceAndType(typeName, out typeName, out var _); | ||
| TrySplitNamespaceAndType(typeName, out var typeNameTextSpan, out var _); | ||
| typeName = GetTextSpanContent(typeNameTextSpan, typeName); | ||
| } | ||
| if (!TrySplitNamespaceAndType(typeName, out var _, out var className)) | ||
| if (!TrySplitNamespaceAndType(typeName, out var _, out var classNameTextSpan)) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor but this method could exit earlier if the original
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't want to affect the functionality of this code, as there are some subtle behaviors here. Let me know if you think it's worth pursuing. In reply to: 439606336 [](ancestors = 439606336)
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not my code. I defer to other reviewers 😺 |
||
| { | ||
| return false; | ||
| } | ||
| var className = GetTextSpanContent(classNameTextSpan, typeName); | ||
|
|
||
| return ComponentMetadata.IsMangledClass(className); | ||
| } | ||
|
|
||
| // Internal for testing. | ||
| internal static bool TrySplitNamespaceAndType(string fullTypeName, out string @namespace, out string typeName) | ||
| internal static bool TrySplitNamespaceAndType(string fullTypeName, out TextSpan @namespace, out TextSpan typeName) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice |
||
| { | ||
| @namespace = string.Empty; | ||
| typeName = string.Empty; | ||
| @namespace = default; | ||
| typeName = default; | ||
|
|
||
| if (string.IsNullOrEmpty(fullTypeName)) | ||
| { | ||
|
|
@@ -442,20 +447,26 @@ internal static bool TrySplitNamespaceAndType(string fullTypeName, out string @n | |
|
|
||
| if (splitLocation == -1) | ||
| { | ||
| typeName = fullTypeName; | ||
| typeName = new TextSpan(0, fullTypeName.Length); | ||
| return true; | ||
| } | ||
|
|
||
| @namespace = fullTypeName.Substring(0, splitLocation); | ||
| @namespace = new TextSpan(0, splitLocation); | ||
|
|
||
| var typeNameStartLocation = splitLocation + 1; | ||
| if (typeNameStartLocation < fullTypeName.Length) | ||
| { | ||
| typeName = fullTypeName.Substring(typeNameStartLocation, fullTypeName.Length - typeNameStartLocation); | ||
| typeName = new TextSpan(typeNameStartLocation, fullTypeName.Length - typeNameStartLocation); | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| // Internal for testing. | ||
| internal static string GetTextSpanContent(TextSpan textSpan, string s) | ||
| { | ||
| return s.Substring(textSpan.Start, textSpan.Length); | ||
| } | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.