Skip to content

Commit cce8292

Browse files
committed
reduce array transform allocations in block dispatch loops
1 parent 4aaf35b commit cce8292

2 files changed

Lines changed: 50 additions & 16 deletions

File tree

vibes/execution_members_array_grouping.go

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,16 @@ func arrayMemberGrouping(property string) (Value, error) {
1515
arr := receiver.Array()
1616
out := make([]Value, len(arr))
1717
copy(out, arr)
18+
var comparatorArgs [2]Value
1819
var sortErr error
1920
sort.SliceStable(out, func(i, j int) bool {
2021
if sortErr != nil {
2122
return false
2223
}
2324
if block.Block() != nil {
24-
cmpValue, err := exec.CallBlock(block, []Value{out[i], out[j]})
25+
comparatorArgs[0] = out[i]
26+
comparatorArgs[1] = out[j]
27+
cmpValue, err := exec.CallBlock(block, comparatorArgs[:])
2528
if err != nil {
2629
sortErr = err
2730
return false
@@ -60,8 +63,10 @@ func arrayMemberGrouping(property string) (Value, error) {
6063
}
6164
arr := receiver.Array()
6265
withKeys := make([]itemWithSortKey, len(arr))
66+
var blockArg [1]Value
6367
for i, item := range arr {
64-
sortKey, err := exec.CallBlock(block, []Value{item})
68+
blockArg[0] = item
69+
sortKey, err := exec.CallBlock(block, blockArg[:])
6570
if err != nil {
6671
return NewNil(), err
6772
}
@@ -102,8 +107,10 @@ func arrayMemberGrouping(property string) (Value, error) {
102107
arr := receiver.Array()
103108
left := make([]Value, 0, len(arr))
104109
right := make([]Value, 0, len(arr))
110+
var blockArg [1]Value
105111
for _, item := range arr {
106-
match, err := exec.CallBlock(block, []Value{item})
112+
blockArg[0] = item
113+
match, err := exec.CallBlock(block, blockArg[:])
107114
if err != nil {
108115
return NewNil(), err
109116
}
@@ -125,8 +132,10 @@ func arrayMemberGrouping(property string) (Value, error) {
125132
}
126133
arr := receiver.Array()
127134
groups := make(map[string][]Value, len(arr))
135+
var blockArg [1]Value
128136
for _, item := range arr {
129-
groupValue, err := exec.CallBlock(block, []Value{item})
137+
blockArg[0] = item
138+
groupValue, err := exec.CallBlock(block, blockArg[:])
130139
if err != nil {
131140
return NewNil(), err
132141
}
@@ -154,8 +163,10 @@ func arrayMemberGrouping(property string) (Value, error) {
154163
order := make([]string, 0, len(arr))
155164
keyValues := make(map[string]Value, len(arr))
156165
groups := make(map[string][]Value, len(arr))
166+
var blockArg [1]Value
157167
for _, item := range arr {
158-
groupValue, err := exec.CallBlock(block, []Value{item})
168+
blockArg[0] = item
169+
groupValue, err := exec.CallBlock(block, blockArg[:])
159170
if err != nil {
160171
return NewNil(), err
161172
}
@@ -186,10 +197,12 @@ func arrayMemberGrouping(property string) (Value, error) {
186197
}
187198
arr := receiver.Array()
188199
counts := make(map[string]int64, len(arr))
200+
var blockArg [1]Value
189201
for _, item := range arr {
190202
keyValue := item
191203
if block.Block() != nil {
192-
mapped, err := exec.CallBlock(block, []Value{item})
204+
blockArg[0] = item
205+
mapped, err := exec.CallBlock(block, blockArg[:])
193206
if err != nil {
194207
return NewNil(), err
195208
}

vibes/execution_members_array_query.go

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@ func arrayMemberQuery(property string) (Value, error) {
1616
if err := ensureBlock(block, "array.each"); err != nil {
1717
return NewNil(), err
1818
}
19+
var blockArg [1]Value
1920
for _, item := range receiver.Array() {
20-
if _, err := exec.CallBlock(block, []Value{item}); err != nil {
21+
blockArg[0] = item
22+
if _, err := exec.CallBlock(block, blockArg[:]); err != nil {
2123
return NewNil(), err
2224
}
2325
}
@@ -30,8 +32,10 @@ func arrayMemberQuery(property string) (Value, error) {
3032
}
3133
arr := receiver.Array()
3234
result := make([]Value, len(arr))
35+
var blockArg [1]Value
3336
for i, item := range arr {
34-
val, err := exec.CallBlock(block, []Value{item})
37+
blockArg[0] = item
38+
val, err := exec.CallBlock(block, blockArg[:])
3539
if err != nil {
3640
return NewNil(), err
3741
}
@@ -46,8 +50,10 @@ func arrayMemberQuery(property string) (Value, error) {
4650
}
4751
arr := receiver.Array()
4852
out := make([]Value, 0, len(arr))
53+
var blockArg [1]Value
4954
for _, item := range arr {
50-
val, err := exec.CallBlock(block, []Value{item})
55+
blockArg[0] = item
56+
val, err := exec.CallBlock(block, blockArg[:])
5157
if err != nil {
5258
return NewNil(), err
5359
}
@@ -65,8 +71,10 @@ func arrayMemberQuery(property string) (Value, error) {
6571
if err := ensureBlock(block, "array.find"); err != nil {
6672
return NewNil(), err
6773
}
74+
var blockArg [1]Value
6875
for _, item := range receiver.Array() {
69-
match, err := exec.CallBlock(block, []Value{item})
76+
blockArg[0] = item
77+
match, err := exec.CallBlock(block, blockArg[:])
7078
if err != nil {
7179
return NewNil(), err
7280
}
@@ -84,8 +92,10 @@ func arrayMemberQuery(property string) (Value, error) {
8492
if err := ensureBlock(block, "array.find_index"); err != nil {
8593
return NewNil(), err
8694
}
95+
var blockArg [1]Value
8796
for idx, item := range receiver.Array() {
88-
match, err := exec.CallBlock(block, []Value{item})
97+
blockArg[0] = item
98+
match, err := exec.CallBlock(block, blockArg[:])
8999
if err != nil {
90100
return NewNil(), err
91101
}
@@ -115,8 +125,11 @@ func arrayMemberQuery(property string) (Value, error) {
115125
acc = arr[0]
116126
start = 1
117127
}
128+
var blockArgs [2]Value
118129
for i := start; i < len(arr); i++ {
119-
next, err := exec.CallBlock(block, []Value{acc, arr[i]})
130+
blockArgs[0] = acc
131+
blockArgs[1] = arr[i]
132+
next, err := exec.CallBlock(block, blockArgs[:])
120133
if err != nil {
121134
return NewNil(), err
122135
}
@@ -212,8 +225,10 @@ func arrayMemberQuery(property string) (Value, error) {
212225
return NewInt(int64(len(arr))), nil
213226
}
214227
total := int64(0)
228+
var blockArg [1]Value
215229
for _, item := range arr {
216-
include, err := exec.CallBlock(block, []Value{item})
230+
blockArg[0] = item
231+
include, err := exec.CallBlock(block, blockArg[:])
217232
if err != nil {
218233
return NewNil(), err
219234
}
@@ -228,9 +243,11 @@ func arrayMemberQuery(property string) (Value, error) {
228243
if len(args) > 0 {
229244
return NewNil(), fmt.Errorf("array.any? does not take arguments")
230245
}
246+
var blockArg [1]Value
231247
for _, item := range receiver.Array() {
232248
if block.Block() != nil {
233-
val, err := exec.CallBlock(block, []Value{item})
249+
blockArg[0] = item
250+
val, err := exec.CallBlock(block, blockArg[:])
234251
if err != nil {
235252
return NewNil(), err
236253
}
@@ -250,9 +267,11 @@ func arrayMemberQuery(property string) (Value, error) {
250267
if len(args) > 0 {
251268
return NewNil(), fmt.Errorf("array.all? does not take arguments")
252269
}
270+
var blockArg [1]Value
253271
for _, item := range receiver.Array() {
254272
if block.Block() != nil {
255-
val, err := exec.CallBlock(block, []Value{item})
273+
blockArg[0] = item
274+
val, err := exec.CallBlock(block, blockArg[:])
256275
if err != nil {
257276
return NewNil(), err
258277
}
@@ -272,9 +291,11 @@ func arrayMemberQuery(property string) (Value, error) {
272291
if len(args) > 0 {
273292
return NewNil(), fmt.Errorf("array.none? does not take arguments")
274293
}
294+
var blockArg [1]Value
275295
for _, item := range receiver.Array() {
276296
if block.Block() != nil {
277-
val, err := exec.CallBlock(block, []Value{item})
297+
blockArg[0] = item
298+
val, err := exec.CallBlock(block, blockArg[:])
278299
if err != nil {
279300
return NewNil(), err
280301
}

0 commit comments

Comments
 (0)