google-cloud-go icon indicating copy to clipboard operation
google-cloud-go copied to clipboard

firestore: when decoding into a short slice, try to grow it

Open jba opened this issue 6 years ago • 5 comments

There is a case when decoding values that doesn't match the behavior of encoding/json. When decoding an array value that is too large for the slice we create a new slice (https://code.googlesource.com/gocloud/+/v0.34.0/firestore/from_value.go#186). The json code grows the existing slice (https://go.googlesource.com/go/+/go1.11.4/src/encoding/json/decode.go#556).

This is a behavior change, so it should be fixed before leaving beta.

jba avatar Jan 02 '19 12:01 jba

Actually, since firestore knows the length, I believe the firestore code should be

 if v.Cap() >= xlen {
     v.SetLen(xlen)
} else {
    v.Set(reflect.MakeSlice(v.Type(), xlen, xlen))
}

(untested)

jba avatar Jan 02 '19 12:01 jba

@jba Is this a performance / allocation improvement, or are there behavior change that a user might see?

jeanbza avatar Jan 03 '19 23:01 jeanbza

It's both. The input slice will be overwritten instead of being left intact.

jba avatar Jan 04 '19 00:01 jba

Not sure I'm getting the point. The input slice must change its size and be completely overwriten with new values, or you want to union values, like:

result := setReflectFromProtoValue({1}, {2, 3})
fmt.Println(result)
> {1, 2, 3} or {2, 3, 1}

? As I understand, "growing" means "union with old"

IlyaFaer avatar Jul 04 '19 13:07 IlyaFaer

Going through old firestore issues -- it looks like this can't be fixed in the current library version since it's now GA, based on what @jba wrote. I'll leave this open as a potential future improvement.

tritone avatar Jul 13 '20 20:07 tritone