From 7cdad06c0264b0006efd3865efbecfd960ddd65c Mon Sep 17 00:00:00 2001 From: rameel Date: Thu, 24 Apr 2025 00:42:49 +0500 Subject: [PATCH 1/4] Elide redundant bounds check --- src/Ramstack.FileSystem.Abstractions/VirtualPath.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Ramstack.FileSystem.Abstractions/VirtualPath.cs b/src/Ramstack.FileSystem.Abstractions/VirtualPath.cs index 886d652..a89a5b3 100644 --- a/src/Ramstack.FileSystem.Abstractions/VirtualPath.cs +++ b/src/Ramstack.FileSystem.Abstractions/VirtualPath.cs @@ -168,7 +168,7 @@ public static bool IsNormalized(ReadOnlySpan path) { var prior = path[0]; - for (var j = 1; j < path.Length; j++) + for (var j = 1; (uint)j < (uint)path.Length; j++) { var ch = path[j]; if (ch == '\\' || ch == '/' && prior == '/') From 4447093d79ef33335587fbc6831d4d5b518cb1d9 Mon Sep 17 00:00:00 2001 From: rameel Date: Thu, 24 Apr 2025 00:44:10 +0500 Subject: [PATCH 2/4] Simplify VirtualPath.IsNormalized with pattern matching for cleaner and more readable code --- src/Ramstack.FileSystem.Abstractions/VirtualPath.cs | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/Ramstack.FileSystem.Abstractions/VirtualPath.cs b/src/Ramstack.FileSystem.Abstractions/VirtualPath.cs index a89a5b3..15b6fd6 100644 --- a/src/Ramstack.FileSystem.Abstractions/VirtualPath.cs +++ b/src/Ramstack.FileSystem.Abstractions/VirtualPath.cs @@ -176,20 +176,18 @@ public static bool IsNormalized(ReadOnlySpan path) if (ch == '.' && prior == '/') { - if ((uint)j + 1 >= path.Length) + if ((uint)j + 1 >= (uint)path.Length) return false; - var nch = path[j + 1]; - if (nch == '/' || nch == '\\') + if (path[j + 1] is '/' or '\\') return false; - if (nch == '.') + if (path[j + 1] == '.') { - if ((uint)j + 2 >= path.Length) + if ((uint)j + 2 >= (uint)path.Length) return false; - var sch = path[j + 2]; - if (sch == '/' || sch == '\\') + if (path[j + 2] is '/' or '\\') return false; } } From a8b3efd0980ee297aa031fa0a5f190851504480c Mon Sep 17 00:00:00 2001 From: rameel Date: Thu, 24 Apr 2025 00:44:58 +0500 Subject: [PATCH 3/4] Clean up and formatting --- .../Utilities/PathTokenizer.cs | 7 +++++-- src/Ramstack.FileSystem.Abstractions/VirtualPath.cs | 10 ---------- src/Ramstack.FileSystem.Globbing/GlobbingFileSystem.cs | 4 ++-- 3 files changed, 7 insertions(+), 14 deletions(-) diff --git a/src/Ramstack.FileSystem.Abstractions/Utilities/PathTokenizer.cs b/src/Ramstack.FileSystem.Abstractions/Utilities/PathTokenizer.cs index 4b5f608..b53bb50 100644 --- a/src/Ramstack.FileSystem.Abstractions/Utilities/PathTokenizer.cs +++ b/src/Ramstack.FileSystem.Abstractions/Utilities/PathTokenizer.cs @@ -49,7 +49,8 @@ internal Enumerator(string path) => /// /// Gets the current path component. /// - public ReadOnlySpan Current => + public ReadOnlySpan Current + { // // Using AsSpan(_start) followed by slicing is more efficient // than AsSpan(_start, _count) because: @@ -60,7 +61,9 @@ internal Enumerator(string path) => // which the JIT can't optimize away: // (ulong)(uint)_start + (ulong)(uint)_count <= (ulong)(uint)Length // - _path.AsSpan(_start)[.._count]; + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get => _path.AsSpan(_start)[.._count]; + } /// /// Advances the enumerator to the next path component. diff --git a/src/Ramstack.FileSystem.Abstractions/VirtualPath.cs b/src/Ramstack.FileSystem.Abstractions/VirtualPath.cs index 15b6fd6..9535ccb 100644 --- a/src/Ramstack.FileSystem.Abstractions/VirtualPath.cs +++ b/src/Ramstack.FileSystem.Abstractions/VirtualPath.cs @@ -1,6 +1,4 @@ using System.Buffers; -using System.Diagnostics.CodeAnalysis; -using System.Runtime.CompilerServices; using Ramstack.FileSystem.Utilities; @@ -317,7 +315,6 @@ public static string Join(ReadOnlySpan path1, ReadOnlySpan path2) /// if the path has a leading directory separator; /// otherwise, . /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool HasLeadingSlash(string path) => path.StartsWith('/') || path.StartsWith('\\'); @@ -329,7 +326,6 @@ public static bool HasLeadingSlash(string path) => /// if the path has a trailing directory separator; /// otherwise, . /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool HasTrailingSlash(string path) => path.EndsWith('/') || path.EndsWith('\\'); @@ -341,7 +337,6 @@ public static bool HasTrailingSlash(string path) => /// if the path has a leading directory separator; /// otherwise, . /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool HasLeadingSlash(ReadOnlySpan path) { if (path.Length != 0) @@ -362,7 +357,6 @@ public static bool HasLeadingSlash(ReadOnlySpan path) /// if the path has a trailing directory separator; /// otherwise, . /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool HasTrailingSlash(ReadOnlySpan path) { if (path.Length != 0) @@ -390,8 +384,4 @@ private static int GetDirectoryNameOffset(ReadOnlySpan path) return index; } - - [DoesNotReturn] - private static void Error_InvalidPath() => - throw new ArgumentException("Invalid path"); } diff --git a/src/Ramstack.FileSystem.Globbing/GlobbingFileSystem.cs b/src/Ramstack.FileSystem.Globbing/GlobbingFileSystem.cs index 1ea19f2..7fb42bf 100644 --- a/src/Ramstack.FileSystem.Globbing/GlobbingFileSystem.cs +++ b/src/Ramstack.FileSystem.Globbing/GlobbingFileSystem.cs @@ -55,8 +55,8 @@ public GlobbingFileSystem(IVirtualFileSystem fileSystem, string[] patterns, stri ArgumentNullException.ThrowIfNull(patterns); _fs = fileSystem; - _patterns = patterns.ToArray(); - _excludes = excludes?.ToArray() ?? []; + _patterns = [..patterns]; + _excludes = [..excludes ?? []]; } /// From a2016bc9387d3b3a6663c29d2e7f10ce6ff531d7 Mon Sep 17 00:00:00 2001 From: rameel Date: Thu, 24 Apr 2025 00:45:50 +0500 Subject: [PATCH 4/4] Remove unused Microsoft.Extensions.FileProviders.Composite --- Directory.Packages.props | 1 - .../Ramstack.FileSystem.Adapters.csproj | 1 - 2 files changed, 2 deletions(-) diff --git a/Directory.Packages.props b/Directory.Packages.props index bc80d5e..44fe4ea 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -8,7 +8,6 @@ - diff --git a/src/Ramstack.FileSystem.Adapters/Ramstack.FileSystem.Adapters.csproj b/src/Ramstack.FileSystem.Adapters/Ramstack.FileSystem.Adapters.csproj index fa1d60f..82f0333 100644 --- a/src/Ramstack.FileSystem.Adapters/Ramstack.FileSystem.Adapters.csproj +++ b/src/Ramstack.FileSystem.Adapters/Ramstack.FileSystem.Adapters.csproj @@ -42,7 +42,6 @@ - all runtime; build; native; contentfiles; analyzers; buildtransitive