• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mlange-42 / ark / 14191219880

01 Apr 2025 08:44AM CUT coverage: 99.832%. Remained the same
14191219880

push

github

web-flow
Add awesome-go badge (#229)

8298 of 8312 relevant lines covered (99.83%)

20284.26 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/ecs/column.go
1
package ecs
2

3
import (
4
        "reflect"
5
        "unsafe"
6
)
7

8
// column storage for components in an table.
9
type column struct {
10
        pointer    unsafe.Pointer // pointer to the first element
11
        data       reflect.Value  // data buffer
12
        itemSize   uintptr        // memory size of items
13
        target     Entity         // target entity if for a relation component
14
        index      uint32         // index of the column in the containing table
15
        isRelation bool           // whether this column is for a relation component
16
}
17

18
// newColumn creates a new column for a given type and capacity.
19
func newColumn(index uint32, tp reflect.Type, itemSize uintptr, isRelation bool, target Entity, capacity uint32) column {
2,373✔
20
        // TODO: should be use a slice instead of an array here?
2,373✔
21
        data := reflect.New(reflect.ArrayOf(int(capacity), tp)).Elem()
2,373✔
22
        pointer := data.Addr().UnsafePointer()
2,373✔
23

2,373✔
24
        return column{
2,373✔
25
                index:      index,
2,373✔
26
                data:       data,
2,373✔
27
                pointer:    pointer,
2,373✔
28
                itemSize:   itemSize,
2,373✔
29
                isRelation: isRelation,
2,373✔
30
                target:     target,
2,373✔
31
        }
2,373✔
32
}
2,373✔
33

34
// Get returns a pointer to the component at the given index.
35
func (c *column) Get(index uintptr) unsafe.Pointer {
2,016,607✔
36
        return unsafe.Add(c.pointer, index*c.itemSize)
2,016,607✔
37
}
2,016,607✔
38

39
func (c *column) SetLast(other *column, ownLen uint32, count uint32) {
463✔
40
        start := ownLen - count
463✔
41
        src := other.Get(0)
463✔
42
        dst := c.Get(uintptr(start))
463✔
43
        copyPtr(src, dst, c.itemSize*uintptr(count))
463✔
44
}
463✔
45

46
// Set overwrites the component at the given index.
47
func (c *column) Set(index uint32, comp unsafe.Pointer) unsafe.Pointer {
883✔
48
        dst := c.Get(uintptr(index))
883✔
49
        if c.itemSize == 0 {
939✔
50
                return dst
56✔
51
        }
56✔
52

53
        copyPtr(comp, dst, uintptr(c.itemSize))
827✔
54
        return dst
827✔
55
}
56

57
// Zero resets the memory at the given index.
58
func (c *column) Zero(index uintptr, zero unsafe.Pointer) {
498,420✔
59
        if c.itemSize == 0 {
498,649✔
60
                return
229✔
61
        }
229✔
62
        dst := unsafe.Add(c.pointer, index*c.itemSize)
498,191✔
63
        copyPtr(zero, dst, uintptr(c.itemSize))
498,191✔
64
}
65

66
// Zero resets a block of storage in one buffer.
67
func (c *column) ZeroRange(start, len uint32, zero unsafe.Pointer) {
876✔
68
        size := uint32(c.itemSize)
876✔
69
        if size == 0 {
907✔
70
                return
31✔
71
        }
31✔
72
        var i uint32
845✔
73
        for i = 0; i < len; i++ {
11,934✔
74
                dst := unsafe.Add(c.pointer, (i+start)*size)
11,089✔
75
                copyPtr(zero, dst, c.itemSize)
11,089✔
76
        }
11,089✔
77
}
78

79
func (c *column) Reset(ownLen uint32, zero unsafe.Pointer) {
889✔
80
        if ownLen == 0 {
892✔
81
                return
3✔
82
        }
3✔
83
        if ownLen <= 64 { // A coarse estimate where manually zeroing is faster
1,762✔
84
                c.ZeroRange(0, ownLen, zero)
876✔
85
        } else {
886✔
86
                c.data.SetZero()
10✔
87
        }
10✔
88
}
89

90
// entityColumn storage for entities in an table.
91
type entityColumn struct {
92
        pointer unsafe.Pointer // pointer to the first element
93
        data    reflect.Value  // data buffer
94
}
95

96
// newColumn creates a new column for a given type and capacity.
97
func newEntityColumn(capacity uint32) entityColumn {
826✔
98
        // TODO: should be use a slice instead of an array here?
826✔
99
        data := reflect.New(reflect.ArrayOf(int(capacity), entityType)).Elem()
826✔
100
        pointer := data.Addr().UnsafePointer()
826✔
101

826✔
102
        return entityColumn{
826✔
103
                data:    data,
826✔
104
                pointer: pointer,
826✔
105
        }
826✔
106
}
826✔
107

108
// Get returns a pointer to the component at the given index.
109
func (c *entityColumn) Get(index uintptr) unsafe.Pointer {
2,972,665✔
110
        return unsafe.Add(c.pointer, index*entitySize)
2,972,665✔
111
}
2,972,665✔
112

113
func (c *entityColumn) SetLast(other *entityColumn, ownLen uint32, count uint32) {
215✔
114
        start := ownLen - count
215✔
115
        src := other.Get(0)
215✔
116
        dst := c.Get(uintptr(start))
215✔
117
        copyPtr(src, dst, entitySize*uintptr(count))
215✔
118
}
215✔
119

120
// Set overwrites the component at the given index.
121
func (c *entityColumn) Set(index uint32, comp unsafe.Pointer) unsafe.Pointer {
499,779✔
122
        dst := c.Get(uintptr(index))
499,779✔
123

499,779✔
124
        copyPtr(comp, dst, entitySize)
499,779✔
125
        return dst
499,779✔
126
}
499,779✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc