Skip to content

Commit 4abf91e

Browse files
rvcaswolf31o2
andauthored
feat: use less memory for builtin arguments (#103)
* feat: try to use less memory for builtin arguments * fix(lint): hoist nil check to for loop Signed-off-by: Chris Gianelloni <[email protected]> --------- Signed-off-by: Chris Gianelloni <[email protected]> Co-authored-by: Chris Gianelloni <[email protected]>
1 parent 668ed5a commit 4abf91e

File tree

5 files changed

+423
-179
lines changed

5 files changed

+423
-179
lines changed

builtin/default_function.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ var Builtins map[string]DefaultFunction = map[string]DefaultFunction{
253253
"indexArray": IndexArray,
254254
}
255255

256-
var defaultFunctionForceCount = [TotalBuiltinCount]int{
256+
var defaultFunctionForceCount = [TotalBuiltinCount]uint{
257257
// Integer functions
258258
AddInteger: 0,
259259
SubtractInteger: 0,
@@ -369,11 +369,11 @@ var defaultFunctionForceCount = [TotalBuiltinCount]int{
369369
IndexArray: 1,
370370
}
371371

372-
func (f DefaultFunction) ForceCount() int {
372+
func (f DefaultFunction) ForceCount() uint {
373373
return defaultFunctionForceCount[f]
374374
}
375375

376-
var defaultFunctionArity = [TotalBuiltinCount]int{
376+
var defaultFunctionArity = [TotalBuiltinCount]uint{
377377
// Integer functions
378378
AddInteger: 2,
379379
SubtractInteger: 2,
@@ -489,7 +489,7 @@ var defaultFunctionArity = [TotalBuiltinCount]int{
489489
IndexArray: 2,
490490
}
491491

492-
func (f DefaultFunction) Arity() int {
492+
func (f DefaultFunction) Arity() uint {
493493
return defaultFunctionArity[f]
494494
}
495495

cek/builtin_args.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package cek
2+
3+
import (
4+
"iter"
5+
6+
"github.com/blinklabs-io/plutigo/syn"
7+
)
8+
9+
type argHolder[T syn.Eval] [6]Value[T]
10+
11+
func newArgHolder[T syn.Eval]() argHolder[T] {
12+
return argHolder[T]([6]Value[T]{})
13+
}
14+
15+
type BuiltinArgs[T syn.Eval] struct {
16+
data Value[T]
17+
next *BuiltinArgs[T]
18+
}
19+
20+
func (b *BuiltinArgs[T]) Iter() iter.Seq[Value[T]] {
21+
return func(yield func(Value[T]) bool) {
22+
temp := b
23+
acc := []Value[T]{}
24+
25+
for temp != nil {
26+
27+
acc = append(acc, temp.data)
28+
29+
temp = temp.next
30+
}
31+
32+
for i := len(acc) - 1; i >= 0; i-- {
33+
if !yield(acc[i]) {
34+
return
35+
}
36+
}
37+
}
38+
}
39+
40+
func (b *BuiltinArgs[T]) Extend(data Value[T]) *BuiltinArgs[T] {
41+
return &BuiltinArgs[T]{
42+
data: data,
43+
next: b,
44+
}
45+
}
46+
47+
func (b *BuiltinArgs[T]) Extract(holder *argHolder[T], count uint) {
48+
temp := b
49+
50+
for temp != nil {
51+
52+
holder[count-1] = temp.data
53+
54+
temp = temp.next
55+
count--
56+
}
57+
}

0 commit comments

Comments
 (0)