Begin v2 of the API, fixes memory usage #139
Draft
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.
While hunting down some memory usage issues elsewhere, I saw that, much to my chagrin, hclog was suffering from the exact memory issue issue that
log/slog
was designed to avoid: memory usage when the log is ignored.As Jonathan Amsterdam points out extremely well in his presentations of slog, Go is unable to elide a ... generated slice when calling an interface function. This makes completely sense, given it has to assume it could dispatch to any number of targets.
Following in the same pattern used in slog, this PR begins a minor (but breaking) API change to correct the issue in hclog. By making Logger a struct value (that can be used as a pointer or not, it's no matter) and performing level checking before dispatching to the interface, the memory usage is elided correctly.
The benchmark included shows the memory fix as it now reports:
Where as previously, it reports:
There is more work to be done if we approve of this going forward, for instance, using the same technique slog uses to clean up the line calculation.
Here is Jonathan talking all about the issue that this v2 API would fix: https://youtu.be/8rnI2xLrdeM?t=1491