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

GH-33999: [Go] Removed cast from byte[] to string and copied entire string when Value() is called #34450

Closed
wants to merge 1 commit into from
Closed
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
7 changes: 5 additions & 2 deletions go/arrow/array/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,11 @@ func (a *String) Reset(data arrow.ArrayData) {

// Value returns the slice at index i. This value should not be mutated.
func (a *String) Value(i int) string {
sb := strings.Builder{}

i = i + a.array.data.offset
return a.values[a.offsets[i]:a.offsets[i+1]]
sb.WriteString(a.values[a.offsets[i]:a.offsets[i+1]])
return sb.String()
Comment on lines +53 to +57
Copy link
Member

@zeroshade zeroshade Mar 6, 2023

Choose a reason for hiding this comment

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

There's no need to use the string builder here, you can just do string([]byte(a.values[a.offsets[i]:a.offsets[i+1]])[:]) or something like:

val := a.values[a.offsets[i]:a.offsets[i+1]]
out := make([]byte, len(val))
copy(out, val)
return *(*string)(unsafe.Pointer(&out))

Copy link
Member

Choose a reason for hiding this comment

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

Ideally we'd still maintain a method that would let someone retrieve the slice referencing the data to allow consumers to avoid the copy if desired (such as the GetView in the C++ lib)

}

// ValueOffset returns the offset of the value at index i.
Expand Down Expand Up @@ -246,7 +249,7 @@ func (a *LargeString) setData(data *Data) {

if vdata := data.buffers[2]; vdata != nil {
b := vdata.Bytes()
a.values = *(*string)(unsafe.Pointer(&b))
a.values = string(b[:])
Comment on lines 250 to +252
Copy link
Member

Choose a reason for hiding this comment

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

we shouldn't copy the entire buffer here. Keep the unsafe.Pointer here so we only perform copies in the Value method and only on the values that are requested.

}

if offsets := data.buffers[1]; offsets != nil {
Expand Down