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

Clear string pointers before returning them to the memory pool. #131

Merged
merged 6 commits into from
Feb 21, 2019

Conversation

notbdu
Copy link
Collaborator

@notbdu notbdu commented Feb 20, 2019

String pointers are now cleared before putting string arrays back into the memory pool.

@@ -5,6 +5,7 @@ import (
"testing"

"github.com/stretchr/testify/require"
"github.com/xichen2020/eventdb/x/strings"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Import

doubleArrayPool *pool.BucketizedFloat64ArrayPool
stringArrayPool *pool.BucketizedStringArrayPool
int64ArrayPool *pool.BucketizedInt64ArrayPool
stringValuesResetFn strings.ClearArrayFn
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps just ArrayFn?

@@ -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 {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: SetStringArrayResetFn.

}

// StringValuesResetFn resets string values before returning a string array back to the memory pool.
func (o *DocsFieldBuilderOptions) StringValuesResetFn() strings.ClearArrayFn {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

StringArrayResetFn.

@@ -100,7 +101,8 @@ func newMutableSegment(
SetIntArrayPool(opts.IntArrayPool()).
SetDoubleArrayPool(opts.DoubleArrayPool()).
SetStringArrayPool(opts.StringArrayPool()).
SetInt64ArrayPool(opts.Int64ArrayPool())
SetInt64ArrayPool(opts.Int64ArrayPool()).
SetStringValuesResetFn(strings.ValuesResetFn)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ResetArrayFn.

Copy link
Owner

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)
Copy link
Owner

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?

package strings

// ClearArrayFn is the type signature of a string slice clearing fn.
type ClearArrayFn func(values []string)
Copy link
Owner

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.

type ClearArrayFn func(values []string)

// ValuesResetFn clears string pointers inside of a slice of strings.
func ValuesResetFn(values []string) {
Copy link
Owner

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)
Copy link
Owner

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.

Copy link
Owner

@xichen2020 xichen2020 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@notbdu notbdu merged commit 1ea5edc into master Feb 21, 2019
@notbdu notbdu deleted the bdu/clear-string-pointers branch February 21, 2019 18:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants