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

Increase bson.NewObjectId() performance by caching the process id #294

Merged
merged 1 commit into from
Jul 14, 2016
Merged

Increase bson.NewObjectId() performance by caching the process id #294

merged 1 commit into from
Jul 14, 2016

Conversation

256dpi
Copy link
Contributor

@256dpi 256dpi commented Jul 5, 2016

Every call to bson.NewObjectId() will also call syscall.Getpid() to obtain the process id. This PR caches the id and increases the performance:

Before:
PASS: bson_test.go:1828: S.BenchmarkNewObjectId 10000000  228 ns/op

After:
PASS: bson_test.go:1828: S.BenchmarkNewObjectId 20000000 76.0 ns/op

AFAIK go does not support process forking, the process id should therefore always be the same.

I noticed this while working on a system that writes a lot of data to the database and generates the ObjectId on the client. This PR significantly increases the performance of the said system.

@niemeyer
Copy link
Contributor

Thanks!

@niemeyer niemeyer merged commit ffaa07c into go-mgo:v2-unstable Jul 14, 2016
@256dpi
Copy link
Contributor Author

256dpi commented Jul 15, 2016

Thanks! How long will it take until this change is merged into v2 (stable)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants