Skip to content

intlogger/With: reduce memory allocation #150

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

ggambetti
Copy link
Member

@ggambetti ggambetti commented Mar 27, 2025

For repeated calls to Logger.With(...) applications might set the same set of keys multiple times, in those circumstances the size of the result map is the most correct way to determine the true number of resulting key-value pairs for the logger.

This ever so slightly reduces memory usage in the case that multiple With calls set the same keys, by reducing over-allocation.

Example:

  sl := logger.With("a", 1, "b", 2, "c", 3)
  // later
  sl.With("a", 1, "b", 2, "c", 3) // new code leaks ever so slightly less.

For reviewers: I kept (what appeared to be) the allocation strategy around MissingKey and extra, so callers to With will still perform a reallocation if they pass an odd number of parameters. They're not allocated for in the previous code (len(l.implied)+len(args), ), and aren't allocated for in the new code (2*len(result)).

For repeated calls to Logger.With(...) applications might set the
same set of keys multiple times, in those circumstances the size of
the result map is the most correct way to determine the true number
of resulting key-value pairs for the logger.
@ggambetti ggambetti requested a review from a team as a code owner March 27, 2025 14:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant