Skip to content

Commit

Permalink
Change how array regions are serialized
Browse files Browse the repository at this point in the history
  • Loading branch information
chriso committed Nov 24, 2023
1 parent 062a8bc commit f21a835
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 30 deletions.
19 changes: 9 additions & 10 deletions gen/proto/go/coroutine/v1/coroutine.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion gen/proto/go/coroutine/v1/coroutine_vtproto.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 7 additions & 8 deletions proto/coroutine/v1/coroutine.proto
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,15 @@ message Build {

// Region is an encoded region of memory.
message Region {
// Type is the type of the region.
// Type is the type of the region, shifted left by one.
//
// The least significant bit indicates that this region represents
// an array, and that the type is the array element type rather
// than the object that's encoded in this region.
uint32 type = 1;

// Array length, when >= 0, indicates that this region is an
// array of the specified length (in terms of number of
// elements, not in terms of bytes) and that the region type
// is the type of the array element. If the array length is < 0
// then this region does not represent an array, and the region
// type describes the object that is encoded.
int32 array_length = 2;
// Array length, for regions that are arrays.
uint32 array_length = 2;

// Data is the encoded contents of the memory region.
bytes data = 3;
Expand Down
4 changes: 2 additions & 2 deletions types/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -598,8 +598,8 @@ func (t *Region) Index() int {

// Type is the type of the region.
func (r *Region) Type() *Type {
t := r.state.Type(int(r.region.Type - 1))
if r.region.ArrayLength >= 0 {
t := r.state.Type(int((r.region.Type >> 1) - 1))
if r.region.Type&1 == 1 {
t = newArrayType(r.state, int64(r.region.ArrayLength), t)
}
return t
Expand Down
14 changes: 8 additions & 6 deletions types/reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,8 +415,11 @@ func serializePointedAt(s *Serializer, et reflect.Type, length int, p unsafe.Poi
}

region := &coroutinev1.Region{
Type: s.types.ToType(r.typ),
ArrayLength: int32(r.len),
Type: s.types.ToType(r.typ) << 1,
}
if r.len >= 0 {
region.Type |= 1
region.ArrayLength = uint32(r.len)
}
s.regions = append(s.regions, region)

Expand Down Expand Up @@ -473,9 +476,9 @@ func deserializePointedAt(d *Deserializer, t reflect.Type, length int) unsafe.Po
}
region := d.regions[id-1]

regionType := d.types.ToReflect(typeid(region.Type))
regionType := d.types.ToReflect(typeid(region.Type >> 1))

if region.ArrayLength >= 0 {
if region.Type&1 == 1 {
elemSize := int(regionType.Size())
length := int(region.ArrayLength)
data := make([]byte, elemSize*length)
Expand Down Expand Up @@ -531,8 +534,7 @@ func serializeMapReflect(s *Serializer, t reflect.Type, r reflect.Value) {
size := r.Len()

region := &coroutinev1.Region{
Type: s.types.ToType(t),
ArrayLength: -1, // not an array
Type: s.types.ToType(t) << 1,
}
s.regions = append(s.regions, region)

Expand Down
5 changes: 2 additions & 3 deletions types/serde.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,8 @@ func Serialize(x any) ([]byte, error) {
Strings: s.strings.strings,
Regions: s.regions,
Root: &coroutinev1.Region{
Type: s.types.ToType(t),
ArrayLength: -1, // not an array
Data: s.b,
Type: s.types.ToType(t) << 1,
Data: s.b,
},
}
return state.MarshalVT()
Expand Down

0 comments on commit f21a835

Please sign in to comment.