perf: use ValueStringBuilder to avoid allocations#4231
perf: use ValueStringBuilder to avoid allocations#4231thomhurst merged 2 commits intothomhurst:mainfrom
ValueStringBuilder to avoid allocations#4231Conversation
e89aa27 to
d79a9a7
Compare
d79a9a7 to
f67a0e1
Compare
f67a0e1 to
d0e9c4d
Compare
|
This seems to have broken execution for a bunch of tests. I think using a custom collection is quite risky too. Maybe we should leave this one. |
What are the errors? I wouldn't be surprised if I messed up the logic without any tests: 😊
Fair enough, I had a lot of plans to use these but I can adapt. For what it's worth the collections are both internally used by Microsoft in the .Net runtime and IMO are easier to use than |
|
How come they don't add it into the core library? I haven't investigated properly, but it's expected things like 84 tests in a run and only getting 13. Click on the Ubuntu pipeline failures on the pr checks |
d0e9c4d to
1fd2ced
Compare
|
The failed test are mostly
There is a proposal for this but it's blocked until they add a safety tool. They are worried that people will accidentally misuse it by; copying the |
1fd2ced to
e6e20d8
Compare
SummaryThis PR optimizes TestIdentifierService to reduce allocations using ValueStringBuilder and ValueListBuilder, improving performance. Critical Issues1. Code Duplication: ValueStringBuilder already exists The PR adds ValueStringBuilder to TUnit.Engine/Helpers/, but this type already exists at TUnit.Core/Helpers/ValueStringBuilder.cs (line 9). Since TUnit.Engine.csproj already references TUnit.Core (line 34 of TUnit.Engine.csproj), you can import and use the existing type instead of duplicating the code. Recommendation:
2. Potential Bug in WriteTypeNameWithGenerics In WriteTypeNameWithGenerics (TestIdentifierService.cs:87), there is a ValueListBuilder initialized with a stack-allocated array of 4 nulls. This may cause the builder to start with Length=4 instead of Length=0, potentially adding null strings to the output when iterating backwards at line 139. Suggestions
VerdictREQUEST CHANGES - Critical code duplication and potential bug in ValueListBuilder initialization |
|
Should have fixed the issue, on line 138 I looped the list in reverse but didn't update line 140 to reflect this. Note that it might be possible to remove the allocation on line 124 by changing Edit: I think there is a second bug 😭 |
e6e20d8 to
4528f50
Compare
02be577 to
b86a8bd
Compare
b86a8bd to
32ad665
Compare
Uses
ValueListBuilderandValueStringBuilderto drastically reduce allocations inTestIdentifierServiceValueListBuilderhas a lot of potential in other areas ofTUnit; I see a lot of temporary Lists in use.typeSbcould be aVLSbut it's used so infrequently I don't see any point - Happy to change this.VLSas it may see some use if someone uses generic tests.stringallocations andArrayPoolusageBef ore
After