Skip to content

Commit 523d4ce

Browse files
Translator: Cap the constant slot size to avoid slot explosion
Some large functions have a lot of constants than we expected. For example, `SwiftSyntax.SyntaxRewriter.dispatchVisit(_:)` is over 70kb in wasm binary size and has 1000+ unique constants, so the constant slot size was over 1000. This caused a call to the function to consume a lot of stack space, even though the function only accesses a few of them in a single function call. This patch caps the constant slot size to 128 to avoid slot explosion.
1 parent 9962a18 commit 523d4ce

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

Sources/WasmKit/Translator.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,9 @@ struct StackLayout {
216216
self.frameHeader = FrameHeaderLayout(type: type)
217217
self.numberOfLocals = numberOfLocals
218218
// The number of constant slots is determined by the code size
219-
self.constantSlotSize = max(codeSize / 20, 4)
219+
// This is a heuristic value to balance the fast access to constants
220+
// and the size of stack frame. Cap the slot size to avoid size explosion.
221+
self.constantSlotSize = min(max(codeSize / 20, 4), 128)
220222
let (maxSlots, overflow) = self.constantSlotSize.addingReportingOverflow(numberOfLocals)
221223
guard !overflow, maxSlots < VReg.max else {
222224
throw TranslationError("The number of constant slots overflows")

0 commit comments

Comments
 (0)