[chore] update go-structr => v0.6.2 (fixes nested field ptr following) (#2822)

* update go-structr => v0.6.1 (fixes nested field ptr following)

* bump to v0.6.2
This commit is contained in:
kim 2024-04-11 10:46:08 +01:00 committed by GitHub
parent 9fb8a78f91
commit db2dcc3455
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 108 additions and 58 deletions

2
go.mod
View file

@ -21,7 +21,7 @@ require (
codeberg.org/gruf/go-runners v1.6.2 codeberg.org/gruf/go-runners v1.6.2
codeberg.org/gruf/go-sched v1.2.3 codeberg.org/gruf/go-sched v1.2.3
codeberg.org/gruf/go-store/v2 v2.2.4 codeberg.org/gruf/go-store/v2 v2.2.4
codeberg.org/gruf/go-structr v0.6.0 codeberg.org/gruf/go-structr v0.6.2
codeberg.org/superseriousbusiness/exif-terminator v0.7.0 codeberg.org/superseriousbusiness/exif-terminator v0.7.0
github.com/DmitriyVTitov/size v1.5.0 github.com/DmitriyVTitov/size v1.5.0
github.com/KimMachineGun/automemlimit v0.5.0 github.com/KimMachineGun/automemlimit v0.5.0

4
go.sum
View file

@ -72,8 +72,8 @@ codeberg.org/gruf/go-sched v1.2.3 h1:H5ViDxxzOBR3uIyGBCf0eH8b1L8wMybOXcdtUUTXZHk
codeberg.org/gruf/go-sched v1.2.3/go.mod h1:vT9uB6KWFIIwnG9vcPY2a0alYNoqdL1mSzRM8I+PK7A= codeberg.org/gruf/go-sched v1.2.3/go.mod h1:vT9uB6KWFIIwnG9vcPY2a0alYNoqdL1mSzRM8I+PK7A=
codeberg.org/gruf/go-store/v2 v2.2.4 h1:8HO1Jh2gg7boQKA3hsDAIXd9zwieu5uXwDXEcTOD9js= codeberg.org/gruf/go-store/v2 v2.2.4 h1:8HO1Jh2gg7boQKA3hsDAIXd9zwieu5uXwDXEcTOD9js=
codeberg.org/gruf/go-store/v2 v2.2.4/go.mod h1:zI4VWe5CpXAktYMtaBMrgA5QmO0sQH53LBRvfn1huys= codeberg.org/gruf/go-store/v2 v2.2.4/go.mod h1:zI4VWe5CpXAktYMtaBMrgA5QmO0sQH53LBRvfn1huys=
codeberg.org/gruf/go-structr v0.6.0 h1:cFiTE0SN1RzgX833y5DnPgWcdVNfqcvLVvPGLTNcvjg= codeberg.org/gruf/go-structr v0.6.2 h1:1zs7UkPBsRGRDMHhrfFL7GrwAyPHxFXCchu8ADv/zuM=
codeberg.org/gruf/go-structr v0.6.0/go.mod h1:K1FXkUyO6N/JKt8aWqyQ8rtW7Z9ZmXKWP8mFAQ2OJjE= codeberg.org/gruf/go-structr v0.6.2/go.mod h1:K1FXkUyO6N/JKt8aWqyQ8rtW7Z9ZmXKWP8mFAQ2OJjE=
codeberg.org/superseriousbusiness/exif-terminator v0.7.0 h1:Y6VApSXhKqExG0H2hZ2JelRK4xmWdjDQjn13CpEfzko= codeberg.org/superseriousbusiness/exif-terminator v0.7.0 h1:Y6VApSXhKqExG0H2hZ2JelRK4xmWdjDQjn13CpEfzko=
codeberg.org/superseriousbusiness/exif-terminator v0.7.0/go.mod h1:gCWKduudUWFzsnixoMzu0FYVdxHWG+AbXnZ50DqxsUE= codeberg.org/superseriousbusiness/exif-terminator v0.7.0/go.mod h1:gCWKduudUWFzsnixoMzu0FYVdxHWG+AbXnZ50DqxsUE=
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=

View file

@ -255,18 +255,21 @@ func (c *Cache[T]) LoadOne(index *Index, key Key, load func() (T, error)) (T, er
item := index.get_one(key) item := index.get_one(key)
if ok = (item != nil); ok { if ok = (item != nil); ok {
if value, is := item.data.(T); is { var is bool
if val, is = item.data.(T); is {
// Set value COPY. // Set value COPY.
val = c.copy(value) val = c.copy(val)
// Push to front of LRU list, USING // Push to front of LRU list, USING
// THE ITEM'S LRU ENTRY, NOT THE // THE ITEM'S LRU ENTRY, NOT THE
// INDEX KEY ENTRY. VERY IMPORTANT!! // INDEX KEY ENTRY. VERY IMPORTANT!!
c.lru.move_front(&item.elem) c.lru.move_front(&item.elem)
} else if error, is := item.data.(error); is { } else {
// Return error.
err = error // Attempt to return error.
err, _ = item.data.(error)
} }
} }
@ -423,10 +426,10 @@ func (c *Cache[T]) Invalidate(index *Index, keys ...Key) {
// Preallocate expected ret slice. // Preallocate expected ret slice.
values := make([]T, 0, len(keys)) values := make([]T, 0, len(keys))
for i := range keys { for _, key := range keys {
// Delete all items under key from index, collecting // Delete all items under key from index, collecting
// value items and dropping them from all their indices. // value items and dropping them from all their indices.
index.delete(keys[i], func(item *indexed_item) { index.delete(key, func(item *indexed_item) {
if value, ok := item.data.(T); ok { if value, ok := item.data.(T); ok {
// No need to copy, as item // No need to copy, as item
@ -524,6 +527,9 @@ func (c *Cache[T]) store_value(index *Index, key Key, value T) {
index.append(key, item) index.append(key, item)
} }
// Get ptr to value data.
ptr := unsafe.Pointer(&value)
// Acquire key buf. // Acquire key buf.
buf := new_buffer() buf := new_buffer()
@ -538,7 +544,10 @@ func (c *Cache[T]) store_value(index *Index, key Key, value T) {
} }
// Extract fields comprising index key. // Extract fields comprising index key.
parts := extract_fields(value, idx.fields) parts := extract_fields(ptr, idx.fields)
if parts == nil {
continue
}
// Calculate index key. // Calculate index key.
key := idx.key(buf, parts) key := idx.key(buf, parts)

View file

@ -19,17 +19,15 @@ type IndexConfig struct {
// be specified using periods. An example: // be specified using periods. An example:
// "Username,Favorites.Color" // "Username,Favorites.Color"
// //
// Field types supported include: // Note that nested fields where the nested
// - ~int // struct field is a ptr are supported, but
// - ~int8 // nil ptr values in nesting will result in
// - ~int16 // that particular value NOT being indexed.
// - ~int32 // e.g. with "Favorites.Color" if *Favorites
// - ~int64 // is nil then it will not be indexed.
// - ~float32 //
// - ~float64 // Field types supported include any of those
// - ~string // supported by the `go-mangler` library.
// - slices of above
// - ptrs of above
Fields string Fields string
// Multiple indicates whether to accept multiple // Multiple indicates whether to accept multiple
@ -124,7 +122,6 @@ func (i *Index) init(t reflect.Type, cfg IndexConfig, cap int) {
case t.Kind() == reflect.Struct: case t.Kind() == reflect.Struct:
case t.Kind() == reflect.Pointer && case t.Kind() == reflect.Pointer &&
t.Elem().Kind() == reflect.Struct: t.Elem().Kind() == reflect.Struct:
t = t.Elem()
default: default:
panic("index only support struct{} and *struct{}") panic("index only support struct{} and *struct{}")
} }
@ -146,10 +143,10 @@ func (i *Index) init(t reflect.Type, cfg IndexConfig, cap int) {
// Preallocate expected struct field slice. // Preallocate expected struct field slice.
i.fields = make([]struct_field, len(fields)) i.fields = make([]struct_field, len(fields))
for x, name := range fields {
for x, fieldName := range fields {
// Split name to account for nesting. // Split name to account for nesting.
names := strings.Split(fieldName, ".") names := strings.Split(name, ".")
// Look for usable struct field. // Look for usable struct field.
i.fields[x] = find_field(t, names) i.fields[x] = find_field(t, names)
@ -214,23 +211,23 @@ func (i *Index) get(key Key, hook func(*indexed_item)) {
func (i *Index) key(buf *byteutil.Buffer, parts []any) Key { func (i *Index) key(buf *byteutil.Buffer, parts []any) Key {
if len(parts) != len(i.fields) { if len(parts) != len(i.fields) {
panicf("incorrect number key parts: want=%d received=%d", panicf("incorrect number key parts: want=%d received=%d",
len(parts),
len(i.fields), len(i.fields),
len(parts),
) )
} }
buf.B = buf.B[:0] buf.B = buf.B[:0]
if !allow_zero(i.flags) { if !allow_zero(i.flags) {
for x := range parts { for x, field := range i.fields {
before := len(buf.B) before := len(buf.B)
buf.B = i.fields[x].mangle(buf.B, parts[x]) buf.B = field.mangle(buf.B, parts[x])
if string(buf.B[before:]) == i.fields[x].zero { if string(buf.B[before:]) == field.zero {
return Key{} return Key{}
} }
buf.B = append(buf.B, '.') buf.B = append(buf.B, '.')
} }
} else { } else {
for x := range parts { for x, field := range i.fields {
buf.B = i.fields[x].mangle(buf.B, parts[x]) buf.B = field.mangle(buf.B, parts[x])
buf.B = append(buf.B, '.') buf.B = append(buf.B, '.')
} }
} }

View file

@ -262,6 +262,9 @@ func (q *Queue[T]) index(value T) *indexed_item {
// Set item value. // Set item value.
item.data = value item.data = value
// Get ptr to value data.
ptr := unsafe.Pointer(&value)
// Acquire key buf. // Acquire key buf.
buf := new_buffer() buf := new_buffer()
@ -270,7 +273,10 @@ func (q *Queue[T]) index(value T) *indexed_item {
idx := &(q.indices[i]) idx := &(q.indices[i])
// Extract fields comprising index key. // Extract fields comprising index key.
parts := extract_fields(value, idx.fields) parts := extract_fields(ptr, idx.fields)
if parts == nil {
continue
}
// Calculate index key. // Calculate index key.
key := idx.key(buf, parts) key := idx.key(buf, parts)

View file

@ -16,15 +16,14 @@ import (
// including memory offset and hash function. // including memory offset and hash function.
type struct_field struct { type struct_field struct {
// type2 is the runtime type pointer // type2 contains the reflect2
// underlying the struct field type. // type information for this field,
// used for repacking our own erfaces. // used in repacking it as eface.
type2 reflect2.Type type2 reflect2.Type
// offset is the offset in memory // offsets defines whereabouts in
// of this struct field from the // memory this field is located.
// outer-most value ptr location. offsets []next_offset
offset uintptr
// struct field type mangling // struct field type mangling
// (i.e. fast serializing) fn. // (i.e. fast serializing) fn.
@ -36,6 +35,15 @@ type struct_field struct {
zero string zero string
} }
// next_offset defines a next offset location
// in a struct_field, first by the number of
// derefences required, then by offset from
// that final memory location.
type next_offset struct {
derefs uint
offset uintptr
}
// find_field will search for a struct field with given set of names, // find_field will search for a struct field with given set of names,
// where names is a len > 0 slice of names account for struct nesting. // where names is a len > 0 slice of names account for struct nesting.
func find_field(t reflect.Type, names []string) (sfield struct_field) { func find_field(t reflect.Type, names []string) (sfield struct_field) {
@ -64,24 +72,33 @@ func find_field(t reflect.Type, names []string) (sfield struct_field) {
) )
for len(names) > 0 { for len(names) > 0 {
var ok bool
// Pop next name. // Pop next name.
name := pop_name() name := pop_name()
var off next_offset
// Dereference any ptrs to struct.
for t.Kind() == reflect.Pointer {
t = t.Elem()
off.derefs++
}
// Check for valid struct type. // Check for valid struct type.
if t.Kind() != reflect.Struct { if t.Kind() != reflect.Struct {
panicf("field %s is not struct: %s", t, name) panicf("field %s is not struct (or ptr-to): %s", t, name)
} }
var ok bool
// Look for next field by name. // Look for next field by name.
field, ok = t.FieldByName(name) field, ok = t.FieldByName(name)
if !ok { if !ok {
panicf("unknown field: %s", name) panicf("unknown field: %s", name)
} }
// Increment total field offset. // Set next offset value.
sfield.offset += field.Offset off.offset = field.Offset
sfield.offsets = append(sfield.offsets, off)
// Set the next type. // Set the next type.
t = field.Type t = field.Type
@ -89,36 +106,57 @@ func find_field(t reflect.Type, names []string) (sfield struct_field) {
// Get field type as reflect2. // Get field type as reflect2.
sfield.type2 = reflect2.Type2(t) sfield.type2 = reflect2.Type2(t)
i := sfield.type2.New()
// Find mangler for field type. // Find mangler for field type.
sfield.mangle = mangler.Get(t) sfield.mangle = mangler.Get(t)
// Set possible mangled zero value.
sfield.zero = string(sfield.mangle(nil, i))
return return
} }
// extract_fields extracts given structfields from the provided value type, // extract_fields extracts given structfields from the provided value type,
// this is done using predetermined struct field memory offset locations. // this is done using predetermined struct field memory offset locations.
func extract_fields[T any](value T, fields []struct_field) []any { func extract_fields(ptr unsafe.Pointer, fields []struct_field) []any {
// Get ptr to raw value data.
ptr := unsafe.Pointer(&value)
// If this is a pointer type deref the value ptr.
if reflect.TypeOf(value).Kind() == reflect.Pointer {
ptr = *(*unsafe.Pointer)(ptr)
}
// Prepare slice of field ifaces. // Prepare slice of field ifaces.
ifaces := make([]any, len(fields)) ifaces := make([]any, len(fields))
for i, field := range fields {
for i := 0; i < len(fields); i++ { // loop scope.
// Manually access field at memory offset and pack eface. fptr := ptr
ptr := unsafe.Pointer(uintptr(ptr) + fields[i].offset)
ifaces[i] = fields[i].type2.UnsafeIndirect(ptr) for _, offset := range field.offsets {
// Dereference any ptrs to offset.
fptr = deref(fptr, offset.derefs)
if fptr == nil {
return nil
}
// Jump forward by offset to next ptr.
fptr = unsafe.Pointer(uintptr(fptr) +
offset.offset)
}
// Repack value data ptr as empty interface.
ifaces[i] = field.type2.UnsafeIndirect(fptr)
} }
return ifaces return ifaces
} }
// deref will dereference ptr 'n' times (or until nil).
func deref(p unsafe.Pointer, n uint) unsafe.Pointer {
for ; n > 0; n-- {
if p == nil {
return nil
}
p = *(*unsafe.Pointer)(p)
}
return p
}
// panicf provides a panic with string formatting. // panicf provides a panic with string formatting.
func panicf(format string, args ...any) { func panicf(format string, args ...any) {
panic(fmt.Sprintf(format, args...)) panic(fmt.Sprintf(format, args...))

2
vendor/modules.txt vendored
View file

@ -59,7 +59,7 @@ codeberg.org/gruf/go-sched
## explicit; go 1.19 ## explicit; go 1.19
codeberg.org/gruf/go-store/v2/storage codeberg.org/gruf/go-store/v2/storage
codeberg.org/gruf/go-store/v2/util codeberg.org/gruf/go-store/v2/util
# codeberg.org/gruf/go-structr v0.6.0 # codeberg.org/gruf/go-structr v0.6.2
## explicit; go 1.21 ## explicit; go 1.21
codeberg.org/gruf/go-structr codeberg.org/gruf/go-structr
# codeberg.org/superseriousbusiness/exif-terminator v0.7.0 # codeberg.org/superseriousbusiness/exif-terminator v0.7.0