Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Smoother column resize #42

Merged
merged 1 commit into from
Dec 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 21 additions & 10 deletions column.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,14 +261,25 @@ func (c *columnBool) Snapshot(chunk commit.Chunk, dst *commit.Buffer) {

// --------------------------- funcs ----------------------------

// capacityFor computes the next power of 2 for a given index
func capacityFor(v uint32) int {
v--
v |= v >> 1
v |= v >> 2
v |= v >> 4
v |= v >> 8
v |= v >> 16
v++
return int(v)
// resize calculates the new required capacity and a new index
func resize(capacity int, v uint32) int {
const threshold = 256
if v < threshold {
v |= v >> 1
v |= v >> 2
v |= v >> 4
v |= v >> 8
v |= v >> 16
v++
return int(v)
}

if capacity < threshold {
capacity = threshold
}

for 0 < capacity && capacity < int(v+1) {
capacity += (capacity + 3*threshold) / 4
}
return capacity
}
2 changes: 1 addition & 1 deletion column_generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (c *columnNumber) Grow(idx uint32) {
}

c.fill.Grow(idx)
clone := make([]number, idx+1, capacityFor(idx+1))
clone := make([]number, idx+1, resize(cap(c.data), idx+1))
copy(clone, c.data)
c.data = clone
}
Expand Down
20 changes: 10 additions & 10 deletions column_numbers.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions column_strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (c *columnEnum) Grow(idx uint32) {
}

c.fill.Grow(idx)
clone := make([]uint32, idx+1, capacityFor(idx+1))
clone := make([]uint32, idx+1, resize(cap(c.locs), idx+1))
copy(clone, c.locs)
c.locs = clone
}
Expand Down Expand Up @@ -175,7 +175,7 @@ func (c *columnString) Grow(idx uint32) {
}

c.fill.Grow(idx)
clone := make([]string, idx+1, capacityFor(idx+1))
clone := make([]string, idx+1, resize(cap(c.data), idx+1))
copy(clone, c.data)
c.data = clone
}
Expand Down
14 changes: 14 additions & 0 deletions column_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,3 +398,17 @@ func TestSnapshotIndex(t *testing.T) {
output.Apply(rdr)
assert.Equal(t, input.Column.(*columnIndex).fill, output.Column.(*columnIndex).fill)
}

func TestResize(t *testing.T) {
assert.Equal(t, 1, resize(100, 0))
assert.Equal(t, 2, resize(100, 1))
assert.Equal(t, 4, resize(100, 2))
assert.Equal(t, 16, resize(100, 11))
assert.Equal(t, 256, resize(100, 255))
assert.Equal(t, 1232, resize(100, 1000))
assert.Equal(t, 1232, resize(200, 1000))
assert.Equal(t, 1232, resize(512, 1000))
assert.Equal(t, 1213, resize(500, 1000)) // Inconsistent
assert.Equal(t, 22504, resize(512, 20000))
assert.Equal(t, 28322, resize(22504, 22600))
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/kelindar/column
go 1.17

require (
github.com/kelindar/bitmap v1.1.4
github.com/kelindar/bitmap v1.1.5
github.com/kelindar/intmap v1.1.0
github.com/kelindar/iostream v1.3.0
github.com/kelindar/smutex v1.0.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
github.com/kelindar/async v1.0.0 h1:oJiFAt3fVB/b5zVZKPBU+pP9lR3JVyeox9pYlpdnIK8=
github.com/kelindar/async v1.0.0/go.mod h1:bJRlwaRiqdHi+4dpVDNHdwgyRyk6TxpA21fByLf7hIY=
github.com/kelindar/bitmap v1.1.4 h1:rNwZ6RMRhrE3Um0QqBwFoJAcAzYi/4M7XGDCZYS6TOU=
github.com/kelindar/bitmap v1.1.4/go.mod h1:shAFyS8BOif+pvJ05GqxnCM0SdohHQjKvDetqI/9z6M=
github.com/kelindar/bitmap v1.1.5 h1:cqXplFOOwJX/HRu+GZBcz03wo2LZftVUMsuAjW/3/rQ=
github.com/kelindar/bitmap v1.1.5/go.mod h1:URwjvM6WXldcKN7/D3FLHy0LSkEdg3rE7VjdN1DcI9E=
github.com/kelindar/intmap v1.1.0 h1:S+YEDvw5FQus5UJDEG+xsLp8il3BTYqBMkkuVVZPMH8=
github.com/kelindar/intmap v1.1.0/go.mod h1:tDanawPWq1B0HC+X3W8Z6IKNrJqxjruy6CdyTlf6Nic=
github.com/kelindar/iostream v1.3.0 h1:Bz2qQabipZlF1XCk64bnxsGLete+iHtayGPeWVpbwbo=
Expand Down