-
-
Notifications
You must be signed in to change notification settings - Fork 216
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
v8.Value becomes manually releaseable #361
Conversation
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.
This is great if it can be used to mitigate the memleak issues associated with #105
The code looks good, but it would be nice to get a memory profile demonstrating it
v8go.cc
Outdated
@@ -119,7 +122,10 @@ m_value* tracked_value(m_ctx* ctx, m_value* val) { | |||
// Go <--> C, which would be a significant change, as there are places where | |||
// we get the context from the value, but if we then need the context to get | |||
// the value, we would be in a circular bind. | |||
ctx->vals.push_back(val); | |||
if (val->id == 0) { | |||
val->id = ctx->nextValId++; |
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.
Is this right? The initial value will be 0, not 1. It felt wrong because the conditional would be true for the initial call and the second call but never any subsequent calls.
I think this should be preincrement. vals is an unordered map now so I don't think we care about filling in from key 0.
v8go.cc
Outdated
@@ -119,7 +122,10 @@ m_value* tracked_value(m_ctx* ctx, m_value* val) { | |||
// Go <--> C, which would be a significant change, as there are places where |
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.
🔕 This to-do can probably be revised.
v8go.cc
Outdated
val->ptr.Reset(); | ||
delete val; | ||
|
||
for(auto& [key, value] : ctx->vals) { |
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.
I'm getting a warning here:
v8go.cc: In function ‘void ContextFree(ContextPtr)’:
v8go.cc:620:13: warning: structured bindings only available with ‘-std=c++17’ or ‘-std=gnu++17’
620 | for(auto& [key, value] : ctx->vals) {
I think this is only a c++14 part of the project.
for(auto it = ctx->vals.begin(); it != ctx->vals.end(); ++it) {
it->second->ptr.Reset();
delete it->second;
}
function_template_test.go
Outdated
defer ctx.Close() | ||
ctx.RunScript("print('foo', 'bar', 0, 1)", "") | ||
if ctx.RetainedValueCount() != 5 { | ||
t.Errorf("expected 5 retained values, got: %d", ctx.RetainedValueCount()) |
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.
This is failing for me locally:
go test
# rogchap.com/v8go
v8go.cc: In function ‘void ContextFree(ContextPtr)’:
v8go.cc:620:13: warning: structured bindings only available with ‘-std=c++17’ or ‘-std=gnu++17’
620 | for(auto& [key, value] : ctx->vals) {
| ^
[foo bar 0 1]
[foo bar 0 1]
--- FAIL: TestFunctionTemplate_generates_values (0.02s)
function_template_test.go:66: expected 5 retained values, got: 1
FAIL
exit status 1
FAIL rogchap.com/v8go 7.678s
Can you repro?
@@ -166,6 +166,7 @@ extern void CPUProfileDelete(CPUProfile* ptr); | |||
extern ContextPtr NewContext(IsolatePtr iso_ptr, | |||
TemplatePtr global_template_ptr, | |||
int ref); | |||
extern int ContextRetainedValueCount(ContextPtr ctx); |
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.
🔕 Maybe ctx_ptr and val_ptr to match other APIs.
}; | ||
|
||
struct m_value { | ||
long id; |
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.
I think we need a constructor here to enforce that this member is initialized to zero (eg, m_value() : id(0) { }
). Otherwise, we have to depend on the caller to invoke like new m_value()
instead of new m_value
and everybody here is doing the latter. The reason I think this is that we explicitly check whether id is zero further down.
} | ||
ctx->vals.clear(); |
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.
🔕 I don't know about perf but I think it'd be clearer to call ValueRelease() (but be careful about modifying while looping).
Codecov ReportBase: 95.23% // Head: 95.28% // Increases project coverage by
Additional details and impacted files@@ Coverage Diff @@
## master #361 +/- ##
==========================================
+ Coverage 95.23% 95.28% +0.04%
==========================================
Files 17 17
Lines 588 594 +6
==========================================
+ Hits 560 566 +6
Misses 19 19
Partials 9 9
Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. ☔ View full report at Codecov. |
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.
🥇
defer iso.Dispose() | ||
global := v8.NewObjectTemplate(iso) | ||
printfn := v8.NewFunctionTemplate(iso, func(info *v8.FunctionCallbackInfo) *v8.Value { | ||
fmt.Printf("%+v\n", info.Args()) |
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.
Just to keep the test output clean, please consider removing any unneeded print statements here. I know the test is trying to show a print implementation but we should probably check the value here, not print it out. Same thing below.
[![Mend Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com) This PR contains the following updates: | Package | Type | Update | Change | |---|---|---|---| | [rogchap.com/v8go](https://togithub.com/rogchap/v8go) | require | minor | `v0.7.0` -> `v0.8.0` | --- ### Release Notes <details> <summary>rogchap/v8go</summary> ### [`v0.8.0`](https://togithub.com/rogchap/v8go/releases/tag/v0.8.0) [Compare Source](https://togithub.com/rogchap/v8go/compare/v0.7.0...v0.8.0) Full changelog ⇒ [v0.8.0](https://togithub.com/rogchap/v8go/blob/master/CHANGELOG.md#v080---2023-01-19) #### What's Changed - Allocate profile node children array using count by [@​genevieve](https://togithub.com/genevieve) in [https://github.com/rogchap/v8go/pull/256](https://togithub.com/rogchap/v8go/pull/256) - Build v8 for linux_arm64 by [@​epk](https://togithub.com/epk) in [https://github.com/rogchap/v8go/pull/223](https://togithub.com/rogchap/v8go/pull/223) - CHANGELOG: Highlight the subtlety of the Function Call breaking change by [@​dylanahsmith](https://togithub.com/dylanahsmith) in [https://github.com/rogchap/v8go/pull/259](https://togithub.com/rogchap/v8go/pull/259) - change -fpic to -fPIC in cgo.go by [@​iwind](https://togithub.com/iwind) in [https://github.com/rogchap/v8go/pull/263](https://togithub.com/rogchap/v8go/pull/263) - Remove unnecessary nested Locker and Isolate::Scope use by [@​dylanahsmith](https://togithub.com/dylanahsmith) in [https://github.com/rogchap/v8go/pull/275](https://togithub.com/rogchap/v8go/pull/275) - Upgrade V8 binaries for 9.7.106.18 version by [@​github-actions](https://togithub.com/github-actions) in [https://github.com/rogchap/v8go/pull/264](https://togithub.com/rogchap/v8go/pull/264) - Use length to ensure null chars do not cause early termination of C string copies/reads by [@​genevieve](https://togithub.com/genevieve) in [https://github.com/rogchap/v8go/pull/272](https://togithub.com/rogchap/v8go/pull/272) - Fix Object.Set with an empty key string by [@​dylanahsmith](https://togithub.com/dylanahsmith) in [https://github.com/rogchap/v8go/pull/276](https://togithub.com/rogchap/v8go/pull/276) - Upgrade V8 binaries for 9.7.106.19 version by [@​github-actions](https://togithub.com/github-actions) in [https://github.com/rogchap/v8go/pull/278](https://togithub.com/rogchap/v8go/pull/278) - Fix typo in promise.go by [@​lukasmalkmus](https://togithub.com/lukasmalkmus) in [https://github.com/rogchap/v8go/pull/310](https://togithub.com/rogchap/v8go/pull/310) - Updating context documentation by [@​ryanmurakami](https://togithub.com/ryanmurakami) in [https://github.com/rogchap/v8go/pull/335](https://togithub.com/rogchap/v8go/pull/335) - Add additional CPUProfile values by [@​ryanmurakami](https://togithub.com/ryanmurakami) in [https://github.com/rogchap/v8go/pull/341](https://togithub.com/rogchap/v8go/pull/341) - v8.Value becomes manually releaseable by [@​fizx](https://togithub.com/fizx) in [https://github.com/rogchap/v8go/pull/361](https://togithub.com/rogchap/v8go/pull/361) #### New Contributors - [@​iwind](https://togithub.com/iwind) made their first contribution in [https://github.com/rogchap/v8go/pull/263](https://togithub.com/rogchap/v8go/pull/263) - [@​lukasmalkmus](https://togithub.com/lukasmalkmus) made their first contribution in [https://github.com/rogchap/v8go/pull/310](https://togithub.com/rogchap/v8go/pull/310) - [@​ryanmurakami](https://togithub.com/ryanmurakami) made their first contribution in [https://github.com/rogchap/v8go/pull/335](https://togithub.com/rogchap/v8go/pull/335) - [@​fizx](https://togithub.com/fizx) made their first contribution in [https://github.com/rogchap/v8go/pull/361](https://togithub.com/rogchap/v8go/pull/361) **Full Changelog**: rogchap/v8go@v0.7.0...v0.8.0 </details> --- ### Configuration 📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined). 🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied. ♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox. 🔕 **Ignore**: Close this PR and you won't be reminded about this update again. --- - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box --- This PR has been generated by [Mend Renovate](https://www.mend.io/free-developer-tools/renovate/). View repository job log [here](https://app.renovatebot.com/dashboard#github/ajvpot/lockfileparsergo). <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNC4xMDUuNCIsInVwZGF0ZWRJblZlciI6IjM0LjEwNS40In0=--> Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
* values are manually releaseable Co-authored-by: Kyle Maxwell <[email protected]>
commit cee5f84 Author: Jonathan Geddes <[email protected]> Date: Mon Apr 10 10:09:13 2023 -0600 shared array buffers (rogchap#378) * Initial support for SharedArrayBuffer commit 0e40e6e Author: Randy Goodman <[email protected]> Date: Mon Mar 20 19:27:55 2023 -0400 v0.9.0 (rogchap#376) commit be4ff5e Author: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Date: Tue Mar 21 07:17:39 2023 +1100 Upgrade V8 binaries for 11.1.277.13 version (rogchap#373) commit 02e1763 Author: Jacques Nadeau <[email protected]> Date: Mon Jan 30 06:51:45 2023 -0800 Upgrade V8 binaries for 10.9.194.9 version **working** (rogchap#363) * Upgrade V8 binaries for 10.9.194.9 version * Fixes to support newest stable v8. - Update github workflow to use go 18 & 19 - Update github workflow to use macos-latest - Update github build workflow to use ubuntu 22.04 - Add gitignore for jetbrains and .gclient_previous files - Switch cgo build to C++17 and enable sandbox at build time - Update test with update to date error message - Remove no longer supported build flag. - Move initialization to v8go.go and include flag set to avoid flag freezing - Reorder initialization so allocator is initialized after v8 (required by latest v8) * Update V8 static library for macos-11 x86_64 * Update V8 static library for ubuntu-22.04 x86_64 * Update V8 static library for macos-11 arm64 * Update V8 static library for ubuntu-22.04 arm64 * Update changelog and remove no-longer valid comment --------- Co-authored-by: jacques-n <[email protected]> commit 7d843f1 Author: Kyle Maxwell <[email protected]> Date: Thu Jan 19 13:17:30 2023 -0800 v8.Value becomes manually releaseable (rogchap#361) * values are manually releaseable Co-authored-by: Kyle Maxwell <[email protected]> commit 1f00b50 Author: Ryan H. Lewis <[email protected]> Date: Wed Nov 2 14:15:10 2022 -0600 adding additional profile values (rogchap#341) commit fc8b9f1 Author: Ryan H. Lewis <[email protected]> Date: Mon Aug 8 16:10:42 2022 -0600 updating context documentation (rogchap#335) Co-authored-by: Ryan H Lewis <[email protected]> commit da7f43c Author: Lukas Malkmus <[email protected]> Date: Wed Apr 13 16:20:09 2022 +0200 Fix typo in promise.go (rogchap#310) Literally just fixing a typo in `promise.go` that I discovered while strolling through the code. commit db78170 Author: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Date: Wed Mar 9 14:29:03 2022 -0800 Upgrade V8 binaries for 9.7.106.19 version (rogchap#278) * Upgrade V8 binaries for 9.7.106.19 version * Update V8 static library for ubuntu-18.04 x86_64 (rogchap#292) Co-authored-by: genevieve <[email protected]> * Update V8 static library for ubuntu-18.04 arm64 (rogchap#293) Co-authored-by: genevieve <[email protected]> * Update V8 static library for macos-11 x86_64 (rogchap#294) Co-authored-by: genevieve <[email protected]> * Update V8 static library for macos-11 arm64 (rogchap#295) Co-authored-by: genevieve <[email protected]> Co-authored-by: dylanahsmith <[email protected]> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: genevieve <[email protected]> commit 5e91d3d Author: Dylan Thacker-Smith <[email protected]> Date: Wed Jan 12 17:06:50 2022 -0500 Fix Object.Set with an empty key string (rogchap#276) commit 1d433f7 Author: Genevieve <[email protected]> Date: Wed Jan 12 10:41:04 2022 -0800 Use length to ensure null chars do not cause early termination of C string copies/reads (rogchap#272) * Intro test case for null terminator string * unexpected result: expected ss, got sx00s * Fix the assertion, add Unicode symbols * Pass go string length into v8::String::New to ensure it does not cut off at null chars * Reuse the existing RtnString type * Formatting * Update value_test.go Co-authored-by: Dylan Thacker-Smith <[email protected]> * Table tests for NewValue * Use std::string constructor in CopyString(Utf8Value) to keep whole string * Update changelog Co-authored-by: Genevieve L'Esperance <[email protected]> Co-authored-by: Maxim Shirshin <[email protected]> Co-authored-by: Dylan Thacker-Smith <[email protected]> commit cb8779b Author: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Date: Wed Jan 12 07:51:03 2022 -0800 Upgrade V8 binaries for 9.7.106.18 version (rogchap#264) Co-authored-by: dylanahsmith <[email protected]> Co-authored-by: Genevieve <[email protected]> commit c0dbfd3 Author: Dylan Thacker-Smith <[email protected]> Date: Wed Jan 12 09:58:36 2022 -0500 Remove unnecessary nested Locker and Isolate::Scope use (rogchap#275) * Remove unnecessary nested Locker and Isolate::Scope use * Make ExceptionError static, since it isn't used outside that file commit ede7cee Author: Liu Xiangchao <[email protected]> Date: Fri Jan 7 01:33:29 2022 +0800 modify -fpic to -fPIC (rogchap#263) commit a0417ad Author: Dylan Thacker-Smith <[email protected]> Date: Wed Dec 22 15:48:45 2021 -0500 CHANGELOG: Highlight the subtlety of the Function Call breaking change (rogchap#259) commit 943fcf9 Author: Aditya Sharma <[email protected]> Date: Wed Dec 22 09:30:54 2021 -0800 Build v8 for linux_arm64 (rogchap#223) * Build v8 for linux_arm64 * Update V8 static library for ubuntu-18.04 arm64 Co-authored-by: epk <[email protected]> commit e635d48 Author: Genevieve <[email protected]> Date: Mon Dec 13 09:28:33 2021 -0800 Allocate children array with count (rogchap#256) commit 6e4af34 Author: Roger Chapman <[email protected]> Date: Thu Dec 9 10:29:56 2021 +1100 v0.7.0 commit 762c7a8 Author: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Date: Wed Dec 8 15:22:43 2021 -0800 Upgrade V8 binaries for 9.6.180.12 version (rogchap#231) * Upgrade V8 binaries for 9.6.180.12 version * Add -ldl flag to linux build to fix missing dlsym errors * Update V8 static library for macos-11 x86_64 (rogchap#249) Co-authored-by: genevieve <[email protected]> * Update V8 static library for ubuntu-18.04 x86_64 (rogchap#250) Co-authored-by: genevieve <[email protected]> * Update V8 static library for macos-11 arm64 (rogchap#251) Co-authored-by: genevieve <[email protected]> * Update changelog Co-authored-by: dylanahsmith <[email protected]> Co-authored-by: Genevieve L'Esperance <[email protected]> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: genevieve <[email protected]> commit a92639e Author: Genevieve <[email protected]> Date: Tue Nov 30 12:59:03 2021 -0800 Enable intl support and store data in v8 binary (rogchap#242) * Enable intl support and store data in v8 binary * Update V8 static library for ubuntu-18.04 x86_64 * Update V8 static library for macos-11 arm64 * Update changelog Co-authored-by: genevieve <[email protected]> commit f55271d Author: Genevieve <[email protected]> Date: Mon Nov 22 14:50:43 2021 -0800 Remove Windows support (rogchap#234) * Remove Windows binary support * Update CHANGELOG.md Co-authored-by: Oliver Fuerst <[email protected]> * Update CHANGELOG.md Co-authored-by: Dylan Thacker-Smith <[email protected]> * Update windows section * Update README.md Co-authored-by: Dylan Thacker-Smith <[email protected]> * Update README.md Co-authored-by: Dylan Thacker-Smith <[email protected]> * Remove windows fossa step * Remove deps/windows_x86_64 Co-authored-by: Oliver Fuerst <[email protected]> Co-authored-by: Dylan Thacker-Smith <[email protected]> Co-authored-by: Roger Chapman <[email protected]>
This allows long-lived contexts that call go functions without leaking memory. Typically, you'd want to use the following pattern for go functions that don't allow values to escape.