首页 > 其他分享 >Go数组的扩容规则

Go数组的扩容规则

时间:2024-03-24 15:46:26浏览次数:23  
标签:扩容 slice old uintptr cap 数组 et Go newcap

Go数组的扩容规则

Go数组的扩容规则

技术要点

先是双倍扩容, 然后是一定的比例扩容, 逐渐向1.25进行靠近

在目前的实现里面, 在小于256的时候会进行double, 在大于256的时候, 会根据一定的生长因子进行扩容, 但是总体来说还是会逐渐的靠近到1.25

func growslice(et *_type, old slice, cap int) slice {
  if raceenabled {
    callerpc := getcallerpc()
    racereadrangepc(old.array, uintptr(old.len*int(et.size)), callerpc, abi.FuncPCABIInternal(growslice))
  }
  if msanenabled {
    msanread(old.array, uintptr(old.len*int(et.size)))
  }

  if cap < old.cap {
    panic(errorString("growslice: cap out of range"))
  }

  if et.size == 0 {
    // append should not create a slice with nil pointer but non-zero len.
    // We assume that append doesn't need to preserve old.array in this case.
    return slice{unsafe.Pointer(&zerobase), old.len, cap}
  }

  newcap := old.cap
  doublecap := newcap + newcap
  if cap > doublecap {
    newcap = cap
  } else {
    const threshold = 256
    if old.cap < threshold {
      newcap = doublecap
    } else {
      // Check 0 < newcap to detect overflow
      // and prevent an infinite loop.
      for 0 < newcap && newcap < cap {
        // Transition from growing 2x for small slices
        // to growing 1.25x for large slices. This formula
        // gives a smooth-ish transition between the two.
        newcap += (newcap + 3*threshold) / 4
      }
      // Set newcap to the requested cap when
      // the newcap calculation overflowed.
      if newcap <= 0 {
        newcap = cap
      }
    }
  }

  var overflow bool
  var lenmem, newlenmem, capmem uintptr
  // Specialize for common values of et.size.
  // For 1 we don't need any division/multiplication.
  // For sys.PtrSize, compiler will optimize division/multiplication into a shift by a constant.
  // For powers of 2, use a variable shift.
  switch {
  case et.size == 1:
    lenmem = uintptr(old.len)
    newlenmem = uintptr(cap)
    capmem = roundupsize(uintptr(newcap))
    overflow = uintptr(newcap) > maxAlloc
    newcap = int(capmem)
  case et.size == goarch.PtrSize:
    lenmem = uintptr(old.len) * goarch.PtrSize
    newlenmem = uintptr(cap) * goarch.PtrSize
    capmem = roundupsize(uintptr(newcap) * goarch.PtrSize)
    overflow = uintptr(newcap) > maxAlloc/goarch.PtrSize
    newcap = int(capmem / goarch.PtrSize)
  case isPowerOfTwo(et.size):
    var shift uintptr
    if goarch.PtrSize == 8 {
      // Mask shift for better code generation.
      shift = uintptr(sys.Ctz64(uint64(et.size))) & 63
    } else {
      shift = uintptr(sys.Ctz32(uint32(et.size))) & 31
    }
    lenmem = uintptr(old.len) << shift
    newlenmem = uintptr(cap) << shift
    capmem = roundupsize(uintptr(newcap) << shift)
    overflow = uintptr(newcap) > (maxAlloc >> shift)
    newcap = int(capmem >> shift)
  default:
    lenmem = uintptr(old.len) * et.size
    newlenmem = uintptr(cap) * et.size
    capmem, overflow = math.MulUintptr(et.size, uintptr(newcap))
    capmem = roundupsize(capmem)
    newcap = int(capmem / et.size)
  }

  // The check of overflow in addition to capmem > maxAlloc is needed
  // to prevent an overflow which can be used to trigger a segfault
  // on 32bit architectures with this example program:
  //
  // type T [1<<27 + 1]int64
  //
  // var d T
  // var s []T
  //
  // func main() {
  //   s = append(s, d, d, d, d)
  //   print(len(s), "\n")
  // }
  if overflow || capmem > maxAlloc {
    panic(errorString("growslice: cap out of range"))
  }

  var p unsafe.Pointer
  if et.ptrdata == 0 {
    p = mallocgc(capmem, nil, false)
    // The append() that calls growslice is going to overwrite from old.len to cap (which will be the new length).
    // Only clear the part that will not be overwritten.
    memclrNoHeapPointers(add(p, newlenmem), capmem-newlenmem)
  } else {
    // Note: can't use rawmem (which avoids zeroing of memory), because then GC can scan uninitialized memory.
    p = mallocgc(capmem, et, true)
    if lenmem > 0 && writeBarrier.enabled {
      // Only shade the pointers in old.array since we know the destination slice p
      // only contains nil pointers because it has been cleared during alloc.
      bulkBarrierPreWriteSrcOnly(uintptr(p), uintptr(old.array), lenmem-et.size+et.ptrdata)
    }
  }
  memmove(p, old.array, lenmem)

  return slice{p, old.len, newcap}
}

