-
Notifications
You must be signed in to change notification settings - Fork 2
Fixed memory leak #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,212 @@ | ||
| // Copyright 2024 Dolthub, Inc. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package regex | ||
|
|
||
| import ( | ||
| "context" | ||
| "github.com/tetratelabs/wazero" | ||
| "github.com/tetratelabs/wazero/api" | ||
| "github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1" | ||
| "reflect" | ||
| "runtime" | ||
| "sync" | ||
| ) | ||
|
|
||
| // modulePool is the pool that is used internally by the project. | ||
| var modulePool = NewPool() | ||
|
|
||
| // RuntimeTracker tracks all relevant information that the Pool needs regarding a runtime. | ||
| type RuntimeTracker struct { | ||
| id uint64 | ||
| r wazero.Runtime | ||
| compiled wazero.CompiledModule | ||
| modules []api.Module | ||
| max uint64 | ||
| fetches uint64 | ||
| } | ||
|
|
||
| // Pool is a special pool object for handling ICU regex modules. The cause isn't quite clear, but runtimes continue to | ||
| // hold onto memory even when their owned modules are closed, so this special pool type will also recycle the runtimes | ||
| // once a certain number of modules have been fetched. | ||
| type Pool struct { | ||
| mutex *sync.Mutex | ||
| runtimes []*RuntimeTracker | ||
| outstandingMods map[uintptr]uint64 | ||
| nextId uint64 | ||
| maxFetch uint64 | ||
| } | ||
|
|
||
| // NewPool creates a new *Pool. | ||
| func NewPool() *Pool { | ||
| r, compiled := createRuntime(context.Background()) | ||
| pool := &Pool{ | ||
| mutex: &sync.Mutex{}, | ||
| runtimes: []*RuntimeTracker{{ | ||
| id: 1, | ||
| r: r, | ||
| compiled: compiled, | ||
| modules: make([]api.Module, 0, 16), | ||
| max: 0, | ||
| fetches: 0, | ||
| }}, | ||
| outstandingMods: make(map[uintptr]uint64), | ||
| nextId: 2, | ||
| maxFetch: 128, | ||
| } | ||
| return pool | ||
| } | ||
|
|
||
| // Get returns a new module from the pool. | ||
| func (pool *Pool) Get() api.Module { | ||
| pool.mutex.Lock() | ||
| defer pool.mutex.Unlock() | ||
|
|
||
| ctx := context.Background() | ||
| rtracker := pool.runtimes[len(pool.runtimes)-1] | ||
| rtracker.fetches++ | ||
| // If we've used up the number of fetches allowed in this runtime, then we'll create a new one | ||
| if rtracker.fetches >= pool.maxFetch { | ||
| r, compiled := createRuntime(ctx) | ||
| rtracker = &RuntimeTracker{ | ||
| id: pool.nextId, | ||
| r: r, | ||
| compiled: compiled, | ||
| modules: make([]api.Module, 0, 16), | ||
| max: 0, | ||
| fetches: 0, | ||
| } | ||
| pool.runtimes = append(pool.runtimes, rtracker) | ||
| pool.nextId++ | ||
| } | ||
| var module api.Module | ||
| // If the runtime has no modules remaining, then we need to create a new module | ||
| if len(rtracker.modules) == 0 { | ||
| rtracker.max++ | ||
| var err error | ||
| module, err = rtracker.r.InstantiateModule(ctx, rtracker.compiled, icuConfig) | ||
| if err != nil { | ||
| panic(err) | ||
| } | ||
| } else { | ||
| // Pop the last module from the slice | ||
| module = rtracker.modules[len(rtracker.modules)-1] | ||
| rtracker.modules = rtracker.modules[:len(rtracker.modules)-1] | ||
| } | ||
| // Now we need to track that this module is being returned | ||
| pool.outstandingMods[reflect.ValueOf(module).Pointer()] = rtracker.id | ||
| runtime.SetFinalizer(module, func(module api.Module) { | ||
| pool.finalized(module) | ||
| }) | ||
| return module | ||
| } | ||
|
|
||
| // Put returns the module to the pool. | ||
| func (pool *Pool) Put(module api.Module) { | ||
| pool.mutex.Lock() | ||
| defer pool.mutex.Unlock() | ||
| pool.receivedModule(module, true) | ||
| } | ||
|
|
||
| // finalized is called by the finalizer, and only exists to catch orphaned modules. | ||
| func (pool *Pool) finalized(module api.Module) { | ||
| pool.mutex.Lock() | ||
| defer pool.mutex.Unlock() | ||
| pool.receivedModule(module, false) | ||
| } | ||
|
|
||
| // receivedModule is called when either the module is returned through Put, or the finalizer catches an orphaned module | ||
| // through finalized. | ||
| func (pool *Pool) receivedModule(module api.Module, isPut bool) { | ||
| // Remove the finalizer that was set when the object was fetched. | ||
| // This is only called from Put, as the finalizer is being called so we don't want to remove it. | ||
| if isPut { | ||
| runtime.SetFinalizer(module, nil) | ||
| } | ||
| // Grab the runtime ID and remove the module from the tracking map | ||
| ptr := reflect.ValueOf(module).Pointer() | ||
| runtimeId := pool.outstandingMods[ptr] | ||
| delete(pool.outstandingMods, ptr) | ||
| for rtrackerIdx := 0; rtrackerIdx < len(pool.runtimes); rtrackerIdx++ { | ||
| ctx := context.Background() | ||
| rtracker := pool.runtimes[rtrackerIdx] | ||
| // If this is a different runtime, then we still need to check whether it should be removed | ||
| if rtracker.id != runtimeId { | ||
| if rtracker.fetches >= pool.maxFetch && uint64(len(rtracker.modules)) >= rtracker.max { | ||
| pool.closeRuntime(ctx, rtrackerIdx, rtracker) | ||
| rtrackerIdx-- | ||
| } | ||
| continue | ||
| } | ||
| if isPut { | ||
| // Add the module back to the runtime when called from Put | ||
| rtracker.modules = append(rtracker.modules, module) | ||
| } else { | ||
| // We remove the module from the runtime altogether when called from the finalizer | ||
| rtracker.max-- | ||
| _ = module.Close(ctx) | ||
| } | ||
| // If this runtime has run out of fetches and all of its modules are back, then we need to close and remove it | ||
| if rtracker.fetches >= pool.maxFetch && uint64(len(rtracker.modules)) >= rtracker.max { | ||
| pool.closeRuntime(ctx, rtrackerIdx, rtracker) | ||
| } | ||
| return | ||
| } | ||
| // We could not find the runtime ID (or the module was not in the map), which should never happen | ||
| panic("go-icu-regex pool found orphaned module") | ||
| } | ||
|
|
||
| // closeRuntime closes the given runtime, as well as removing it from the list of runtimes. | ||
| func (pool *Pool) closeRuntime(ctx context.Context, rtrackerIdx int, rtracker *RuntimeTracker) { | ||
| // First we'll close all the modules, then we'll close the runtime itself | ||
| for _, mod := range rtracker.modules { | ||
| _ = mod.Close(ctx) | ||
| } | ||
| _ = rtracker.r.Close(ctx) | ||
| // We then remove the runtime from the slice | ||
| newSlice := make([]*RuntimeTracker, len(pool.runtimes)-1) | ||
| copy(newSlice, pool.runtimes[:rtrackerIdx]) | ||
| copy(newSlice, pool.runtimes[rtrackerIdx+1:]) | ||
| pool.runtimes = newSlice | ||
| } | ||
|
|
||
| // createRuntime creates a new runtime, as well as compiling the ICU module. The compiled module is only valid with the | ||
| // runtime that compiled it. | ||
| func createRuntime(ctx context.Context) (wazero.Runtime, wazero.CompiledModule) { | ||
| r := wazero.NewRuntime(ctx) | ||
| wasi_snapshot_preview1.MustInstantiate(ctx, r) | ||
| envBuilder := r.NewHostModuleBuilder("env") | ||
| noop_two := func(int32, int32) int32 { return -1 } | ||
| noop_four := func(int32, int32, int32, int32) int32 { return -1 } | ||
| envBuilder.NewFunctionBuilder().WithFunc(noop_two).Export("__syscall_stat64") | ||
| envBuilder.NewFunctionBuilder().WithFunc(noop_two).Export("__syscall_lstat64") | ||
| envBuilder.NewFunctionBuilder().WithFunc(noop_two).Export("__syscall_fstat64") | ||
| envBuilder.NewFunctionBuilder().WithFunc(noop_four).Export("__syscall_newfstatat") | ||
| _, err := envBuilder.Instantiate(ctx) | ||
| if err != nil { | ||
| panic(err) | ||
| } | ||
| compiledIcuWasm, err := r.CompileModule(ctx, icuWasm) | ||
| if err != nil { | ||
| panic(err) | ||
| } | ||
| return r, compiledIcuWasm | ||
| } | ||
|
|
||
| // SetPoolFetchMax determines how many fetches are allowed from the internal Pool before a runtime is recycled. | ||
| func SetPoolFetchMax(maxFetch uint64) { | ||
| modulePool.mutex.Lock() | ||
| defer modulePool.mutex.Unlock() | ||
| modulePool.maxFetch = maxFetch | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this is slow, it might make sense to get it a single time at creation, and for the pool to vend an object which includes both the
api.Moduleand itsuintptrThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't the slow portion. It's
wazero.NewRuntime(ctx)that's slow (like half a second on my machine). That's why we can't just create a new one on every request. Second to that isInstantiateModule. Everything else is essentially instant in comparison. The slowdown is due to having to periodically a new runtime, not really anything else.