1
0
Fork 0
forked from External/ergo

Merge pull request #1231 from slingamn/buffer.2

more memory-efficient implementation of line reading
This commit is contained in:
Shivaram Lingamneni 2020-11-30 02:34:25 -08:00 committed by GitHub
commit db100f1f91
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 231 additions and 45 deletions

View file

@ -322,18 +322,6 @@ func (list *Buffer) next(index int) int {
}
}
// return n such that v <= n and n == 2**i for some i
func roundUpToPowerOfTwo(v int) int {
// http://graphics.stanford.edu/~seander/bithacks.html
v -= 1
v |= v >> 1
v |= v >> 2
v |= v >> 4
v |= v >> 8
v |= v >> 16
return v + 1
}
func (list *Buffer) maybeExpand() {
if list.window == 0 {
return // autoresize is disabled
@ -353,7 +341,7 @@ func (list *Buffer) maybeExpand() {
return // oldest element is old enough to overwrite
}
newSize := roundUpToPowerOfTwo(length + 1)
newSize := utils.RoundUpToPowerOfTwo(length + 1)
if list.maximumSize < newSize {
newSize = list.maximumSize
}