For mocks on functions that take complex types as arg, Sprintf-ing the actual value can be costly. Switch successful matches to use thunks to lazily render their output, and fully avoid rendering their output when there are no differences since we return a constant string in that case anyways
Changes
Switch to a string builder for accumulating output for better performance even when we do render
Motivation
We heavily use mocks at DevotedHealth, and use them frequently in gRPC contexts where we're mocking RPCs that take <Whatever>Request structs that are costly to %v via Sprintf. Sprintf showed up as a tangible contributor to runtime performance when profiling some of our heavier tests. The below ~5s cpu time spent on Sprintf goes away completely here in one of our exemplar tests
Summary
Sprintf
-ing theactual
value can be costly. Switch successful matches to use thunks to lazily render their output, and fully avoid rendering their output when there are no differences since we return a constant string in that case anywaysChanges
Motivation
<Whatever>Request
structs that are costly to%v
viaSprintf
.Sprintf
showed up as a tangible contributor to runtime performance when profiling some of our heavier tests. The below ~5s cpu time spent onSprintf
goes away completely here in one of our exemplar testsRelated issues