server icon indicating copy to clipboard operation
server copied to clipboard

[BEEEP] Minor Tweaks/Optimizations

Open justindbaur opened this issue 4 years ago • 4 comments

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.Compile option and caches the Regex object 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 ValueTuple instead of Tuple. I used the () shorthand for declaring them and added descriptive names for their fields.
  • Used StringBuilder to append strings instead of +=
  • Configured .editorconfig to make Async suffix optional on public methods in files that end in Controller or Tests.

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)

justindbaur avatar Jan 18 '22 17:01 justindbaur

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 avatar Jan 18 '22 17:01 Hinton

@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

justindbaur avatar Jan 18 '22 18:01 justindbaur

@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 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

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

djsmith85 avatar Jan 18 '22 18:01 djsmith85

I generally try and use string builders in loops. Otherwise it feels like micro-optimizations :).

Hinton avatar Jan 18 '22 18:01 Hinton