refactor: don't fill empty metadata slots#32
Merged
mosajjal merged 1 commit intogopacket:masterfrom Nov 25, 2023
Merged
Conversation
Some metadata arrays are pretty big (e.g. `EthernetTypeMetadata` has 65536 slots), while only a small subset of the values are filled with actual decoders (e.g. for `EthernetTypeMetadata` that is 18 entries). The rest were filled in `func init()` to return error on access. This leads to noticeable startup time delay, as these mappings are pretty big. This change simply leaves not used slots unset (zero value), and the request error values are generated on access to the slot. Comparing time with `GODEBUG=inittrace=1` on a simple test program which imports `gopacket/layers`: before: ``` init internal/bytealg @0 ms, 0 ms clock, 0 bytes, 0 allocs init runtime @0.009 ms, 0.063 ms clock, 0 bytes, 0 allocs init math @0.18 ms, 0 ms clock, 0 bytes, 0 allocs init errors @0.19 ms, 0 ms clock, 0 bytes, 0 allocs init sync @0.20 ms, 0.002 ms clock, 16 bytes, 1 allocs init internal/godebug @0.21 ms, 0.020 ms clock, 816 bytes, 20 allocs init internal/intern @0.23 ms, 0.002 ms clock, 408 bytes, 8 allocs init hash/crc32 @0.24 ms, 0.008 ms clock, 1024 bytes, 1 allocs init net/netip @0.26 ms, 0 ms clock, 48 bytes, 2 allocs init syscall @0.26 ms, 0.003 ms clock, 640 bytes, 3 allocs init time @0.27 ms, 0.002 ms clock, 0 bytes, 0 allocs init context @0.28 ms, 0 ms clock, 96 bytes, 1 allocs init io/fs @0.29 ms, 0 ms clock, 0 bytes, 0 allocs init os @0.30 ms, 0.006 ms clock, 328 bytes, 7 allocs init unicode @0.31 ms, 0 ms clock, 512 bytes, 4 allocs init reflect @0.32 ms, 0 ms clock, 0 bytes, 0 allocs init vendor/golang.org/x/net/dns/dnsmessage @0.32 ms, 0.002 ms clock, 624 bytes, 6 allocs init net @0.33 ms, 0.002 ms clock, 688 bytes, 13 allocs init github.com/gopacket/gopacket @0.34 ms, 0.003 ms clock, 720 bytes, 5 allocs init github.com/gopacket/gopacket/layers @0.35 ms, 1.3 ms clock, 25136 bytes, 63 allocs ``` after: ``` init internal/bytealg @0 ms, 0 ms clock, 0 bytes, 0 allocs init runtime @0.011 ms, 0.070 ms clock, 0 bytes, 0 allocs init math @0.26 ms, 0 ms clock, 0 bytes, 0 allocs init errors @0.27 ms, 0 ms clock, 0 bytes, 0 allocs init sync @0.27 ms, 0.003 ms clock, 16 bytes, 1 allocs init internal/godebug @0.28 ms, 0.023 ms clock, 816 bytes, 20 allocs init internal/intern @0.31 ms, 0.002 ms clock, 408 bytes, 8 allocs init hash/crc32 @0.32 ms, 0.008 ms clock, 1024 bytes, 1 allocs init net/netip @0.33 ms, 0 ms clock, 48 bytes, 2 allocs init syscall @0.34 ms, 0.003 ms clock, 640 bytes, 3 allocs init time @0.35 ms, 0.003 ms clock, 0 bytes, 0 allocs init context @0.37 ms, 0 ms clock, 96 bytes, 1 allocs init io/fs @0.38 ms, 0 ms clock, 0 bytes, 0 allocs init os @0.38 ms, 0.009 ms clock, 328 bytes, 7 allocs init unicode @0.40 ms, 0 ms clock, 512 bytes, 4 allocs init reflect @0.41 ms, 0 ms clock, 0 bytes, 0 allocs init vendor/golang.org/x/net/dns/dnsmessage @0.42 ms, 0.002 ms clock, 624 bytes, 6 allocs init net @0.43 ms, 0.004 ms clock, 688 bytes, 13 allocs init github.com/gopacket/gopacket @0.44 ms, 0.003 ms clock, 720 bytes, 5 allocs init github.com/gopacket/gopacket/layers @0.46 ms, 0.086 ms clock, 25872 bytes, 66 allocs ``` Signed-off-by: Andrey Smirnov <andrey.smirnov@siderolabs.com>
6147fa5 to
6035c7b
Compare
Contributor
|
Hi, how much optimization are we talking about here? in terms of allocation, doesn't look different at all. and for startup time, we're talking about 0.1ms? |
Contributor
Author
|
output is confusing, before is after: So -1.2ms. In other bigger projects this initialization is the worst part of all 100+ libraries imported, and measures at 1-3ms. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Some metadata arrays are pretty big (e.g.
EthernetTypeMetadatahas 65536 slots), while only a small subset of the values are filled with actual decoders (e.g. forEthernetTypeMetadatathat is 18 entries).The rest were filled in
func init()to return error on access. This leads to noticeable startup time delay, as these mappings are pretty big.This change simply leaves not used slots unset (zero value), and the request error values are generated on access to the slot.
Comparing time with
GODEBUG=inittrace=1on a simple test program which importsgopacket/layers:before:
after: