Skip to content

Commit

Permalink
groot: streamline unsafeDecay{,Slice}Array
Browse files Browse the repository at this point in the history
Signed-off-by: Sebastien Binet <[email protected]>
  • Loading branch information
sbinet committed Feb 12, 2024
1 parent 3aec323 commit ac11738
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 415 deletions.
43 changes: 6 additions & 37 deletions groot/gen.rtree.go
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,6 @@ func genRLeaves() {
Size int
Kind Kind
Func string
Decay string
Count bool

WithStreamerElement bool // for TLeaf{F16,D32}
Expand Down Expand Up @@ -695,13 +694,6 @@ func genRLeaves() {
typ.Func = typ.Name
}

switch typ.Type[0] {
case 'u':
typ.Decay = "unsafeDecayArrayU"
default:
typ.Decay = "unsafeDecayArray"
}

tmpl := template.Must(template.New(typ.Name).Parse(rleafTmpl))
err = tmpl.Execute(f, typ)
if err != nil {
Expand Down Expand Up @@ -757,7 +749,7 @@ func newRLeaf{{.Name}}(leaf *{{.Base}}, rvar ReadVar, rctx rleafCtx) rleaf {
}
sli := reflect.ValueOf(rvar.Value).Elem()
ptr := (*[]{{.Type}})(unsafe.Pointer(sli.UnsafeAddr()))
hdr := unsafeDecaySliceArray{{.Name}}(ptr, sz).(*[]{{.Type}})
hdr := unsafeDecaySliceArray[{{.Type}}](ptr, sz)
if *hdr == nil {
*hdr = make([]{{.Type}}, 0, rleafDefaultSliceCap*sz)
}
Expand All @@ -766,16 +758,15 @@ func newRLeaf{{.Name}}(leaf *{{.Base}}, rvar ReadVar, rctx rleafCtx) rleaf {
n: rctx.rcountFunc(leaf.count.Name()),
v: hdr,
}
rawSli := (*reflect.SliceHeader)(unsafe.Pointer(sli.UnsafeAddr()))
rawHdr := (*reflect.SliceHeader)(unsafe.Pointer(hdr))
rhdr := reflect.ValueOf(hdr).Elem()
rptr := reflect.ValueOf(ptr).Elem()
// alias slices
rawSli.Data = rawHdr.Data
rptr.Set(rhdr)
rleaf.set = func() {
n := rleaf.n()
rawSli.Len = n
rawSli.Cap = n
rptr.SetLen(n)
}
return rleaf
Expand All @@ -784,7 +775,7 @@ func newRLeaf{{.Name}}(leaf *{{.Base}}, rvar ReadVar, rctx rleafCtx) rleaf {
case leaf.len > 1:
return &rleafArr{{.Name}}{
base: leaf,
v: reflect.ValueOf(unsafeDecayArray{{.Name}}(rvar.Value)).Elem().Interface().([]{{.Type}}),
v: *unsafeDecayArray[{{.Type}}](rvar.Value),
}
default:
Expand All @@ -808,28 +799,6 @@ func (leaf *rleaf{{.Kind}}{{.Name}}) ivalue() int { return int(*leaf.v) }
{{- end}}
{{- end}}
{{if eq .Kind "Arr"}}
func unsafeDecayArray{{.Name}}(ptr interface{}) interface{} {
rv := reflect.ValueOf(ptr).Elem()
sz := rv.Type().Size() / {{.Size}}
arr := (*[0]{{.Type}})(unsafe.Pointer(rv.UnsafeAddr()))
sli := (*arr)[:]
hdr := (*reflect.SliceHeader)(unsafe.Pointer(&sli))
hdr.Len = int(sz)
hdr.Cap = int(sz)
return &sli
}
func unsafeDecaySliceArray{{.Name}}(ptr *[]{{.Type}}, size int) interface{} {
var sli []{{.Type}}
hdr := (*reflect.SliceHeader)(unsafe.Pointer(&sli))
hdr.Len = int(size)
hdr.Cap = int(size)
hdr.Data = (*reflect.SliceHeader)(unsafe.Pointer(ptr)).Data
return &sli
}
{{- end}}
{{if .WithStreamerElement}}
func (leaf *rleaf{{.Kind}}{{.Name}}) readFromBuffer(r *rbytes.RBuffer) error {
{{- if eq .Kind "Val" }}
Expand Down
24 changes: 24 additions & 0 deletions groot/rtree/rleaf.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,3 +344,27 @@ func (l *rleafCount) imax() int {
var (
_ leafCount = (*rleafCount)(nil)
)

// FIXME(sbinet): directly use reflect.TypeFor[T]().Size()
// instead of this shim function.
// (when Go >= 1.22)
func sizeOfT[T any]() uintptr {
var t T
return reflect.TypeOf(t).Size()
}

func unsafeDecayArray[T any](ptr any) *[]T {
rv := reflect.ValueOf(ptr).Elem()
sz := rv.Type().Size() / sizeOfT[T]()
sli := unsafe.Slice((*T)(unsafe.Pointer(rv.UnsafeAddr())), sz)
return &sli
}

func unsafeDecaySliceArray[T any](ptr *[]T, size int) *[]T {
var sli []T
if *ptr == nil {
return &sli
}
sli = unsafe.Slice(unsafe.SliceData(*ptr), size)
return &sli
}
Loading

0 comments on commit ac11738

Please sign in to comment.