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

add Encode method for memory sensitive cases #56

Merged
merged 1 commit into from
Jul 19, 2020
Merged

add Encode method for memory sensitive cases #56

merged 1 commit into from
Jul 19, 2020

Conversation

phuslu
Copy link
Contributor

@phuslu phuslu commented Jul 19, 2020

xid is wonderful for tracing, it would be nice to add a Encode method, enabling us to write zero allocation servers. Thanks.

Signed-off-by: phuslu phus.lu@gmail.com

Signed-off-by: phuslu <phus.lu@gmail.com>
@rs rs merged commit f1b4319 into rs:master Jul 19, 2020
@phuslu phuslu deleted the encode branch July 19, 2020 16:26
danmcgee-soda added a commit to sodahealth/xid that referenced this pull request Jun 8, 2022
Now that an `Encode()` method was added in rs#56, it is less necessary to
resort to unsafe methods in `String()`. This also appears to have
minimal benefit, as seen by these before (using unsafe) and after
(without unsafe) benchmarks:

Before (using unsafe):

% go test -benchmem -bench .
goos: darwin
goarch: arm64
pkg: github.com/rs/xid
BenchmarkNew-8          	21845306	        52.34 ns/op	       0 B/op	       0 allocs/op
BenchmarkNewString-8    	22674129	        51.18 ns/op	       0 B/op	       0 allocs/op
BenchmarkFromString-8   	286217863	         4.233 ns/op	       0 B/op	       0 allocs/op
PASS
ok  	github.com/rs/xid	4.142s

After (without unsafe):

% go test -benchmem -bench .
goos: darwin
goarch: arm64
pkg: github.com/rs/xid
BenchmarkNew-8          	21423278	        53.01 ns/op	       0 B/op	       0 allocs/op
BenchmarkNewString-8    	21977233	        53.67 ns/op	       0 B/op	       0 allocs/op
BenchmarkFromString-8   	289641164	         4.162 ns/op	       0 B/op	       0 allocs/op
PASS
ok  	github.com/rs/xid	4.181s
rs pushed a commit that referenced this pull request Jun 23, 2023
* Remove superfluous bitmasking in `encode`

We only need to mask with `&0x1F` once per generated character, rather
than twice as we did in many cases. As long as the `&0x1F` operation is
last, we will always throw away the top three bits as expected.

* Remove use of `unsafe` in `String()` method

Now that an `Encode()` method was added in #56, it is less necessary to
resort to unsafe methods in `String()`. This also appears to have
minimal benefit, as seen by these before (using unsafe) and after
(without unsafe) benchmarks:

Before (using unsafe):

% go test -benchmem -bench .
goos: darwin
goarch: arm64
pkg: github.com/rs/xid
BenchmarkNew-8          	21845306	        52.34 ns/op	       0 B/op	       0 allocs/op
BenchmarkNewString-8    	22674129	        51.18 ns/op	       0 B/op	       0 allocs/op
BenchmarkFromString-8   	286217863	         4.233 ns/op	       0 B/op	       0 allocs/op
PASS
ok  	github.com/rs/xid	4.142s

After (without unsafe):

% go test -benchmem -bench .
goos: darwin
goarch: arm64
pkg: github.com/rs/xid
BenchmarkNew-8          	21423278	        53.01 ns/op	       0 B/op	       0 allocs/op
BenchmarkNewString-8    	21977233	        53.67 ns/op	       0 B/op	       0 allocs/op
BenchmarkFromString-8   	289641164	         4.162 ns/op	       0 B/op	       0 allocs/op
PASS
ok  	github.com/rs/xid	4.181s
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.

None yet

2 participants