Skip to content

Commit

Permalink
Stop rebuilding IntFieldBuilder on each Add call (#119)
Browse files Browse the repository at this point in the history
  • Loading branch information
black-adder committed Feb 12, 2019
1 parent 4db8e54 commit 11667e5
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 3 deletions.
2 changes: 1 addition & 1 deletion index/field/docs_field.go
Original file line number Diff line number Diff line change
Expand Up @@ -834,7 +834,7 @@ func (b *docsFieldBuilder) addBool(docID int32, v bool) error {
}

func (b *docsFieldBuilder) addInt(docID int32, v int) error {
if b.bfb == nil {
if b.ifb == nil {
docIDsBuilder := b.newDocIDSetBuilder()
intValuesBuilder := impl.NewArrayBasedIntValues(b.opts.IntArrayPool())
b.ifb = newIntFieldBuilder(docIDsBuilder, intValuesBuilder)
Expand Down
85 changes: 83 additions & 2 deletions index/field/docs_field_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,11 @@ func TestDocsFieldNewDocsField(t *testing.T) {
}
require.Equal(t, expectedMeta, newField.Metadata())

// Closing the sealed field should not cause the string field to be retruend to pool.
// Closing the sealed field should not cause the string field to be returned to pool.
sealed.Close()
assertReturnedToStringArrayPool(t, stringArrayPool, expected, false)

// Closing the new field should cause the string field to be retruend to pool.
// Closing the new field should cause the string field to be returned to pool.
newField.Close()
assertReturnedToStringArrayPool(t, stringArrayPool, expected, true)
}
Expand Down Expand Up @@ -259,6 +259,87 @@ func TestDocsFieldMergeInPlace(t *testing.T) {
assertReturnedToStringArrayPool(t, stringArrayPool2, expected2, true)
}

func TestDocsFieldFieldBuilderInitializedOnce(t *testing.T) {
var (
boolArrayBuckets = []pool.BoolArrayBucket{
{Capacity: 128, Count: 1},
}
doubleArrayBuckets = []pool.Float64ArrayBucket{
{Capacity: 128, Count: 1},
}
intArrayBuckets = []pool.IntArrayBucket{
{Capacity: 128, Count: 1},
}
int64ArrayBuckets = []pool.Int64ArrayBucket{
{Capacity: 128, Count: 1},
}
stringArrayBuckets = []pool.StringArrayBucket{
{Capacity: 128, Count: 1},
}
boolArrayPool = pool.NewBucketizedBoolArrayPool(boolArrayBuckets, nil)
doubleArrayPool = pool.NewBucketizedFloat64ArrayPool(doubleArrayBuckets, nil)
intArrayPool = pool.NewBucketizedIntArrayPool(intArrayBuckets, nil)
int64ArrayPool = pool.NewBucketizedInt64ArrayPool(int64ArrayBuckets, nil)
stringArrayPool = pool.NewBucketizedStringArrayPool(stringArrayBuckets, nil)
boolAllocs int
doubleAllocs int
intAllocs int
int64Allocs int
stringAllocs int
)
boolArrayPool.Init(func(capacity int) []bool {
boolAllocs++
return make([]bool, 0, capacity)
})
doubleArrayPool.Init(func(capacity int) []float64 {
doubleAllocs++
return make([]float64, 0, capacity)
})
intArrayPool.Init(func(capacity int) []int {
intAllocs++
return make([]int, 0, capacity)
})
int64ArrayPool.Init(func(capacity int) []int64 {
int64Allocs++
return make([]int64, 0, capacity)
})
stringArrayPool.Init(func(capacity int) []string {
stringAllocs++
return make([]string, 0, capacity)
})
opts := NewDocsFieldBuilderOptions().
SetBoolArrayPool(boolArrayPool).
SetDoubleArrayPool(doubleArrayPool).
SetIntArrayPool(intArrayPool).
SetInt64ArrayPool(int64ArrayPool).
SetStringArrayPool(stringArrayPool)
builder := NewDocsFieldBuilder([]string{"testPath"}, opts)

builder.Add(1, field.ValueUnion{Type: field.BoolType, BoolVal: true})
builder.Add(2, field.ValueUnion{Type: field.BoolType, BoolVal: false})
builder.Add(1, field.ValueUnion{Type: field.DoubleType, DoubleVal: 1})
builder.Add(2, field.ValueUnion{Type: field.DoubleType, DoubleVal: 2})
builder.Add(1, field.ValueUnion{Type: field.IntType, IntVal: 1})
builder.Add(2, field.ValueUnion{Type: field.IntType, IntVal: 2})
builder.Add(1, field.ValueUnion{Type: field.TimeType, TimeNanosVal: 1})
builder.Add(2, field.ValueUnion{Type: field.TimeType, TimeNanosVal: 2})
builder.Add(1, field.ValueUnion{Type: field.StringType, StringVal: "foo"})
builder.Add(2, field.ValueUnion{Type: field.StringType, StringVal: "bar"})
builder.Close()

require.Equal(t, 1, boolAllocs)
require.Equal(t, 1, doubleAllocs)
require.Equal(t, 1, intAllocs)
require.Equal(t, 1, int64Allocs)
require.Equal(t, 1, stringAllocs)

require.Equal(t, []bool{true, false}, boolArrayPool.Get(2)[:2])
require.Equal(t, []float64{1, 2}, doubleArrayPool.Get(2)[:2])
require.Equal(t, []int{1, 2}, intArrayPool.Get(2)[:2])
require.Equal(t, []int64{1, 2}, int64ArrayPool.Get(2)[:2])
require.Equal(t, []string{"foo", "bar"}, stringArrayPool.Get(2)[:2])
}

func assertReturnedToStringArrayPool(
t *testing.T,
p *pool.BucketizedStringArrayPool,
Expand Down

0 comments on commit 11667e5

Please sign in to comment.