⚡ Bolt: TuiColor Parsing Optimization#299
Conversation
Co-authored-by: tedd <493224+tedd@users.noreply.github.com>
|
👋 Jules, reporting for duty! I'm here to lend a hand with this pull request. When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down. I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job! For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
There was a problem hiding this comment.
Pull request overview
Optimizes CSS-style color parsing in TuiColor.FromHex by switching functional parsing to span-based slicing/splitting to reduce allocations, and adds a benchmark + legacy baseline implementation to quantify the improvement.
Changes:
- Refactor
TuiColor.FromHex/ParseFunctionalto useReadOnlySpan<char>andSpan<Range>-based splitting to minimize allocations. - Add
TuiColorParseBenchmarkandTuiColorLegacybaseline implementation for benchmarking comparisons. - Minor test/demo formatting tweaks and an entry in
.jules/bolt.md.
Reviewed changes
Copilot reviewed 12 out of 13 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| src/Tedd.TUI/TuiColor.cs | Span-based parsing refactor for FromHex/ParseFunctional and related parsing helpers. |
| src/Tedd.TUI.Archive/TuiColorLegacy.cs | Adds legacy parser implementation used as a benchmark baseline. |
| src/Tedd.TUI.Benchmarks/TuiColorParseBenchmark.cs | Adds benchmark comparing legacy vs optimized parsing. |
| src/Tedd.TUI.Tests/ValidatorLayoutMatrixTests.cs | Formatting-only change in lambda block style. |
| src/Tedd.TUI.Tests/ValidatorGroupBoxTests.cs | Formatting-only change in lambda block style. |
| src/Tedd.TUI.Tests/MarkdownViewTests.cs | Uses Assert.Single instead of Assert.Equal(1, ...). |
| src/Tedd.TUI.Tests/ItemsControlTests.cs | Formatting-only changes to collection expressions/spacing. |
| src/Tedd.TUI.Demo/DemoController.cs | Formatting-only change to event handler lambda block style. |
| .jules/bolt.md | Records the optimization work in the Jules/Bolt log. |
| src/Tedd.TUI/TuiColor.cs.rej | Should not be committed (patch reject artifact). |
| src/Tedd.TUI/TuiColor.cs.orig | Should not be committed (backup copy artifact). |
| src/Tedd.TUI.Benchmarks/TuiColorParseBenchmark.cs.orig | Should not be committed (backup copy artifact). |
| src/Tedd.TUI.Benchmarks/Tedd.TUI.Benchmarks.csproj.orig | Should not be committed (backup copy artifact). |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| --- src/Tedd.TUI/TuiColor.cs | ||
| +++ src/Tedd.TUI/TuiColor.cs | ||
| @@ -128,7 +128,7 @@ |
| using System; | ||
| using System.Runtime.CompilerServices; | ||
|
|
||
| namespace Tedd.TUI; | ||
|
|
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFramework>net10.0</TargetFramework> |
| using BenchmarkDotNet.Attributes; | ||
| using BenchmarkDotNet.Jobs; | ||
| using Tedd.TUI; | ||
|
|
||
| namespace Tedd.TUI.Benchmarks; | ||
|
|
||
| [MemoryDiagnoser] | ||
| [SimpleJob(RuntimeMoniker.Net100)] | ||
| public class TuiColorParseBenchmark | ||
| { |
| using BenchmarkDotNet.Attributes; | ||
| using BenchmarkDotNet.Jobs; | ||
| using Tedd.TUI; |
💡 Hypothesis: The mechanical inefficiency identified in
TuiColor.FromHexCSS color parsing is allocating dynamic strings continuously during comma parsing,Trim(),Substring(), and splitting comma-delimited parts into arrays.🎯 Execution: Substituted
string.Splitand intermediate formatting instances withReadOnlySpan<char>.Splitalongside stack-allocated rangesSpan<Range> ranges = stackalloc Range[5]. Thestring textparameter allocation points were refactored intoReadOnlySpan<char> textto eliminate redundant memory creation and slicing during the structural parsing.📊 Empirical Impact:
🔬 Verification Protocol:
Run
dotnet run -c Release --project src/Tedd.TUI.Benchmarks/Tedd.TUI.Benchmarks.csproj --filter '*TuiColorParseBenchmark*'or visually inspect the empirical impact.Memory allocation dropping from
599 Bto practically zero (7 Bartifacts), while the speed executes nearly 25% faster (0.77ratio), confirms a statistically significant layout scaling win.PR created automatically by Jules for task 13800298386988768688 started by @tedd