Skip to content

Commit

Permalink
manual isolate dispose (#26)
Browse files Browse the repository at this point in the history
* manual isolate dispose

* Add close method for context
  • Loading branch information
rogchap authored Jan 24, 2020
1 parent ed27ac7 commit bce58ca
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 28 deletions.
5 changes: 5 additions & 0 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ func (c *Context) RunScript(source string, origin string) (*Value, error) {
return getValue(rtn), getError(rtn)
}

// Close will dispose the context and free the memory.
func (c *Context) Close() {
c.finalizer()
}

func (c *Context) finalizer() {
C.ContextDispose(c.ptr)
c.ptr = nil
Expand Down
16 changes: 16 additions & 0 deletions context_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package v8go_test

import (
"encoding/json"
"fmt"
"testing"

"rogchap.com/v8go"
Expand Down Expand Up @@ -58,3 +60,17 @@ func TestJSExceptions(t *testing.T) {
})
}
}

func BenchmarkContext(b *testing.B) {
b.ReportAllocs()
vm, _ := v8go.NewIsolate()
defer vm.Close()
for n := 0; n < b.N; n++ {
ctx, _ := v8go.NewContext(vm)
ctx.RunScript(script, "main.js")
str, _ := json.Marshal(makeObject())
cmd := fmt.Sprintf("process(%s)", str)
ctx.RunScript(cmd, "cmd.js")
ctx.Close()
}
}
5 changes: 5 additions & 0 deletions isolate.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,8 @@ func (i *Isolate) finalizer() {
i.ptr = nil
runtime.SetFinalizer(i, nil)
}

// Close will dispose the Isolate VM; subsequent calls will panic
func (i *Isolate) Close() {
i.finalizer()
}
45 changes: 45 additions & 0 deletions isolate_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package v8go_test

import (
"encoding/json"
"fmt"
"math/rand"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -48,3 +51,45 @@ func TestGetHeapStatistics(t *testing.T) {
t.Error("expect NumberOfDetachedContexts return 0, got", hs.NumberOfDetachedContexts)
}
}

func BenchmarkIsolateInitialization(b *testing.B) {
b.ReportAllocs()
for n := 0; n < b.N; n++ {
vm, _ := v8go.NewIsolate()
vm.Close() // force disposal of the VM
}
}

func BenchmarkIsolateInitAndRun(b *testing.B) {
b.ReportAllocs()
for n := 0; n < b.N; n++ {
vm, _ := v8go.NewIsolate()
ctx, _ := v8go.NewContext(vm)
ctx.RunScript(script, "main.js")
str, _ := json.Marshal(makeObject())
cmd := fmt.Sprintf("process(%s)", str)
ctx.RunScript(cmd, "cmd.js")
ctx.Close()
vm.Close() // force disposal of the VM
}
}

const script = `
const process = (record) => {
const res = [];
for (let [k, v] of Object.entries(record)) {
res.push({
name: k,
value: v,
});
}
return JSON.stringify(res);
};
`

func makeObject() interface{} {
return map[string]interface{}{
"a": rand.Intn(1000000),
"b": "AAAABBBBAAAABBBBAAAABBBBAAAABBBBAAAABBBB",
}
}
33 changes: 5 additions & 28 deletions v8go.cc
Original file line number Diff line number Diff line change
Expand Up @@ -188,32 +188,17 @@ void ContextDispose(ContextPtr ptr) {
return;
}
m_ctx* ctx = static_cast<m_ctx*>(ptr);
Isolate* iso = ctx->iso;
Locker locker(iso);
Isolate::Scope isolate_scope(iso);

ctx->ptr.Reset();
if (ctx == nullptr) {
return;
}
ctx->ptr.Reset();
delete ctx;
}

/********** Value **********/

void ValueDispose(ValuePtr ptr) {
m_value* val = static_cast<m_value*>(ptr);
if (val == nullptr) {
return;
}
m_ctx* ctx = val->ctx_ptr;
if (ctx == nullptr) {
return;
}

Isolate* iso = ctx->iso;
Locker locker(iso);
Isolate::Scope isolate_scope(iso);

val->ptr.Reset();
delete val;
delete static_cast<m_value*>(ptr);
}

const char* ValueToString(ValuePtr ptr) {
Expand Down Expand Up @@ -241,11 +226,3 @@ const char* Version() {

}


int _main(int argc, char* argv[]) {
Init();
auto i = NewIsolate();
auto c = NewContext(i);
RunScript(c, "18 + 17", "");
return 0;
}

0 comments on commit bce58ca

Please sign in to comment.