package ecsimport ()var nilDummy *struct{} = nil// capPow2 calculated the next power-of-2 capacity value.func capPow2( uint32) uint32 {if == 0 {return1 } -- |= >> 1 |= >> 2 |= >> 4 |= >> 8 |= >> 16return + 1}// get the component for an entity from a component storage.//// Returns nil if the entity does not have the component.func get[ any]( *componentStorage, *entityIndex) * { := .columns[.table]if == nil {returnnil }return (*)(.Get(uintptr(.row)))}// copyPtr copies from one pointer to another.// This is not GC-safe. Use only for trivial/value types.func copyPtr(, unsafe.Pointer, uintptr) { := (*[math.MaxInt32]byte)()[::] := (*[math.MaxInt32]byte)()[::]copy(, )}// copyValue copies an item between two reflect arrays using typedmemmove.// This is GC-safe. Use for non-trivial types.func copyValue(, *column, , uintptr) { := unsafe.Add(.pointer, *.itemSize) := unsafe.Add(.pointer, *.itemSize)typedmemmove(.typePtr, , )}// copyRange copies a range of items from one reflect array to another.// Copies src[:count] to dst[start:].// This is GC-safe. Use for non-trivial types.func copyRange(, *column, , uint32) {if <= 64 {copyRangeSmall(, , uintptr(), uintptr()) } else {copyRangeLarge(, , int(), int()) }}// copyRangeSmall copies a range of items from one reflect array to another.// Copies src[:count] to dst[start:].// This is GC-safe. Use for non-trivial types.//// Should be used for small ranges (<=64).func copyRangeSmall(, *column, , uintptr) { := .itemSize := unsafe.Add(.pointer, *) := .pointerforrange {typedmemmove(.typePtr, , ) = unsafe.Add(, ) = unsafe.Add(, ) }}// copyRangeLarge copies a range of items from one reflect array to another.// Copies src[:count] to dst[start:].// This is GC-safe. Use for non-trivial types.//// Should be used for large ranges (>64).func copyRangeLarge(, *column, , int) { := .data.Slice(0, ) := .data.Slice(, +)reflect.Copy(, )}//go:linkname memclrNoHeapPointers runtime.memclrNoHeapPointersfunc memclrNoHeapPointers( unsafe.Pointer, uintptr)//go:linkname typedmemclr reflect.typedmemclr//go:noescapefunc typedmemclr( unsafe.Pointer, unsafe.Pointer)//go:linkname typedmemmove runtime.typedmemmove//go:noescapefunc typedmemmove( unsafe.Pointer, , unsafe.Pointer)type ifaceWords struct { typ uintptr data uintptr}//nolint:staticcheck,govetfunc rtypePtr( reflect.Type) unsafe.Pointer {// reflect.Type is interface (type word, data word) in current runtime. // We need the data word, i.e. pointer to runtime type info. := *(*ifaceWords)(unsafe.Pointer(&))returnunsafe.Pointer(.data)}// isRelation determines whether a type is a relation component.func isRelation( reflect.Type) bool {if .Kind() != reflect.Struct || .NumField() == 0 {returnfalse } := .Field(0)return .Type == relationType && .Name == relationType.Name()}// isTrivial checks if a type is "trivial" (contains no pointers, slices, maps, strings, or channels).// It also returns false if the type itself is one of these.func isTrivial( reflect.Type) bool {// Base case: If the type is invalid, return falseif == nil {returnfalse }// Check if the type itself is a pointer, slice, map, or channelswitch .Kind() {//nolint:govetcasereflect.Ptr, reflect.Slice, reflect.Map, reflect.Chan, reflect.Interface, reflect.String:returnfalse }// If it's a struct, check its fields recursivelyif .Kind() == reflect.Struct {for := range .NumField() { := .Field().Typeif !() {returnfalse } } }// If it's an array, check its elements recursivelyif .Kind() == reflect.Array { := .Elem()if !() {returnfalse } }// If none of the above conditions matched, it's trivialreturntrue}// pagedSlice is a paged collection working with pages of length 32 slices.// its primary purpose is pointer persistence, which is not given using simple slices.//// Implements [archetypes].type pagedSlice[ any] struct { pages [][] len int32 lenLast int32 pageSize int32}func newPagedSlice[ any]( int32) pagedSlice[] {returnpagedSlice[]{pageSize: , }}// Add adds a value to the paged slice.func ( *pagedSlice[]) ( ) {if .len == 0 || .lenLast == .pageSize { .pages = append(.pages, make([], .pageSize)) .lenLast = 0 } .pages[len(.pages)-1][.lenLast] = .len++ .lenLast++}// Get returns the value at the given index.func ( *pagedSlice[]) ( int32) * {return &.pages[/.pageSize][%.pageSize]}// Set sets the value at the given index.func ( *pagedSlice[]) ( int32, ) { .pages[/.pageSize][%.pageSize] = }// Len returns the current number of items in the paged slice.func ( *pagedSlice[]) () int32 {return .len}
The pages are generated with Goldsv0.8.7. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.