Repository: tetratelabs/wazero
Pull request #1754 (2023-10-04 22:57:16)

Simplifies wasm.ElementInstance struct


Patch:
diff --git a/internal/engine/compiler/engine.go b/internal/engine/compiler/engine.go
index 8fc9d52b..d4e8dcd0 100644
--- a/internal/engine/compiler/engine.go
+++ b/internal/engine/compiler/engine.go
@@ -404,7 +404,7 @@ const (
 	dataInstanceStructSize = 24
 
 	// Consts for wasm.ElementInstance.
-	elementInstanceStructSize = 32
+	elementInstanceStructSize = 24
 
 	// pointerSizeLog2 satisfies: 1 << pointerSizeLog2 = sizeOf(uintptr)
 	pointerSizeLog2 = 3
diff --git a/internal/engine/interpreter/interpreter.go b/internal/engine/interpreter/interpreter.go
index 9adb6afe..d47e7c31 100644
--- a/internal/engine/interpreter/interpreter.go
+++ b/internal/engine/interpreter/interpreter.go
@@ -1653,15 +1653,15 @@ func (ce *callEngine) callNativeFunc(ctx context.Context, m *wasm.ModuleInstance
 			inElementOffset := ce.popValue()
 			inTableOffset := ce.popValue()
 			table := tables[op.U2]
-			if inElementOffset+copySize > uint64(len(elementInstance.References)) ||
+			if inElementOffset+copySize > uint64(len(elementInstance)) ||
 				inTableOffset+copySize > uint64(len(table.References)) {
 				panic(wasmruntime.ErrRuntimeInvalidTableAccess)
 			} else if copySize != 0 {
-				copy(table.References[inTableOffset:inTableOffset+copySize], elementInstance.References[inElementOffset:])
+				copy(table.References[inTableOffset:inTableOffset+copySize], elementInstance[inElementOffset:])
 			}
 			frame.pc++
 		case wazeroir.OperationKindElemDrop:
-			elementInstances[op.U1].References = nil
+			elementInstances[op.U1] = nil
 			frame.pc++
 		case wazeroir.OperationKindTableCopy:
 			srcTable, dstTable := tables[op.U1].References, tables[op.U2].References
diff --git a/internal/wasm/store.go b/internal/wasm/store.go
index 4574b491..f15c19fc 100644
--- a/internal/wasm/store.go
+++ b/internal/wasm/store.go
@@ -164,18 +164,17 @@ func (m *ModuleInstance) GetFunctionTypeID(t *FunctionType) FunctionTypeID {
 }
 
 func (m *ModuleInstance) buildElementInstances(elements []ElementSegment) {
-	m.ElementInstances = make([]ElementInstance, len(elements))
+	m.ElementInstances = make([][]Reference, len(elements))
 	for i, elm := range elements {
 		if elm.Type == RefTypeFuncref && elm.Mode == ElementModePassive {
 			// Only passive elements can be access as element instances.
 			// See https://www.w3.org/TR/2022/WD-wasm-core-2-20220419/syntax/modules.html#element-segments
 			inits := elm.Init
-			elemInst := &m.ElementInstances[i]
-			elemInst.References = make([]Reference, len(inits))
-			elemInst.Type = RefTypeFuncref
+			inst := make([]Reference, len(inits))
+			m.ElementInstances[i] = inst
 			for j, idx := range inits {
 				if idx != ElementInitNullReference {
-					elemInst.References[j] = m.Engine.FunctionInstanceReference(idx)
+					inst[j] = m.Engine.FunctionInstanceReference(idx)
 				}
 			}
 		}
diff --git a/internal/wasm/table.go b/internal/wasm/table.go
index b202e8af..5bdbc123 100644
--- a/internal/wasm/table.go
+++ b/internal/wasm/table.go
@@ -134,12 +134,7 @@ type TableInstance struct {
 // ElementInstance represents an element instance in a module.
 //
 // See https://www.w3.org/TR/2022/WD-wasm-core-2-20220419/exec/runtime.html#element-instances
-type ElementInstance struct {
-	// References holds references whose type is either RefTypeFuncref or RefTypeExternref (unsupported).
-	References []Reference
-	// Type is the RefType of the references in this instance's References.
-	Type RefType
-}
+type ElementInstance = []Reference
 
 // Reference is the runtime representation of RefType which is either RefTypeFuncref or RefTypeExternref.
 type Reference = uintptr


Test patch:
diff --git a/internal/engine/compiler/compiler_post1_0_test.go b/internal/engine/compiler/compiler_post1_0_test.go
index 10a96d3b..7632b354 100644
--- a/internal/engine/compiler/compiler_post1_0_test.go
+++ b/internal/engine/compiler/compiler_post1_0_test.go
@@ -476,13 +476,7 @@ func TestCompiler_compileMemoryInit(t *testing.T) {
 }
 
 func TestCompiler_compileElemDrop(t *testing.T) {
-	origins := []wasm.ElementInstance{
-		{References: []wasm.Reference{1}},
-		{References: []wasm.Reference{2}},
-		{References: []wasm.Reference{3}},
-		{References: []wasm.Reference{4}},
-		{References: []wasm.Reference{5}},
-	}
+	origins := []wasm.ElementInstance{{1}, {2}, {3}, {4}, {5}}
 
 	for i := 0; i < len(origins); i++ {
 		t.Run(strconv.Itoa(i), func(t *testing.T) {
@@ -494,7 +488,7 @@ func TestCompiler_compileElemDrop(t *testing.T) {
 
 			// Verify assumption that before Drop instruction, all the element instances are not empty.
 			for _, inst := range insts {
-				require.NotEqual(t, 0, len(inst.References))
+				require.NotEqual(t, 0, len(inst))
 			}
 
 			compiler := env.requireNewCompiler(t, &wasm.FunctionType{}, newCompiler, &wazeroir.CompilationResult{
@@ -523,9 +517,9 @@ func TestCompiler_compileElemDrop(t *testing.T) {
 
 			for j := 0; j < len(insts); j++ {
 				if i == j {
-					require.Zero(t, len(env.module().ElementInstances[j].References))
+					require.Zero(t, len(env.module().ElementInstances[j]))
 				} else {
-					require.NotEqual(t, 0, len(env.module().ElementInstances[j].References))
+					require.NotEqual(t, 0, len(env.module().ElementInstances[j]))
 				}
 			}
 		})
@@ -627,7 +621,7 @@ func TestCompiler_compileTableCopy(t *testing.T) {
 
 func TestCompiler_compileTableInit(t *testing.T) {
 	elementInstances := []wasm.ElementInstance{
-		{}, {References: []wasm.Reference{1, 2, 3, 4, 5}},
+		{}, {1, 2, 3, 4, 5},
 	}
 
 	const tableSize = 100
@@ -707,8 +701,8 @@ func TestCompiler_compileTableInit(t *testing.T) {
 				for i := 0; i < tableSize; i++ {
 					exp[i] = uintptr(i)
 				}
-				if inst := elementInstances[tc.elemIndex]; inst.References != nil {
-					copy(exp[tc.destOffset:], inst.References[tc.sourceOffset:tc.sourceOffset+tc.copySize])
+				if inst := elementInstances[tc.elemIndex]; inst != nil {
+					copy(exp[tc.destOffset:], inst[tc.sourceOffset:tc.sourceOffset+tc.copySize])
 				}
 				require.Equal(t, exp, table)
 			} else {