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

mlange-42 / ark / 15075416534

16 May 2025 06:54PM CUT coverage: 99.833%. Remained the same
15075416534

Pull #251

github

web-flow
Merge a3a4140a2 into 94436e9e0
Pull Request #251: Prepare release v0.4.3

8352 of 8366 relevant lines covered (99.83%)

18973.76 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 {
7,380✔
20
        // TODO: should we use a slice instead of an array here?
7,380✔
21
        data := reflect.New(reflect.ArrayOf(int(capacity), tp)).Elem()
7,380✔
22
        pointer := data.Addr().UnsafePointer()
7,380✔
23

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

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

39
func (c *column) SetLast(other *column, ownLen uint32, count uint32, isTrivial bool) {
464✔
40
        start := ownLen - count
464✔
41
        if isTrivial {
927✔
42
                src := other.Get(0)
463✔
43
                dst := c.Get(uintptr(start))
463✔
44
                copyPtr(src, dst, c.itemSize*uintptr(count))
463✔
45
                return
463✔
46
        }
463✔
47
        copyRange(other.data, c.data, int(start), int(count))
1✔
48
}
49

50
// Set overwrites the component at the given index.
51
func (c *column) Set(index uint32, src *column, srcIndex int, isTrivial bool) {
4,883✔
52
        if c.itemSize == 0 {
4,939✔
53
                return
56✔
54
        }
56✔
55
        if isTrivial {
7,654✔
56
                comp := src.Get(uintptr(srcIndex))
2,827✔
57
                dst := c.Get(uintptr(index))
2,827✔
58
                copyPtr(comp, dst, c.itemSize)
2,827✔
59
                return
2,827✔
60
        }
2,827✔
61
        copyItem(src.data, c.data, srcIndex, int(index))
2,000✔
62
}
63

64
// Zero resets the memory at the given index.
65
func (c *column) Zero(index uintptr, zero unsafe.Pointer) {
519,702✔
66
        if c.itemSize == 0 {
519,931✔
67
                return
229✔
68
        }
229✔
69
        dst := unsafe.Add(c.pointer, index*c.itemSize)
519,473✔
70
        copyPtr(zero, dst, uintptr(c.itemSize))
519,473✔
71
}
72

73
// Zero resets a block of storage in one buffer.
74
func (c *column) ZeroRange(start, len uint32, zero unsafe.Pointer) {
877✔
75
        size := uint32(c.itemSize)
877✔
76
        if size == 0 {
908✔
77
                return
31✔
78
        }
31✔
79
        var i uint32
846✔
80
        for i = range len {
11,947✔
81
                dst := unsafe.Add(c.pointer, (i+start)*size)
11,101✔
82
                copyPtr(zero, dst, c.itemSize)
11,101✔
83
        }
11,101✔
84
}
85

86
func (c *column) Reset(ownLen uint32, zero unsafe.Pointer) {
890✔
87
        if ownLen == 0 {
893✔
88
                return
3✔
89
        }
3✔
90
        if ownLen <= 64 { // A coarse estimate where manually zeroing is faster
1,764✔
91
                c.ZeroRange(0, ownLen, zero)
877✔
92
        } else {
887✔
93
                c.data.SetZero()
10✔
94
        }
10✔
95
}
96

97
// entityColumn storage for entities in an table.
98
type entityColumn struct {
99
        pointer unsafe.Pointer // pointer to the first element
100
        data    reflect.Value  // data buffer
101
}
102

103
// newColumn creates a new column for a given type and capacity.
104
func newEntityColumn(capacity uint32) entityColumn {
2,835✔
105
        // TODO: should we use a slice instead of an array here?
2,835✔
106
        data := reflect.New(reflect.ArrayOf(int(capacity), entityType)).Elem()
2,835✔
107
        pointer := data.Addr().UnsafePointer()
2,835✔
108

2,835✔
109
        return entityColumn{
2,835✔
110
                data:    data,
2,835✔
111
                pointer: pointer,
2,835✔
112
        }
2,835✔
113
}
2,835✔
114

115
// Get returns a pointer to the component at the given index.
116
func (c *entityColumn) Get(index uintptr) unsafe.Pointer {
2,059,953✔
117
        return unsafe.Add(c.pointer, index*entitySize)
2,059,953✔
118
}
2,059,953✔
119

120
func (c *entityColumn) SetLast(other *entityColumn, ownLen uint32, count uint32) {
216✔
121
        start := ownLen - count
216✔
122
        copyRange(other.data, c.data, int(start), int(count))
216✔
123
}
216✔
124

125
// Set overwrites the component at the given index.
126
func (c *entityColumn) Set(index uint32, comp unsafe.Pointer) unsafe.Pointer {
522,019✔
127
        dst := c.Get(uintptr(index))
522,019✔
128

522,019✔
129
        copyPtr(comp, dst, entitySize)
522,019✔
130
        return dst
522,019✔
131
}
522,019✔
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