Skip to content

Commit 7eaae9c

Browse files
authored
perf(cek): apply hot-path tweaks (#278)
Signed-off-by: Chris Gianelloni <wolf31o2@blinklabs.io>
1 parent 5188e28 commit 7eaae9c

2 files changed

Lines changed: 33 additions & 23 deletions

File tree

cek/machine.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -313,12 +313,13 @@ func allocArenaSlot[S any](chunks *[][]S, pos *int, chunkSize int) *S {
313313
if chunkSize <= 0 {
314314
chunkSize = valueColdChunkSize
315315
}
316-
chunkIdx := *pos / chunkSize
316+
posVal := *pos
317+
chunkIdx := posVal / chunkSize
317318
if chunkIdx == len(*chunks) {
318319
*chunks = append(*chunks, make([]S, chunkSize))
319320
}
320-
slot := &(*chunks)[chunkIdx][*pos%chunkSize]
321-
*pos += 1
321+
slot := &(*chunks)[chunkIdx][posVal%chunkSize]
322+
*pos = posVal + 1
322323
return slot
323324
}
324325

cek/stack_machine.go

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,17 @@ func (m *Machine[T]) pushApplyFrames(args []Value[T]) {
7878
}
7979
}
8080

81+
func (m *Machine[T]) pushApplyFrameValue(arg Value[T]) {
82+
frame := m.pushFrameSlot()
83+
frame.kind = frameAwaitFunValue
84+
frame.value = arg
85+
}
86+
87+
func (m *Machine[T]) pushApplyFrames2(first, second Value[T]) {
88+
m.pushApplyFrameValue(second)
89+
m.pushApplyFrameValue(first)
90+
}
91+
8192
func (m *Machine[T]) pushAwaitArgFrame(funValue Value[T]) {
8293
frame := m.pushFrameSlot()
8394
switch f := funValue.(type) {
@@ -981,7 +992,8 @@ func (m *Machine[T]) caseEvaluateStack(
981992
return branches[v.Tag], env, nil, false, nil
982993
case *Constant:
983994
var tag int
984-
var args []Value[T]
995+
var firstArg Value[T]
996+
var secondArg Value[T]
985997
branchRule := 0
986998

987999
switch cval := v.Constant.(type) {
@@ -1027,17 +1039,15 @@ func (m *Machine[T]) caseEvaluateStack(
10271039
tag = 1
10281040
} else {
10291041
tag = 0
1030-
args = m.allocValueElems(2)
1031-
args[0] = m.allocConstant(cval.List[0])
1042+
firstArg = m.allocConstant(cval.List[0])
10321043
tail := m.allocProtoListConstant(cval.LTyp, cval.List[1:])
1033-
args[1] = m.allocConstant(tail)
1044+
secondArg = m.allocConstant(tail)
10341045
}
10351046
case *syn.ProtoPair:
10361047
branchRule = 1
10371048
tag = 0
1038-
args = m.allocValueElems(2)
1039-
args[0] = m.allocConstant(cval.First)
1040-
args[1] = m.allocConstant(cval.Second)
1049+
firstArg = m.allocConstant(cval.First)
1050+
secondArg = m.allocConstant(cval.Second)
10411051
default:
10421052
return nil, nil, nil, false, &TypeError{
10431053
Code: ErrCodeNonConstrScrutinized,
@@ -1069,7 +1079,9 @@ func (m *Machine[T]) caseEvaluateStack(
10691079
}
10701080
}
10711081

1072-
m.pushApplyFrames(args)
1082+
if firstArg != nil {
1083+
m.pushApplyFrames2(firstArg, secondArg)
1084+
}
10731085
return branches[tag], env, nil, false, nil
10741086
case *dataListValue[T]:
10751087
if len(branches) < 1 || len(branches) > 2 {
@@ -1087,10 +1099,10 @@ func (m *Machine[T]) caseEvaluateStack(
10871099
}
10881100
return branches[1], env, nil, false, nil
10891101
}
1090-
args := m.allocValueElems(2)
1091-
args[0] = m.allocDataValue(v.items[0])
1092-
args[1] = m.allocDataListValue(v.items[1:])
1093-
m.pushApplyFrames(args)
1102+
m.pushApplyFrames2(
1103+
m.allocDataValue(v.items[0]),
1104+
m.allocDataListValue(v.items[1:]),
1105+
)
10941106
return branches[0], env, nil, false, nil
10951107
case *dataMapValue[T]:
10961108
if len(branches) < 1 || len(branches) > 2 {
@@ -1108,10 +1120,10 @@ func (m *Machine[T]) caseEvaluateStack(
11081120
}
11091121
return branches[1], env, nil, false, nil
11101122
}
1111-
args := m.allocValueElems(2)
1112-
args[0] = m.allocDataPairValue(v.items[0][0], v.items[0][1])
1113-
args[1] = m.allocDataMapValue(v.items[1:])
1114-
m.pushApplyFrames(args)
1123+
m.pushApplyFrames2(
1124+
m.allocDataPairValue(v.items[0][0], v.items[0][1]),
1125+
m.allocDataMapValue(v.items[1:]),
1126+
)
11151127
return branches[0], env, nil, false, nil
11161128
case *pairValue[T]:
11171129
if len(branches) != 1 {
@@ -1120,10 +1132,7 @@ func (m *Machine[T]) caseEvaluateStack(
11201132
Message: "InvalidCaseBranchCount",
11211133
}
11221134
}
1123-
args := m.allocValueElems(2)
1124-
args[0] = v.first
1125-
args[1] = v.second
1126-
m.pushApplyFrames(args)
1135+
m.pushApplyFrames2(v.first, v.second)
11271136
return branches[0], env, nil, false, nil
11281137
default:
11291138
return nil, nil, nil, false, &TypeError{

0 commit comments

Comments
 (0)