@@ -26,7 +26,6 @@ func init() {
26
26
27
27
// allocator is a fast bulk memory allocator for the LValue.
28
28
type allocator struct {
29
- top int
30
29
size int
31
30
fptrs []float64
32
31
fheader * reflect.SliceHeader
@@ -37,9 +36,8 @@ type allocator struct {
37
36
38
37
func newAllocator (size int ) * allocator {
39
38
al := & allocator {
40
- top : 0 ,
41
39
size : size ,
42
- fptrs : make ([]float64 , size ),
40
+ fptrs : make ([]float64 , 0 , size ),
43
41
fheader : nil ,
44
42
}
45
43
al .fheader = (* reflect .SliceHeader )(unsafe .Pointer (& al .fptrs ))
@@ -63,16 +61,14 @@ func (al *allocator) LNumber2I(v LNumber) LValue {
63
61
}
64
62
65
63
// 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 )
69
66
al .fheader = (* reflect .SliceHeader )(unsafe .Pointer (& al .fptrs ))
70
67
}
71
68
72
69
// 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 )))
76
72
77
73
// hack our scratch LValue to point to our allocated value
78
74
// this scratch lvalue is copied when this function returns meaning the scratch value can be reused
0 commit comments