azure-sdk-for-net icon indicating copy to clipboard operation
azure-sdk-for-net copied to clipboard

Reduce memory allocation in HttpMessageSanitizer

Open pharring opened this issue 1 year ago • 3 comments
trafficstars

This is a performance optimization in HttpMessageSanitizer to reduce the memory allocated in common cases.

There are three optimization techniques:

  1. Use url.AsSpan(...) instead of url.Substring(...) to create the query part of the URL.
  2. Defer creation of the StringBuilder until/unless absolutely necessary (when there's a redacted query value)
  3. Use a cached StringBuilder.

I added benchmarks to show the difference:

Baseline

Method Mean Error StdDev Gen0 Allocated
SanitizeHeader 3.890 ns 0.3120 ns 0.3468 ns - -
SanitizeUrl 8.218 ns 0.7272 ns 0.8375 ns - -
SanitizeUrlWithQueryNoValue 110.411 ns 4.3084 ns 4.7888 ns 0.0004 264 B
SanitizeUrlWithAllowedQuery 137.150 ns 7.0172 ns 7.7996 ns 0.0005 360 B
SanitizeUrlWithDisallowedQuery 185.949 ns 7.9072 ns 9.1060 ns 0.0007 464 B

Candidate

Method Mean Error StdDev Allocated
SanitizeHeader 4.148 ns 0.5924 ns 0.6822 ns -
SanitizeUrl 7.978 ns 0.9843 ns 1.1336 ns -
SanitizeUrlWithQueryNoValue 21.698 ns 1.9727 ns 2.2717 ns -
SanitizeUrlWithAllowedQuery 27.414 ns 2.7250 ns 3.1381 ns -
SanitizeUrlWithDisallowedQuery 84.632 ns 5.9402 ns 6.8407 ns 104 B

As you can see, we avoid memory allocations (and potential garbage collections) in common cases where there are no query parameters or where all the query parameters are allowed. In the case where we have to redact query values, the bytes allocated is one fourth of the baseline.

pharring avatar May 02 '24 04:05 pharring

Thank you for your contribution @pharring! We will review the pull request and get back to you soon.

github-actions[bot] avatar May 02 '24 04:05 github-actions[bot]

API change check

API changes are not detected in this pull request.

azure-sdk avatar May 02 '24 05:05 azure-sdk

//cc: @JoshLove-msft, in case there's any overlap with the test proxy sanitization.

jsquire avatar May 03 '24 14:05 jsquire

Any further comments? If not, can I get a review and sign-off?

pharring avatar May 06 '24 23:05 pharring

@pharring : Give a mention when you're comfortable with things and I'll merge on your behalf.

jsquire avatar May 09 '24 15:05 jsquire

@jsquire #sign-off

pharring avatar May 09 '24 18:05 pharring

//cc: @JoshLove-msft, in case there's any overlap with the test proxy sanitization.

No this shouldn't impact anything with test recordings as logs are not part of the request/responses.

JoshLove-msft avatar May 09 '24 18:05 JoshLove-msft