-
Notifications
You must be signed in to change notification settings - Fork 1
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
Clear string pointers before returning them to the memory pool. #131
Conversation
@@ -5,6 +5,7 @@ import ( | |||
"testing" | |||
|
|||
"github.com/stretchr/testify/require" | |||
"github.com/xichen2020/eventdb/x/strings" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Import
index/field/docs_field.go
Outdated
doubleArrayPool *pool.BucketizedFloat64ArrayPool | ||
stringArrayPool *pool.BucketizedStringArrayPool | ||
int64ArrayPool *pool.BucketizedInt64ArrayPool | ||
stringValuesResetFn strings.ClearArrayFn |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps just ArrayFn
?
index/field/docs_field.go
Outdated
@@ -963,3 +965,15 @@ func (o *DocsFieldBuilderOptions) SetInt64ArrayPool(v *pool.BucketizedInt64Array | |||
func (o *DocsFieldBuilderOptions) Int64ArrayPool() *pool.BucketizedInt64ArrayPool { | |||
return o.int64ArrayPool | |||
} | |||
|
|||
// SetStringValuesResetFn sets a value reset function for string values. | |||
func (o *DocsFieldBuilderOptions) SetStringValuesResetFn(fn strings.ClearArrayFn) *DocsFieldBuilderOptions { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: SetStringArrayResetFn
.
index/field/docs_field.go
Outdated
} | ||
|
||
// StringValuesResetFn resets string values before returning a string array back to the memory pool. | ||
func (o *DocsFieldBuilderOptions) StringValuesResetFn() strings.ClearArrayFn { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
StringArrayResetFn
.
storage/mutable_segment.go
Outdated
@@ -100,7 +101,8 @@ func newMutableSegment( | |||
SetIntArrayPool(opts.IntArrayPool()). | |||
SetDoubleArrayPool(opts.DoubleArrayPool()). | |||
SetStringArrayPool(opts.StringArrayPool()). | |||
SetInt64ArrayPool(opts.Int64ArrayPool()) | |||
SetInt64ArrayPool(opts.Int64ArrayPool()). | |||
SetStringValuesResetFn(strings.ValuesResetFn) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ResetArrayFn
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also add a comment here to say this is to avoid holding on to documents received after the array is returned to pool.
@@ -15,7 +18,7 @@ func TestRefCountedPooledStringArray(t *testing.T) { | |||
pool.Init(func(capacity int) []string { return make([]string, 0, capacity) }) | |||
|
|||
vals := pool.Get(4) | |||
arr := NewRefCountedPooledStringArray(vals, pool) | |||
arr := NewRefCountedPooledStringArray(vals, pool, strings.ValuesResetFn) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps just leave as nil so you don't need to change the other parts of test?
x/strings/strings.go
Outdated
package strings | ||
|
||
// ClearArrayFn is the type signature of a string slice clearing fn. | ||
type ClearArrayFn func(values []string) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just ArrayFn
? More generic that way.
x/strings/strings.go
Outdated
type ClearArrayFn func(values []string) | ||
|
||
// ValuesResetFn clears string pointers inside of a slice of strings. | ||
func ValuesResetFn(values []string) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just ResetArray
?
@@ -32,24 +33,25 @@ func TestRefCountedPooledStringArray(t *testing.T) { | |||
require.Equal(t, 4, len(arr.Get())) | |||
require.Equal(t, int32(2), arr2.cnt.RefCount()) | |||
require.Equal(t, 4, len(arr2.Get())) | |||
expected1 := []string{"foo", "bar", "baz", "cat"} | |||
assertReturnedToStringArrayPool(t, pool, expected1, false) | |||
assertReturnedToStringArrayPool(t, pool, arr.Get(), false) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm I don't think you need to change the rest of the tests since you already set the reset fn to nil above. I generally prefer to assert against a very explicit value if possible so in this case I prefer to assert against an expected string array instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
String pointers are now cleared before putting string arrays back into the memory pool.