From 6d3a77e89ae6c88ad616ad772df6b27c6cfd6998 Mon Sep 17 00:00:00 2001 From: Takeshi Yoneda Date: Fri, 14 Jun 2024 11:11:00 -0700 Subject: [PATCH] validation: avoids allocation per br_table (#2252) This eliminates the allocation that previously happened per br_table instruction during function validation. Signed-off-by: Takeshi Yoneda --- internal/wasm/func_validation.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/internal/wasm/func_validation.go b/internal/wasm/func_validation.go index ce2c7254db..6044892289 100644 --- a/internal/wasm/func_validation.go +++ b/internal/wasm/func_validation.go @@ -451,14 +451,14 @@ func (m *Module) validateFunctionWithMaxStackValues( return fmt.Errorf("read immediate: %w", err) } - list := make([]uint32, nl) + sts.ls = sts.ls[:0] for i := uint32(0); i < nl; i++ { l, n, err := leb128.DecodeUint32(br) if err != nil { return fmt.Errorf("read immediate: %w", err) } num += n - list[i] = l + sts.ls = append(sts.ls, l) } ln, n, err := leb128.DecodeUint32(br) if err != nil { @@ -511,7 +511,7 @@ func (m *Module) validateFunctionWithMaxStackValues( } } - for _, l := range list { + for _, l := range sts.ls { if int(l) >= len(controlBlockStack.stack) { return fmt.Errorf("invalid l param given for %s", OpcodeBrTableName) } @@ -2003,6 +2003,8 @@ var vecSplatValueTypes = [...]ValueType{ type stacks struct { vs valueTypeStack cs controlBlockStack + // ls is the label slice that is reused for each br_table instruction. + ls []uint32 } func (sts *stacks) reset(functionType *FunctionType) { @@ -2012,6 +2014,7 @@ func (sts *stacks) reset(functionType *FunctionType) { sts.vs.maximumStackPointer = 0 sts.cs.stack = sts.cs.stack[:0] sts.cs.stack = append(sts.cs.stack, controlBlock{blockType: functionType}) + sts.ls = sts.ls[:0] } type controlBlockStack struct {