Improving high-frequency HandleScope performance

30 views
Skip to first unread message

Konnor Krupp

unread,
Jun 29, 2023, 7:02:44 PM6/29/23
to v8-dev
Hey all,

(~beginner to V8 internals!)

We have a use case which does high frequency lambda-like JS function execution across any/multiple threads in a constrained environment (Android) which has heavy cost for malloc.

In local on-device benchmarking, we noticed grabbing the V8::EntryScope on every lambda-like function call does a few malloc/free pairs, specifically:
* Context::Enter uses two DetachableVectors for the first time (once each) which mallocs space
* HandleScope::CreateHandle uses a DetachableVector for the first time (once) which mallocs space
* Destruction of the V8::EntryScope frees this space

For our case which rarely nests v8::EntryScopes and creates/frees them in pairs at high frequency, it would be more efficient (overall) to use an absl::InlinedVector-like object which assumes at least one (but up to N) Handles will be created / Contexts will be entered/saved. This shaved a few percent off our minimal overhead numbers. Naturally this would use up more initial space, though I expect the situations where a V8::EntryScope are created and never used are rare?

I am happy to make this contribution, but want to validate I'm not missing some subtlety, and wanted to generally ask for permission before going through the process! I expect this would be generally useful for other lambda-like use cases such as edge functions, Cloudflare Workers, etc.

My intended implementation would:
* have DetachableVector be backed by an absl::InlinedVector of size kMinimumCapacity (8)
* Optional?: have minimum capacity controlled by compile-time flag (could be default 0 and not inline anything!)

Thanks!

Konnor


Konnor Krupp

unread,
Jul 11, 2023, 3:42:54 AM7/11/23
to v8-dev
Checking in here, I may jump to the "Submit your code" section of the contributor docs and rely on code review to vet the approach and/or get a yes/no?

Thanks,

Konnor

Jakob Kummerow

unread,
Jul 11, 2023, 7:18:08 AM7/11/23
to v8-...@googlegroups.com
I don't know what a v8::EntryScope is, but clarifying the specifics of your plan in the form of uploading a patch to Gerrit is probably a good idea.

I'm guessing an absl::InlinedVector is like a v8::base::SmallVector? From a 20-second glance, it looks like maybe DetachableVector could derive from that? (I don't know this code well and have no strong opinions.)


--

Reply all
Reply to author
Forward
0 new messages