Skip to content

Reduce allocations for logfile reader#40796

Merged
cpuguy83 merged 1 commit intomoby:masterfrom
cpuguy83:log_reads_allocs
Apr 16, 2020
Merged

Reduce allocations for logfile reader#40796
cpuguy83 merged 1 commit intomoby:masterfrom
cpuguy83:log_reads_allocs

Conversation

@cpuguy83
Copy link
Member

@cpuguy83 cpuguy83 commented Apr 8, 2020

Before this change, the log decoder function provided by the log driver
to logfile would not be able to re-use buffers, causing undeeded
allocations and memory bloat for dockerd.

This change introduces an interface that allows the log driver to manage
it's memory usge more effectively.
This only affects json-file and local log drivers.

json-file still is not great just because of how the json decoder in the
stdlib works.
local is significantly improved.

Relates to docker/for-linux#848
Fixes #39963

Before this change, the log decoder function provided by the log driver
to logfile would not be able to re-use buffers, causing undeeded
allocations and memory bloat for dockerd.

This change introduces an interface that allows the log driver to manage
it's memory usge more effectively.
This only affects json-file and local log drivers.

`json-file` still is not great just because of how the json decoder in the
stdlib works.
`local` is significantly improved.

Signed-off-by: Brian Goff <[email protected]>
@cpuguy83 cpuguy83 requested a review from kolyshkin April 8, 2020 19:29
@cpuguy83 cpuguy83 added this to the 20.03.0 milestone Apr 8, 2020
jl.Attrs = make(map[string]string)
for k := range jl.Attrs {
delete(jl.Attrs, k)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the explicit delete better than assigning an empty map? From what I understood it's the same thing.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In one case it has to allocate, in the other not.
Also this pattern for deleting the map is optimized by the compiler.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's worth noting that right now I believe map elements that are deleted won't actually be GCed until the map itself is GCed, see golang/go#20135

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so would the old approach be better? should we manually trigger a garbage collect?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Realistically, this is not going to change in size. We reset it because it's proper b/c things could change, but in reality it is not changing.

Copy link
Contributor

@SamWhited SamWhited Apr 10, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without doing some profiling I'm really not sure. How often are these reset vs. created/destroyed? Maybe the higher memory footprint is worth it's rather small and we avoid an expensive allocation, or maybe we'd rather take one more allocation to avoid the higher memory footprint if its significant.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not think there is any higher memory footprint because these attrs actually never change.

@thaJeztah
Copy link
Member

@kolyshkin looks like your questions were answered; anything left?

@SamWhited PTAL

Copy link
Contributor

@SamWhited SamWhited left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Member

@thaJeztah thaJeztah left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll copy @SamWhited's LGTM

@cpuguy83 cpuguy83 merged commit 2200d93 into moby:master Apr 16, 2020
@cpuguy83 cpuguy83 deleted the log_reads_allocs branch April 16, 2020 19:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Out of memory crash while following log with "local" logging driver.

5 participants