forked from External/ergo
Merge pull request #1231 from slingamn/buffer.2
more memory-efficient implementation of line reading
This commit is contained in:
commit
db100f1f91
7 changed files with 231 additions and 45 deletions
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -241,17 +241,6 @@ func TestDisabledByResize(t *testing.T) {
|
|||
assertEqual(len(items), 0, t)
|
||||
}
|
||||
|
||||
func TestRoundUp(t *testing.T) {
|
||||
assertEqual(roundUpToPowerOfTwo(2), 2, t)
|
||||
assertEqual(roundUpToPowerOfTwo(3), 4, t)
|
||||
assertEqual(roundUpToPowerOfTwo(64), 64, t)
|
||||
assertEqual(roundUpToPowerOfTwo(65), 128, t)
|
||||
assertEqual(roundUpToPowerOfTwo(100), 128, t)
|
||||
assertEqual(roundUpToPowerOfTwo(1000), 1024, t)
|
||||
assertEqual(roundUpToPowerOfTwo(1025), 2048, t)
|
||||
assertEqual(roundUpToPowerOfTwo(269435457), 536870912, t)
|
||||
}
|
||||
|
||||
func BenchmarkInsert(b *testing.B) {
|
||||
buf := NewHistoryBuffer(1024, 0)
|
||||
b.ResetTimer()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue