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

perf: improve get goroutine id #10

Merged
merged 1 commit into from
Aug 11, 2023

Conversation

rfyiamcool
Copy link
Contributor

change

import goroutineID() performance.

test code

package async

import (
	"bytes"
	"fmt"
	"runtime"
	"runtime/debug"
	"strconv"
	"testing"

	"github.com/reugn/async/internal"
)

func TestGoroutineID(t *testing.T) {
	gid, err := goroutineID()
	internal.AssertEqual(t, nil, err)

	ggid, err := goroutineID2()
	internal.AssertEqual(t, nil, err)

	internal.AssertEqual(t, gid, ggid)
}

func BenchmarkGetGroutineID(b *testing.B) {
	for i := 0; i < b.N; i++ {
		_, err := goroutineID()
		if err != nil {
			b.Error("failed to get gid")
		}
	}
}

func BenchmarkGetGroutineID2(b *testing.B) {
	for i := 0; i < b.N; i++ {
		_, err := goroutineID2()
		if err != nil {
			b.Error("failed to get gid")
		}
	}
}

func goroutineID() (uint, error) {
	var id uint
	var prefix string
	_, err := fmt.Sscanf(string(debug.Stack()), "%s %d", &prefix, &id)
	if err != nil {
		return 0, err
	}
	return id, nil
}

func goroutineID2() (uint64, error) {
	b := make([]byte, 64)
	b = b[:runtime.Stack(b, false)]
	b = bytes.TrimPrefix(b, []byte("goroutine "))
	b = b[:bytes.IndexByte(b, ' ')]
	n, err := strconv.ParseUint(string(b), 10, 64)
	return n, err
}

test pass

=== RUN   TestGoroutineID
--- PASS: TestGoroutineID (0.00s)

benchmark result

BenchmarkGetGroutineID
BenchmarkGetGroutineID-12     	  261309	      4575 ns/op	    1712 B/op	       8 allocs/op
BenchmarkGetGroutineID2
BenchmarkGetGroutineID2-12    	  383796	      2994 ns/op	      66 B/op	       2 allocs/op

@codecov-commenter
Copy link

Codecov Report

Merging #10 (033d463) into master (4511bba) will increase coverage by 0.87%.
The diff coverage is 100.00%.

❗ Your organization is not using the GitHub App Integration. As a result you may experience degraded service beginning May 15th. Please install the Github App Integration for your organization. Read more.

@@            Coverage Diff             @@
##           master      #10      +/-   ##
==========================================
+ Coverage   85.15%   86.02%   +0.87%     
==========================================
  Files           8        8              
  Lines         229      229              
==========================================
+ Hits          195      197       +2     
+ Misses         19       18       -1     
+ Partials       15       14       -1     
Files Changed Coverage Δ
goroutine.go 100.00% <100.00%> (+28.57%) ⬆️

Copy link
Owner

@reugn reugn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @rfyiamcool.

@reugn reugn merged commit 6b0c2f3 into reugn:master Aug 11, 2023
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants