In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "how to expand the capacity of slice". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Question 1 the underlying data structure of the slice
Damn it, is it so direct?
I guess it was an array plus a linked list, and I got it wrong by 0.
Look at the source code.
Runtime/slice.go
Type slice struct {array unsafe.Pointer / / data structures are simple and rough array pointers len int cap int} question 2 how does len int cap int slice expand its capacity
Guess wrong again ~
Let's keep looking at the source code.
After searching for a long time from the source code, I found one here.
Growslice handles slice growth during append.
So, it's you.
Func growslice (et * _ type, old slice, cap int) slice {/ / third cap, new minimum capacity / / Barra a heap to determine newcap: = old.cap / / variable storage size doublecap: = newcap + newcap / / a double space size if cap > doublecap {/ / if the history space is more than double the capacity New minimum capacity newcap = cap} else {/ / if the length is less than 1024, the new length is twice the old capacity if old.len
< 1024 { newcap = doublecap } else { //当大于1024 走公式 newcap += newcap / 4,直到newcap大于等于老cap for 0 < newcap && newcap < cap { newcap += newcap / 4 } if newcap maxAlloc newcap = int(capmem) case et.size == sys.PtrSize: lenmem = uintptr(old.len) * sys.PtrSize newlenmem = uintptr(cap) * sys.PtrSize capmem = roundupsize(uintptr(newcap) * sys.PtrSize) overflow = uintptr(newcap) >MaxAlloc/sys.PtrSize newcap = int (capmem / sys.PtrSize) case isPowerOfTwo (et.size): var shift uintptr if sys.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) 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)} / / if append adds too many elements at a time Directly report an error, cross the limit, or exceed the capacity if overflow | | capmem > maxAlloc {panic (errorString ("growslice: cap out of range"))} var p unsafe.Pointer / / apply for new memory And point the pointer to p if et.ptrdata = 0 {p = mallocgc (capmem, nil, false) memclrNoHeapPointers (add (p, newlenmem), capmem-newlenmem)} else {p = mallocgc (capmem, et, true) if lenmem > 0 & writeBarrier.enabled {bulkBarrierPreWriteSrcOnly (uintptr (p), uintptr (old.array)) Lenmem)}} / / move old data to new data memmove (p, old.array, lenmem) return slice {p, old.len, newcap}} summary
In fact, it can be seen that the slice expansion of golang is relatively rough, and the value is directly assigned to the copy. However, the two units of measurement of length and capacity distinguished by golang generally allocate enough cap in advance to reduce the number of maclloc.
This is the end of the content of "how slice is expanded". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.