-
-
Notifications
You must be signed in to change notification settings - Fork 0
β‘ Bolt: [Performance optimization] WslPathResolver allocation reduction #144
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
base: master
Are you sure you want to change the base?
Changes from all commits
0ce5189
598759b
c1e8a80
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 |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| ## 2024-05-24 - WslPathResolver string splitting | ||
| **Learning:** `WslPathResolver.TryParseUncRemainder` used `string.Split()`, `string.Join()`, and `Enumerable.Skip()` which leads to unnecessary GC pressure and multiple string/array allocations for hot path code. | ||
| **Action:** Replace `string.Split` with `ReadOnlySpan<char>.Split` and use `StringBuilder` to append portions sequentially when parsing and reconstructing path parts. This saves multiple array/string allocations and makes parsing faster. |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -142,16 +142,39 @@ | |||||
| private static bool TryParseUncRemainder(string remainder, string fullUnc, out WslLocation location) | ||||||
| { | ||||||
| location = null!; | ||||||
| var parts = remainder.Split('\\', StringSplitOptions.RemoveEmptyEntries); | ||||||
| if (parts.Length < 2) | ||||||
| // Bolt: Performance optimization - avoid string.Split(), Linq Skip(), and string.Join() allocations. | ||||||
| var span = remainder.AsSpan(); | ||||||
| string? distro = null; | ||||||
| var linuxPathBuilder = new System.Text.StringBuilder(); | ||||||
|
Comment on lines
+146
to
+148
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.
Suggested change
Prompt To Fix With AIThis is a comment left during a code review.
Path: QuickShell.Core/Services/WslPathResolver.cs
Line: 148
Comment:
The `StringBuilder` is created unconditionally on every call, even for early-return cases (e.g. a path with only a distro name and no segments). Initialising it with a capacity derived from `remainder.Length` would also avoid internal reallocations for longer paths, and using the `using` alias `using System.Text;` at the top of the file is cleaner than the fully-qualified name inline.
```suggestion
var linuxPathBuilder = new System.Text.StringBuilder(remainder.Length);
```
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time! |
||||||
|
|
||||||
| foreach (var range in span.Split('\\')) | ||||||
Check warningCode scanning / CodeQL Useless assignment to local variable Warning
This assignment to
range Error loading related location Loading |
||||||
|
|
||||||
| { | ||||||
| var part = span[range]; | ||||||
| if (part.IsEmpty) | ||||||
| { | ||||||
| continue; | ||||||
| } | ||||||
|
|
||||||
| if (distro is null) | ||||||
| { | ||||||
| distro = part.ToString(); | ||||||
| } | ||||||
| else | ||||||
| { | ||||||
| linuxPathBuilder.Append('/'); | ||||||
| linuxPathBuilder.Append(part); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| if (linuxPathBuilder.Length == 0 || distro is null) | ||||||
| { | ||||||
| return false; | ||||||
| } | ||||||
|
|
||||||
| location = new WslLocation | ||||||
| { | ||||||
| Distro = parts[0], | ||||||
| LinuxPath = "/" + string.Join('/', parts.Skip(1)), | ||||||
| Distro = distro, | ||||||
| LinuxPath = linuxPathBuilder.ToString(), | ||||||
|
Comment on lines
+150
to
+177
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. π Maintainability & Code Quality | π΅ Trivial | β‘ Quick win Add direct tests for UNC remainder parsing.
π§° Toolsπͺ GitHub Check: CodeQL[warning] 150-150: Useless assignment to local variable π€ Prompt for AI AgentsSource: MCP tools |
||||||
| UncPath = fullUnc, | ||||||
| }; | ||||||
|
|
||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
π Performance & Scalability | π΅ Trivial
Make the performance claim reproducible.
The PR reports allocation and runtime reductions, but the repository does not contain a dedicated benchmark for the old and new implementations. Add a checked-in benchmark that measures allocations, Gen0 collections, and runtime for both paths. Treat the reported measurements as unverified until the benchmark produces them.
π€ Prompt for AI Agents
Source: MCP tools