msgpack icon indicating copy to clipboard operation
msgpack copied to clipboard

small optimizations.

Open OneOfOne opened this issue 2 years ago • 0 comments

Hello, here at @AlpineIQ we use msgpack extensively, however as some of our caches grew, we hit some roadblocks with memory for decoding.

I took a crack at the decoder and come up with this PR:

  • remove bytesAllocLimit/sliceAllocLimit/maxMapSize, always allocate the full size since it's known beforehand.
  • allocate types in blocks instead of one at a time.

I know it might look like a micro-optimization but it's extremely significant at the scale we use it for, it's the difference between running OO< on a 5.75TB instance and only using 1.4TB.

Our internal benchmark (Can't provide a test file, but it's about 159mb, the real files are 1-2gb):

BenchmarkOrig-32	3	23014189915 ns/op	2270733298 B/op	130404548 allocs/op
BenchmarkFork-32	3	22803649859 ns/op	2213246725 B/op	126130710 allocs/op

Benchstat of the all msgpack tests, no real changes:

# generated with go test -benchmem -bench='Map|Struct' -benchtime=10s 

name                          old time/op    new time/op    delta
MapStringString-32               472ns ± 0%     460ns ± 0%   ~     (p=1.000 n=1+1)
MapStringStringPtr-32            618ns ± 0%     618ns ± 0%   ~     (p=1.000 n=1+1)
MapStringInterfaceMsgpack-32    1.26µs ± 0%    1.14µs ± 0%   ~     (p=1.000 n=1+1)
MapStringInterfaceJSON-32       5.00µs ± 0%    4.61µs ± 0%   ~     (p=1.000 n=1+1)
MapIntInt-32                    1.14µs ± 0%    1.13µs ± 0%   ~     (p=1.000 n=1+1)
StructVmihailencoMsgpack-32     3.57µs ± 0%    2.90µs ± 0%   ~     (p=1.000 n=1+1)
StructMarshal-32                1.92µs ± 0%    1.31µs ± 0%   ~     (p=1.000 n=1+1)
StructUnmarshal-32              1.43µs ± 0%    1.39µs ± 0%   ~     (p=1.000 n=1+1)
StructManual-32                 2.37µs ± 0%    2.04µs ± 0%   ~     (p=1.000 n=1+1)
StructUnmarshalPartially-32     1.01µs ± 0%    0.96µs ± 0%   ~     (p=1.000 n=1+1)

name                          old alloc/op   new alloc/op   delta
MapStringString-32               16.0B ± 0%     16.0B ± 0%   ~     (all equal)
MapStringStringPtr-32            16.0B ± 0%     16.0B ± 0%   ~     (all equal)
MapStringInterfaceMsgpack-32      402B ± 0%      402B ± 0%   ~     (all equal)
MapStringInterfaceJSON-32         920B ± 0%      920B ± 0%   ~     (all equal)
MapIntInt-32                      192B ± 0%      192B ± 0%   ~     (all equal)
StructVmihailencoMsgpack-32     1.83kB ± 0%    1.83kB ± 0%   ~     (p=1.000 n=1+1)
StructMarshal-32                1.74kB ± 0%    1.74kB ± 0%   ~     (p=1.000 n=1+1)
StructUnmarshal-32               96.0B ± 0%     96.0B ± 0%   ~     (all equal)
StructManual-32                 1.49kB ± 0%    1.49kB ± 0%   ~     (p=1.000 n=1+1)
StructUnmarshalPartially-32      64.0B ± 0%     64.0B ± 0%   ~     (all equal)

name                          old allocs/op  new allocs/op  delta
MapStringString-32                4.00 ± 0%      4.00 ± 0%   ~     (all equal)
MapStringStringPtr-32             4.00 ± 0%      4.00 ± 0%   ~     (all equal)
MapStringInterfaceMsgpack-32      12.0 ± 0%      12.0 ± 0%   ~     (all equal)
MapStringInterfaceJSON-32         34.0 ± 0%      34.0 ± 0%   ~     (all equal)
MapIntInt-32                      9.00 ± 0%      5.00 ± 0%   ~     (p=1.000 n=1+1)
StructVmihailencoMsgpack-32       15.0 ± 0%      15.0 ± 0%   ~     (all equal)
StructMarshal-32                  7.00 ± 0%      7.00 ± 0%   ~     (all equal)
StructUnmarshal-32                8.00 ± 0%      8.00 ± 0%   ~     (all equal)
StructManual-32                   17.0 ± 0%      17.0 ± 0%   ~     (all equal)
StructUnmarshalPartially-32       2.00 ± 0%      2.00 ± 0%   ~     (all equal)

OneOfOne avatar Jan 23 '22 22:01 OneOfOne