Skip to content

Commit

Permalink
Copy bytes as needed
Browse files Browse the repository at this point in the history
  • Loading branch information
xichen2020 committed Mar 14, 2019
1 parent 6e195f3 commit 29b1e5f
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 11 deletions.
19 changes: 16 additions & 3 deletions document/field/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,20 @@ func (v ValueUnion) MarshalJSON() ([]byte, error) {
}
}

// ValueCloneOptions controls how a value should be cloned.
type ValueCloneOptions struct {
DeepCloneBytes bool
}

// Clone clones a value union.
func (v *ValueUnion) Clone(opts ValueCloneOptions) ValueUnion {
cloned := *v
if v.Type == BytesType && opts.DeepCloneBytes {
cloned.BytesVal = bytes.NewImmutableBytes(v.BytesVal.SafeBytes())
}
return cloned
}

// ToProto converts a value to a value proto message.
func (v *ValueUnion) ToProto() (servicepb.FieldValue, error) {
var fb servicepb.FieldValue
Expand Down Expand Up @@ -491,14 +505,13 @@ func (v Values) Hash() uint64 {
}

// Clone clones the values.
func (v Values) Clone() Values {
func (v Values) Clone(opts ValueCloneOptions) Values {
if len(v) == 0 {
return nil
}
cloned := make(Values, 0, len(v))
for i := 0; i < len(v); i++ {
// NB: This is fine as each value union does not contain reference types.
cloned = append(cloned, v[i])
cloned = append(cloned, v[i].Clone(opts))
}
return cloned
}
Expand Down
9 changes: 5 additions & 4 deletions query/executor/doc_id_values.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,18 @@ type docIDValues struct {
// cloneDocIDValues clones the incoming (doc ID, values) pair.
func cloneDocIDValues(v docIDValues) docIDValues {
// TODO(xichen): Should pool and reuse the value array here.
v.Values = v.Values.Clone()
v.Values = v.Values.Clone(field.ValueCloneOptions{DeepCloneBytes: true})
return v
}

// cloneDocIDValuesTo clones the (doc ID, values) pair from `src` to `target`.
// Precondition: `target` values are guaranteed to have the same length as `src` and can be reused.
func cloneDocIDValuesTo(src docIDValues, target *docIDValues) {
reusedValues := target.Values
copy(reusedValues, src.Values)
target.DocID = src.DocID
target.Values = reusedValues
for i := 0; i < len(src.Values); i++ {
cloned := src.Values[i].Clone(field.ValueCloneOptions{DeepCloneBytes: true})
target.Values[i] = cloned
}
}

type docIDValuesByDocIDAsc []docIDValues
Expand Down
15 changes: 12 additions & 3 deletions query/single_key_result_groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,10 @@ func (m *SingleKeyResultGroups) getOrInsertBytes(
return nil, RejectedDueToLimit
}
arr = m.resultArrayProtoType.New()
m.bytesResults.Set(v.SafeBytes(), arr)
m.bytesResults.SetUnsafe(v.SafeBytes(), arr, SetUnsafeBytesOptions{
NoCopyKey: true,
NoFinalizeKey: true,
})
return arr, Inserted
}

Expand Down Expand Up @@ -423,7 +426,10 @@ func (m *SingleKeyResultGroups) mergeBytesGroups(other *SingleKeyResultGroups) {
// Limit reached, do nothing.
continue
}
m.bytesResults.Set(k, v)
m.bytesResults.SetUnsafe(k, v, SetUnsafeBytesOptions{
NoCopyKey: true,
NoFinalizeKey: true,
})
}
}

Expand Down Expand Up @@ -891,7 +897,10 @@ func (m *SingleKeyResultGroups) trimBytesToTopN(targetSize int) {
})
data := m.topNBytes.RawData()
for i := 0; i < len(data); i++ {
m.bytesResults.Set(data[i].Key, data[i].Values)
m.bytesResults.SetUnsafe(data[i].Key, data[i].Values, SetUnsafeBytesOptions{
NoCopyKey: true,
NoFinalizeKey: true,
})
data[i] = emptyBytesResultGroup
}
m.topNBytes.Reset()
Expand Down
2 changes: 1 addition & 1 deletion query/values_result_array_new_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func NewValuesResultArrayMap(initialSize int) *ValuesResultArrayHash {
return v1.Equal(v2)
},
copy: func(v field.Values) field.Values {
return v.Clone()
return v.Clone(field.ValueCloneOptions{DeepCloneBytes: true})
},
finalize: nil, // No op on key removal
initialSize: initialSize,
Expand Down

0 comments on commit 29b1e5f

Please sign in to comment.