Skip to content
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

Adds APIs to jsg::Lock to perform manual external memory accounting. #2494

Merged
merged 1 commit into from
Aug 9, 2024

Conversation

jasnell
Copy link
Member

@jasnell jasnell commented Aug 7, 2024

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 internal kj::Array<kj::byte> that is never wrapped in a v8::ArrayBuffer ... v8 won't never know about that memory allocation for that kj::Array unless we manually inform it.

class Foo: public jsg::Object {
public:
  Foo(jsg::Lock& js) : foo(kj::heapArray<int>(1024).attach(js.getExternalMemoryAdjuster(1024))) {}
private:
  kj::Array<kj::byte> foo;
}

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 a v8::ArrayBuffer, the v8::ArrayBuffer itself will handle reporting for us, so we don't need to manually adjust in those cases.

@jasnell jasnell requested review from a team as code owners August 7, 2024 19:15

~ExternalMemoryAdjuster() noexcept(false) {
isolate->AdjustAmountOfExternalAllocatedMemory(-size);
}
Copy link
Member Author

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.

Copy link
Member

@npaun npaun left a 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

src/workerd/jsg/jsg.c++ Show resolved Hide resolved
src/workerd/jsg/jsg.h Show resolved Hide resolved
@jasnell
Copy link
Member Author

jasnell commented Aug 9, 2024

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.

@jasnell jasnell merged commit 16c7353 into main Aug 9, 2024
8 of 9 checks passed
@@ -278,6 +278,29 @@ JsSymbol Lock::symbolAsyncDispose() {
return IsolateBase::from(v8Isolate).getSymbolAsyncDispose();
}

void Lock::adjustExternalMemory(ssize_t amount) {
Copy link
Member

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.

Copy link
Member

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.

@kentonv kentonv deleted the jsnell/jsg-manual-external-memory-accounting branch August 9, 2024 17:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants