-
Notifications
You must be signed in to change notification settings - Fork 335
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Adds APIs to jsg::Lock to perform manual external memory accounting. #2494
Conversation
|
||
~ExternalMemoryAdjuster() noexcept(false) { | ||
isolate->AdjustAmountOfExternalAllocatedMemory(-size); | ||
} |
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.
Review note: Obviously this can be a problem if the kj::Own<void>
for this is held beyond the lifespan of the isolate
. Should we include more protections against that by making the isolate
effectively a weak ref rather than a bare pointer? Should we forgo this entirely in favor of just having the manual adjustExternalMemory(...)
call?
The key use case for this would be to allow for something like..
auto ary = kj::heapArray<int>(10).attach(js.getExternalMemoryAdjuster(10 * sizeof(int)));
Such that the external memory will be automatically adjusted in the isolate when the array is freed. This, of course, is only really appropriate if the array does not get bound to an ArrayBuffer
at any point, which would cause the data to be double accounted.
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.
Makes sense to me as an interface
Going to go ahead and merge. If we want to tweak the APIs further for any reason we can, but these are good enough to get us started. |
@@ -278,6 +278,29 @@ JsSymbol Lock::symbolAsyncDispose() { | |||
return IsolateBase::from(v8Isolate).getSymbolAsyncDispose(); | |||
} | |||
|
|||
void Lock::adjustExternalMemory(ssize_t amount) { |
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 this API is error-prone: it's too easy to adjust the memory up and forget to adjust it back down later.
Could we create an RAII-based API instead? Have this method return some sort of an object whose destructor adjusts the memory back down. The object could also have a method to modify the amount of memory it's representing, for cases where we're tracking memory for a data structure that changes over time.
I think this object can just be a trivial wrapper around an integer; no allocation required. It can have a move constructor which zeros out the old value.
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.
Oh, I missed that this PR actually introduces an RAII API as well.
I think we should only offer the RAII API. We can extend it to work well even when the memory allocation changes over time, using the technique I suggested. Also can avoid the need for a heap allocation.
While ultimately we want to explore more automatic memory accounting for jsg objects, there's still a need for manual adjustments. This PR adds two new APIs to
jsg::Lock
for manual external memory adjustments.Why is this needed? Let's say you have a
jsg::Object
that holds onto an internalkj::Array<kj::byte>
that is never wrapped in av8::ArrayBuffer
... v8 won't never know about that memory allocation for thatkj::Array
unless we manually inform it.Manual adjustment is imperfect and should only be used when the need is clear. We want to work on making the adjustments largely automatic when possible to do so, but for now this gives us a path forward to having better external memory tracking.
Note that when a
kj::Array<kj::byte>
is wrapped by av8::ArrayBuffer
, thev8::ArrayBuffer
itself will handle reporting for us, so we don't need to manually adjust in those cases.