txscript: Optimize trace logging. #2301
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
After recent optimizations, the current next biggest offender of more allocations than would be expected revealed by profiling is due to the trace logging closures for the scripting engine execution.
Once upon a time, there was no way to check the current logging level with the logging infrastructure at the time and thus a logging closure was used to defer the fairly expensive construction of the trace logging information until it was actually invoked (meaning tracing is enabled).
However, those closures come at the cost of allocations, and since script execution is something that happens non-stop during normal operation, those allocations really add up, as the profiling shows.
As some point, the logging infrastructure was changed out, and it is now possible to determine the logging level, so this updates the code to take advantage of that and avoid the closures while still only performing the fairly expensive construction of trace logging information when tracing is enabled. In other words, with this change there is zero cost (other than the conditional check, of course) when tracing is not enabled.
Finally, this also removes the no longer necessary code related to creating the logging closures.
Relevant portion of the before profile:
Those logging sections are zero after this PR as expected.