代码解释

  1. 首先,代码根据是否启用 race 和 msan 进行一些检查。
  2. 接下来,代码检查新容量是否小于旧容量,如果是,则抛出错误。
  3. 如果元素大小为 0,则返回一个具有指向全局变量 zerobase 的指针、长度为旧长度、容量为新容量的 slice,表示 append 操作不应该创建一个指向 nil 指针但长度不为 0 的 slice。
  4. 如果新容量大于旧容量的两倍,则将新容量设置为请求的容量,否则根据旧容量和请求的容量计算新容量。此时,代码使用一个循环来计算新容量,从而平滑地从扩大 2 倍转换为扩大 1.25 倍,以避免出现骤增或骤降的情况。
  5. 接下来,代码根据元素大小的不同采用不同的方法来计算旧 slice 的长度、新 slice 的长度和新 slice 的容量。对于大小为 1 的元素,不需要进行除法或乘法运算;对于大小为指针大小的元素,编译器会将除法或乘法运算优化为常量移位操作;对于大小为 2 的幂的元素,使用变量移位操作;对于其他大小的元素,使用 math 包中的 MulUintptr 函数进行计算。
  6. 然后,代码检查新容量是否超出最大分配大小,如果是,则抛出错误。这里还需要注意一个细节,即不仅要检查新容量是否超出最大分配大小,还要检查 capmem 是否大于 maxAlloc,以避免在某些 32 位架构上出现整数溢出的问题。
  7. 最后,代码根据旧 slice 的长度和新 slice 的容量分配一块新的内存,并将旧 slice 的元素复制到新内存中。如果元素大小为 0,则只需清除新内存中未覆盖的部分;否则需要使用 memmove 函数将旧 slice 的元素复制到新内存中。如果元素具有指针数据,则需要使用 writeBarrier(写屏障)将指针标记为已写入状态,以支持垃圾回收。最后,代码返回一个新的 slice,其中包含指向新内存的指针、旧长度和新容量。

参考文档

golang slice扩容机制 http://lifegoeson.cn/2022/01/22/golang%20slice%E6%89%A9%E5%AE%B9%E6%9C%BA%E5%88%B6/

https://github.com/golang/go/blob/2dda92ff6f9f07eeb110ecbf0fc2d7a0ddd27f9d/src/runtime/slice.go#L164

标签:扩容,slice,old,uintptr,cap,数组,et,Go,newcap
From: https://www.cnblogs.com/pDJJq/p/18092500/go-array-expansion-rules-z1b6qdk

相关文章

  • Goland 反射的一些注意事项
    Goland反射的一些注意事项Goland反射的一些注意事项reflected:=reflect.ValueOf(obj).Elem()ifreflected.CanInterface(){//tryinstructreceiverifc,ok:=reflected.Interface().(Parse);ok{gormRes.Merge(c.AsGormSQL())return......
  • Golang:无锁队列实现
    无锁队列实现无锁队列实现参考论文适用场景并发条件下,需要使用到队列的情况,都可以使用无锁队列技术要点使用atomic.loadpointer来实现实时的指针相等判断使用CAS来替代同步状态下的p=p.next额外判断tail是不是正确的tail,并且不断地尝试推后他示例代码pack......
  • Golang: 通过chan来实现并发访问控制
    通过chan来实现并发访问控制通过chan来实现并发访问控制背景介绍这是在阅读grom的源码时,他的schema的初始化方式,给我留下来很深刻的印象,本文将通过channel的一些使用来实现实例的并发访问技术要点如果chan为空时,尝试读可以成功,获得的结果为空示例代码packagemai......
  • Django - 配置Django-Debug-Toolbar
    配置Django-Debug-Toolbargithub:https://github.com/jazzband/django-debug-toolbar安装Django-Debug-Toolbarpipinstalldjango-debug-toolbar配置-修改settings.pyifDEBUG:MIDDLEWARE+=['debug_toolbar.middleware.DebugToolbarMiddleware',......
  • Pedagogic metalanguage in "Coco"
    "Coco"isananimatedfilmaboutayoungboynamedMiguelwhoembarksonanadventuretotheLandoftheDeadtofindhisgreat-great-grandfather,alegendarymusician.Alongtheway,hediscoversthetruemeaningoffamilyandthepowerofmusi......
  • 一个操作让数组处理速度快了5倍,到底是为什么
     概述:通过对数组进行排序,代码更好地利用了缓存,从而提高了程序的性能。这种现象通常被称为"缓存友好"(cache-friendly)或"空间局部性"(spatiallocality)今天做一个数组数据计算时,发现一个效率问题,给大家分享一下一个数组排序和不排序时同样的逻辑处理速度是不一样的。排序后速度......
  • Pedagogic metalanguage
    《妈妈咪鸭》-DuckDuckGooseThefollowingthreepictureshavedrawnthemainideaofthefilm,usingthefocusshotontheaddressor,makingtheoldturtle’sfacialexpressionandthespeedofwordsstandout!Thistwocharactersinthepictureformancompari......
  • Pedagogic metalanguage
    MyfavoriteEnglishmovieisHeartthorb.Iwatcheditthreetimesintotal,andwiththegrowthofageandexperience,eachtimeIwatcheditIwouldhaveadifferentunderstandingoflove.TodayIwillusepedagogicmetalanguagetoanalyzethismyfavorit......
  • Pedagogic Metalanguage about Interstellar
    TheINTERSTELLARisthestartofmyintertesttosciencefictionwork.Inmyopinion,TheINTERSTELLARisrealsciencefictiondifferentfromwesternspaceoperasuchasWARHAMMERFANTASYandDUNE.(1)FeatureofVINTERSTELLAR:reasonablestorylineandwon......
  • Pedagogic metalanguage of "Heidi"
    TheclipIchooseisfromoneofmyfavoritemoviescalledHeidi.AndthefollowpictureisaboutHeidi,whowasbroughttocentraltownbyherauntandlefthergrandfatherandtheirsmallbutwarmhouse.Insteadofusingthebrightandharmoniouscolor......