lots of contexts with the same JS code - reduce memory usage

23 views
Skip to first unread message

mme

unread,
Jan 2, 2010, 5:15:35 PM1/2/10
to v8-users
I am experimenting with having a lot of different contexts(1000+)
running in a single process multi threaded application which share the
same javascript codebase.
Every time a new context is created, I currently run the compiled
javascript source code in the scope of the new context.
Depending on the size of the source code, the memory usage of a single
context goes up to over 1MB in my tests, too much for the application
I have in mind.

Is it possible to either:

.) serialize contexts that have not been used recently to disk and
load them back into memory on demand
.) create a single context containing the javascript definitions once
and "link" new contexts to this context
.) reduce memory usage by creating a custom snapshot that contains my
javascript source at runtime

or would this go against v8's design ?

Is there any other way to reduce memory usage in this case?

Flier Lu

unread,
Jan 22, 2010, 9:03:14 AM1/22/10
to v8-users
v8 provide Extension for the reusable scripts or native code, you
could special the extension when create context

std::auto_ptr<v8::ExtensionConfiguration> cfg;

// fill the array

for (size_t i=0; i<ext_names.size(); i++)
{
ext_ptrs.push_back(ext_names[i].c_str());
}

if (!ext_ptrs.empty()) cfg.reset(new v8::ExtensionConfiguration
(ext_ptrs.size(), &ext_ptrs[0]));

m_context = v8::Context::New(cfg.get());

if you could use python, the code should be like following in pyv8
<http://code.google.com/p/pyv8/source/browse/trunk/demos/ext.py>

from PyV8 import *

firstSrc = "function hello(s) { return 'hello ' + s; }"
firstPy = JSExtension("hello/javascript", firstSrc, register=False)
firstPy.register()


secondSrc = "native function title(s);"
secondPy = JSExtension("title/python", secondSrc, lambda secondfunc:
lambda name: "Mr. " + name, register=False)
secondPy.register()

with JSContext(extensions=['title/python', 'hello/javascript']) as
ctx:
print ctx.eval("hello(title('flier'))")

Reply all
Reply to author
Forward
0 new messages