Skip to content

Commit ff09bf6

Browse files
Reverts change around module name allocation (#62)
This reverts to the former approach of external name management because it ends up faster in practice with http-wasm. The last commit used "anonymous modules" which is not a good fit for http-wasm in how it works today. Basically, http-wasm is pooling modules and these are re-used for sequential handler functions. The members of the pool change at a very low rate, so there's not a lot of lock contention on the module namespace. On the other hand, the normal path, sequential performance is slower when we use anonymous modules. Signed-off-by: Adrian Cole <[email protected]>
1 parent 85968a6 commit ff09bf6

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

handler/middleware.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"strconv"
99
"strings"
1010
"sync"
11+
"sync/atomic"
1112

1213
"github.com/tetratelabs/wazero"
1314
wazeroapi "github.com/tetratelabs/wazero/api"
@@ -52,6 +53,7 @@ type middleware struct {
5253
logger api.Logger
5354
pool sync.Pool
5455
features handler.Features
56+
instanceCounter uint64
5557
}
5658

5759
func (m *middleware) Features() handler.Features {
@@ -74,10 +76,9 @@ func NewMiddleware(ctx context.Context, guest []byte, host handler.Host, opts ..
7476
}
7577

7678
m := &middleware{
77-
host: host,
78-
runtime: wr,
79-
// Clear the module name, to avoid conflicts when the pool size is >1.
80-
moduleConfig: o.moduleConfig.WithName(""),
79+
host: host,
80+
runtime: wr,
81+
moduleConfig: o.moduleConfig,
8182
guestConfig: o.guestConfig,
8283
logger: o.logger,
8384
}
@@ -197,7 +198,9 @@ type guest struct {
197198
}
198199

199200
func (m *middleware) newGuest(ctx context.Context) (*guest, error) {
200-
g, err := m.runtime.InstantiateModule(ctx, m.guestModule, m.moduleConfig)
201+
moduleName := fmt.Sprintf("%d", atomic.AddUint64(&m.instanceCounter, 1))
202+
203+
g, err := m.runtime.InstantiateModule(ctx, m.guestModule, m.moduleConfig.WithName(moduleName))
201204
if err != nil {
202205
_ = m.runtime.Close(ctx)
203206
return nil, fmt.Errorf("wasm: error instantiating guest: %w", err)

0 commit comments

Comments
 (0)