Skip to content

Commit

Permalink
proc: cache result of GetG
Browse files Browse the repository at this point in the history
Benchmark before:

BenchmarkConditionalBreakpoints-4              1        15929810602 ns/op

Benchmark after:

BenchmarkConditionalBreakpoints-4   	       1	11570508729 ns/op

Conditional breakpoint evaluation 1.6ms -> 1.2ms

Updates go-delve#1549
  • Loading branch information
aarzilli committed Jan 25, 2020
1 parent ee22c04 commit 3611dbe
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
3 changes: 3 additions & 0 deletions pkg/proc/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ func (t *Target) SupportsFunctionCalls() bool {
// This should be called anytime the target process executes instructions.
func (t *Target) ClearAllGCache() {
t.gcache.Clear()
for _, thread := range t.ThreadList() {
thread.Common().g = nil
}
}

func (t *Target) Restart(from string) error {
Expand Down
5 changes: 5 additions & 0 deletions pkg/proc/threads.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ func (tbe ErrThreadBlocked) Error() string {
// implementations of the Thread interface.
type CommonThread struct {
returnValues []*Variable
g *G // cached g for this thread
}

// ReturnValues reads the return values from the function executing on
Expand Down Expand Up @@ -478,6 +479,9 @@ func newGVariable(thread Thread, gaddr uintptr, deref bool) (*Variable, error) {
// In order to get around all this craziness, we read the address of the G structure for
// the current thread from the thread local storage area.
func GetG(thread Thread) (*G, error) {
if thread.Common().g != nil {
return thread.Common().g, nil
}
if loc, _ := thread.Location(); loc != nil && loc.Fn != nil && loc.Fn.Name == "runtime.clone" {
// When threads are executing runtime.clone the value of TLS is unreliable.
return nil, nil
Expand Down Expand Up @@ -513,6 +517,7 @@ func GetG(thread Thread) (*G, error) {
if loc, err := thread.Location(); err == nil {
g.CurrentLoc = *loc
}
thread.Common().g = g
return g, nil
}

Expand Down

0 comments on commit 3611dbe

Please sign in to comment.