Skip to content

Commit 123714d

Browse files
committed
removed "top" var from allocator
not needed as slice tracks this already
1 parent 10450a1 commit 123714d

File tree

1 file changed

+5
-9
lines changed

1 file changed

+5
-9
lines changed

alloc.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ func init() {
2626

2727
// allocator is a fast bulk memory allocator for the LValue.
2828
type allocator struct {
29-
top int
3029
size int
3130
fptrs []float64
3231
fheader *reflect.SliceHeader
@@ -37,9 +36,8 @@ type allocator struct {
3736

3837
func newAllocator(size int) *allocator {
3938
al := &allocator{
40-
top: 0,
4139
size: size,
42-
fptrs: make([]float64, size),
40+
fptrs: make([]float64, 0, size),
4341
fheader: nil,
4442
}
4543
al.fheader = (*reflect.SliceHeader)(unsafe.Pointer(&al.fptrs))
@@ -63,16 +61,14 @@ func (al *allocator) LNumber2I(v LNumber) LValue {
6361
}
6462

6563
// check if we need a new alloc page
66-
if al.top == len(al.fptrs)-1 {
67-
al.top = 0
68-
al.fptrs = make([]float64, al.size)
64+
if cap(al.fptrs) == len(al.fptrs) {
65+
al.fptrs = make([]float64, 0, al.size)
6966
al.fheader = (*reflect.SliceHeader)(unsafe.Pointer(&al.fptrs))
7067
}
7168

7269
// alloc a new float, and store our value into it
73-
fptr := (*float64)(unsafe.Pointer(al.fheader.Data + uintptr(al.top)*unsafe.Sizeof(_fv)))
74-
al.top++
75-
*fptr = float64(v)
70+
al.fptrs = append(al.fptrs, float64(v))
71+
fptr := (*float64)(unsafe.Pointer(al.fheader.Data + uintptr(len(al.fptrs)-1)*unsafe.Sizeof(_fv)))
7672

7773
// hack our scratch LValue to point to our allocated value
7874
// this scratch lvalue is copied when this function returns meaning the scratch value can be reused

0 commit comments

Comments
 (0)