MCPcopy
hub / github.com/syncthing/syncthing / Get

Method Get

lib/protocol/bufferpool.go:33–59  ·  view source on GitHub ↗
(size int)

Source from the content-addressed store, hash-verified

31}
32
33func (p *bufferPool) Get(size int) []byte {
34 // Too big, isn't pooled
35 if size > MaxBlockSize {
36 p.skips.Add(1)
37 return make([]byte, size)
38 }
39
40 // Try the fitting and all bigger pools
41 bkt := getBucketForLen(size)
42 for j := bkt; j < len(BlockSizes); j++ {
43 if intf := p.pools[j].Get(); intf != nil {
44 p.hits[j].Add(1)
45 bs := *intf.(*[]byte)
46 return bs[:size]
47 }
48 }
49
50 p.misses.Add(1)
51
52 // All pools are empty, must allocate. For very small slices where we
53 // didn't have a block to reuse, just allocate a small slice instead of
54 // a large one. We won't be able to reuse it, but avoid some overhead.
55 if size < MinBlockSize/64 {
56 return make([]byte, size)
57 }
58 return make([]byte, BlockSizes[bkt])[:size]
59}
60
61// Put makes the given byte slice available again in the global pool.
62// You must only Put() slices that were returned by Get().

Callers

nothing calls this directly

Calls 3

getBucketForLenFunction · 0.85
GetMethod · 0.65
AddMethod · 0.45

Tested by

no test coverage detected