We have to make sure that logged messages don’t contain non-printable characters. Until now we’ve sanitized these messages by recreating them every time, even though we expect most lines to be already normalized. This PR splits out the happy path by identifying the suspicious characters, and if none were found, use the original string, otherwise reconstruct the new string with escaped characters. This makes the most common path simpler to reason about.
For normal messages this results in a ~2x speedup, but the goal of the change is rather making sure that the case we encounter most often (printable values that don’t need sanitization) aren’t recreated every time (i.e. they’re simpler to reason about).
build/src/bench/bench_bitcoin -filter=‘LogEscapeMessage.+’ –min-time=10000
Before:
ns/op | op/s | err% | total | benchmark |
---|---|---|---|---|
216.10 | 4,627,429.03 | 0.2% | 10.81 | LogEscapeMessageNormal |
328.77 | 3,041,669.54 | 0.7% | 10.97 | LogEscapeMessageSuspicious |
After
ns/op | op/s | err% | total | benchmark |
---|---|---|---|---|
106.54 | 9,386,391.31 | 0.2% | 11.01 | LogEscapeMessageNormal |
335.58 | 2,979,947.73 | 0.3% | 10.96 | LogEscapeMessageSuspicious |
Note that this part of the code is called even with -printtoconsole=0
.