1
0
mirror of https://github.com/golang/go synced 2024-11-06 15:26:13 -07:00

runtime: do not alloc never usued tail bytes in fixalloc

Currently, the '_FixAllocChunk % fixalloc.size' tail bytes
will never be used when allocing from persistentalloc.

Wasted bytes on darwin/amd64:
  _FixAllocChunk % mheap_.spanalloc.size             = 64
  _FixAllocChunk % mheap_.cachealloc.size            = 784
  _FixAllocChunk % mheap_.specialfinalizeralloc.size = 16
  _FixAllocChunk % mheap_.specialprofilealloc.size   = 16
  _FixAllocChunk % mheap_.specialReachableAlloc.size = 16
  _FixAllocChunk % mheap_.arenaHintAlloc.size        = 16

After this commit, fixalloc alloc '_FixAllocChunk / fixalloc.size'
objects exactly with zero waste. sizeof(fixalloc) is unchanged.
This commit is contained in:
Hans 2021-07-28 18:29:45 +08:00
parent 849b791129
commit 3fe82bdf5e

View File

@ -31,6 +31,7 @@ type fixalloc struct {
list *mlink
chunk uintptr // use uintptr instead of unsafe.Pointer to avoid write barriers
nchunk uint32
nalloc uint32
inuse uintptr // in-use bytes now
stat *sysMemStat
zero bool // zero allocations
@ -56,6 +57,7 @@ func (f *fixalloc) init(size uintptr, first func(arg, p unsafe.Pointer), arg uns
f.list = nil
f.chunk = 0
f.nchunk = 0
f.nalloc = uint32(_FixAllocChunk / f.size * f.size)
f.inuse = 0
f.stat = stat
f.zero = true
@ -77,8 +79,8 @@ func (f *fixalloc) alloc() unsafe.Pointer {
return v
}
if uintptr(f.nchunk) < f.size {
f.chunk = uintptr(persistentalloc(_FixAllocChunk, 0, f.stat))
f.nchunk = _FixAllocChunk
f.chunk = uintptr(persistentalloc(uintptr(f.nalloc), 0, f.stat))
f.nchunk = f.nalloc
}
v := unsafe.Pointer(f.chunk)