Function templates and object templates in custom start-up snapshot.

150 views
Skip to first unread message

Jane Chen

unread,
Jun 3, 2015, 1:11:21 AM6/3/15
to v8-u...@googlegroups.com
I have global variables and global functions exposed through object templates and function templates, which slows down context creation.  I wonder whether I could still leverage the custom snapshot feature to speed up context creation with any of the following two approaches:

1) With the recent v8::V8::CreateSnapshotDataBlob introduced since V8 4.3
2) With the mechanism implemented in mksnapshot in 3.x by capturing a snapshot with a global object containing functions with callbacks, and later using that to initialize v8.

Thanks in advance.

Ben Noordhuis

unread,
Jun 3, 2015, 7:06:39 AM6/3/15
to v8-u...@googlegroups.com
If your question is if there is a mechanism to serialize
FunctionTemplates and ObjectTemplates, I'm 95% sure that hasn't been
implemented (yet - I believe it has been talked about.)

Have you tried caching your FunctionTemplates and ObjectTemplates?
You don't have to instantiate them afresh for each new context, you
can reuse them.

If you are worried about the overhead of
FunctionTemplate::GetFunction(), you may want to experiment with using
ObjectTemplate::SetAccessor() on your global object's ObjectTemplate
(the one you pass to Context::New()) to lazily instantiate them.

Jane Chen

unread,
Jun 3, 2015, 11:06:18 AM6/3/15
to v8-u...@googlegroups.com
Ben,

My question is, say if I have a function Print with a native callback through function template and set it to the global template, use that to create a context, then use serializer to create a snapshot.  If I use this snapshot to initialize v8, will function Print be pre-defined in that snapshot?

Jane

Ben Noordhuis

unread,
Jun 4, 2015, 7:40:02 AM6/4/15
to v8-u...@googlegroups.com
On Wed, Jun 3, 2015 at 5:06 PM, Jane Chen <jxch...@gmail.com> wrote:
> Ben,
>
> My question is, say if I have a function Print with a native callback
> through function template and set it to the global template, use that to
> create a context, then use serializer to create a snapshot. If I use this
> snapshot to initialize v8, will function Print be pre-defined in that
> snapshot?
>
> Jane

I don't think that has been implemented yet. If it was, there would
have to be some kind of API for mapping addresses because of ASLR.

Jane Chen

unread,
Jun 5, 2015, 2:54:59 AM6/5/15
to v8-u...@googlegroups.com
Thanks Ben.

I did cache my global template through a persistent handle which has a bunch of function templates and object templates set.  The existence for the function templates is to support instanceof operator on our core value types (which you helped me with).

I understand why creating static snapshots with function callbacks is problematic.  But in my case where contexts are created frequently, it would still make a big performance difference if I could snapshot a runtime image with native callbacks after my application starts, and use that for subsequent context creations.  Does that sound more promising?  And is that what V8::CreateSnapshotDataBlob enables?

Jane Chen

unread,
Jun 6, 2015, 2:32:54 AM6/6/15
to v8-u...@googlegroups.com
Ben,

The SetAccessor() idea was great.  It helped cut the context creation time significantly.

Thanks again!
Reply all
Reply to author
Forward
0 new messages