Skip to content

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

Merged
jasnell merged 1 commit intomainfrom
jsnell/jsg-manual-external-memory-accounting
Aug 9, 2024
Merged

Adds APIs to jsg::Lock to perform manual external memory accounting.#2494
jasnell merged 1 commit intomainfrom
jsnell/jsg-manual-external-memory-accounting

Conversation

@jasnell
Copy link
Copy Markdown
Collaborator

@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 anonrig, fhanau, kentonv and npaun August 7, 2024 19:15
@jasnell jasnell requested review from a team as code owners August 7, 2024 19:15

~ExternalMemoryAdjuster() noexcept(false) {
isolate->AdjustAmountOfExternalAllocatedMemory(-size);
}
Copy link
Copy Markdown
Collaborator 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
Copy Markdown
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

@jasnell
Copy link
Copy Markdown
Collaborator 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
return IsolateBase::from(v8Isolate).getSymbolAsyncDispose();
}

void Lock::adjustExternalMemory(ssize_t amount) {
Copy link
Copy Markdown
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
Copy Markdown
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