[BEEEP] Minor Tweaks/Optimizations
Type of change
- [ ] Bug fix
- [ ] New feature development
- [x] Tech debt (refactoring, code cleanup, dependency upgrades, etc)
- [ ] Build/deploy pipeline (DevOps)
- [ ] Other
Objective
There were some low hanging fruit.
Code changes
- ~~When possible I included a
RegexOptions.Compileoption and caches theRegexobject statically on the class. This will have a minor affect on startup but should more than make up for itself with speed everytime the regex is run.~~ - I switched a few methods to return
ValueTupleinstead ofTuple. I used the()shorthand for declaring them and added descriptive names for their fields. - Used
StringBuilderto append strings instead of+= - Configured
.editorconfigto makeAsyncsuffix optional onpublicmethods in files that end inControllerorTests.
Testing requirements
N/A
Before you submit
- [ ] I have checked for formatting errors (
dotnet tool run dotnet-format --check) (required) - [ ] If making database changes - I have also updated Entity Framework queries and/or migrations
- [x] I have added unit tests where it makes sense to do so (encouraged but not required)
- [ ] This change requires a documentation update (notify the documentation team)
- [ ] This change has particular deployment requirements (notify the DevOps team)
I'm a bit split on the StringBuilder stuff. I suspect it won't improve performance since StringBuilder has more overhead than just using a string for limited number of concatenations. However if it improves readability we should go for it.
@Hinton I was curious about the actual performance to so I ran I few benchmarks and with the max amount of concatenations we would have here (4) it does not make sense. As the amount goes up though we should think about StringBuilder I will remove it from this place.
| Method | Mean | Error | StdDev | Gen 0 | Gen 1 | Allocated |
|---|---|---|---|---|---|---|
| String_FourAppends | 55.16 ns | 0.296 ns | 0.247 ns | 0.0714 | - | 448 B |
| StringBuilder_FourAppends | 158.76 ns | 1.482 ns | 1.314 ns | 0.1135 | - | 712 B |
| String_TwentyAppends | 666.53 ns | 5.085 ns | 4.757 ns | 1.2980 | 0.0029 | 8,144 B |
| StringBuilder_TwentyAppends | 373.59 ns | 2.649 ns | 2.212 ns | 0.3481 | 0.0014 | 2,184 B |
@Hinton I was curious about the actual performance to so I ran I few benchmarks and with the max amound of concatenations we would have here (4) it does not make sense. As the amount goes up though we should think about
StringBuilderI will remove it from this place.Method Mean Error StdDev Gen 0 Gen 1 Allocated String_FourAppends 55.16 ns 0.296 ns 0.247 ns 0.0714 - 448 B StringBuilder_FourAppends 158.76 ns 1.482 ns 1.314 ns 0.1135 - 712 B String_TwentyAppends 666.53 ns 5.085 ns 4.757 ns 1.2980 0.0029 8,144 B StringBuilder_TwentyAppends 373.59 ns 2.649 ns 2.212 ns 0.3481 0.0014 2,184 B
As a rule of thumb I also had "has more than 4 strings - use stringbuilder" in mind, but wasn't sure if that is still valid with all the changes to the .net core framework
I generally try and use string builders in loops. Otherwise it feels like micro-optimizations :).