Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use profiles to track object allocation and release. #391

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ func NewContext(opt ...ContextOption) *Context {
ptr: C.NewContext(opts.iso.ptr, opts.gTmpl.ptr, C.int(ref)),
iso: opts.iso,
}
addContext(ctx)
ctx.register()
runtime.KeepAlive(opts.gTmpl)
return ctx
Expand Down Expand Up @@ -123,6 +124,7 @@ func (c *Context) PerformMicrotaskCheckpoint() {
// Access to any values associated with the context after calling Close may panic.
func (c *Context) Close() {
c.deregister()
delContext(c)
C.ContextFree(c.ptr)
c.ptr = nil
}
Expand Down
2 changes: 2 additions & 0 deletions isolate.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func NewIsolate() *Isolate {
ptr: C.NewIsolate(),
cbs: make(map[int]FunctionCallback),
}
addIsolate(iso)
iso.null = newValueNull(iso)
iso.undefined = newValueUndefined(iso)
return iso
Expand Down Expand Up @@ -143,6 +144,7 @@ func (i *Isolate) Dispose() {
if i.ptr == nil {
return
}
delIsolate(i)
C.IsolateDispose(i.ptr)
i.ptr = nil
}
Expand Down
13 changes: 13 additions & 0 deletions v8go_noprofile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright 2023 the v8go contributors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

//go:build !v8goprofile

package v8go

func addIsolate(iso *Isolate) {}
func delIsolate(iso *Isolate) {}

func addContext(ctx *Context) {}
func delContext(ctx *Context) {}
33 changes: 33 additions & 0 deletions v8go_profile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2023 the v8go contributors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

//go:build v8goprofile

package v8go

import "runtime/pprof"

func getProfile(profileName string) *pprof.Profile {
if p := pprof.Lookup(); p != nil {
return p
}

return pprof.NewProfile(profileName)
}

func addIsolate(iso *Isolate) {
getProfile("rogchap.com/v8go/gv8go.Isolate").Add(iso, 1)
}

func delIsolate(iso *Isolate) {
getProfile("rogchap.com/v8go/gv8go.Isolate").Remove(iso)
}

func addContext(ctx *Context) {
getProfile("rogchap.com/v8go/gv8go.Context").Add(ctx, 1)
}

func delContext(ctx *Context) {
getProfile("rogchap.com/v8go/gv8go.Context").Remove(ctx)
}