Performance Optimization for Script::Run()

81 views
Skip to first unread message

Jun Liang

unread,
Jun 19, 2014, 7:52:02 PM6/19/14
to v8-u...@googlegroups.com
Hi All,

We are trying to replace a component (of our current product) with V8 engine. I am writing a V8 helper class in C++.

Init(): to initialize V8 engine and compile the given JavaScript.
Run(): to run the compiled script.

The Run() will be called more than 100,000,000 times. Could you give us some suggestions to optimize this part?

Thank you very much!!

-----------------------------------------------------------------------------------------------------------------------------------------------------------------

Persistent<Context, CopyablePersistentTraits<Context>> _context;
Persistent<Script, CopyablePersistentTraits<Script>> _script;

void Init(char *strJavaScript) 
{
strScript = strJavaScript;
this->isolate = Isolate::New();
isolate->Enter();

Isolate::Scope isolate_scope(this->isolate);

HandleScope handle_scope(this->isolate);

Handle<ObjectTemplate> templ = ObjectTemplate::New(this->isolate);
templ->SetAccessor(v8_str("inputStr"), getStringValue, setStringValue, External::New(this->isolate, &inputStr));
templ->SetAccessor(v8_str("inputNum"), getIntValue, setIntValue, External::New(this->isolate, &inputNum));
templ->SetAccessor(v8_str("outputStr"), getStringValue, setStringValue, External::New(this->isolate, &outputStr));
templ->SetAccessor(v8_str("outputNum"), getIntValue, setIntValue, External::New(this->isolate, &outputNum));

Handle<Context> context = Context::New(isolate, NULL, templ);
_context = Persistent<Context, CopyablePersistentTraits<Context>>(isolate, context);
Context::Scope context_scope(context);

// Create a string containing the JavaScript source code.
Handle<String> source = String::NewFromUtf8(isolate, strScript);

// Compile the given JavaScript
Handle<Script> script = Script::Compile(source);
_script = Persistent<Script, CopyablePersistentTraits<Script>>(isolate, script);
}

void Run() 
{
HandleScope scope(Isolate::GetCurrent());
Context::Scope context_scope(Local<Context>::New(Isolate::GetCurrent(), this->_context));

Local<Value> result = Local<Script>::New(Isolate::GetCurrent(), this->_script)->Run();
}

Gregory Hlavac

unread,
Jun 29, 2014, 9:22:31 AM6/29/14
to v8-u...@googlegroups.com
In my usage of V8 to run something repetitively, I had it run once and execute a callback within the script to create a concrete object in the engine that would be executed every update loop cycle, and that object kept handles to callbacks into script-land to handle that sort of thing.

The gist is, don't constantly run it over and over and over again.
Reply all
Reply to author
Forward
0 new messages