-
Notifications
You must be signed in to change notification settings - Fork 138
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
Small optimizations #139
Small optimizations #139
Conversation
We can know when the file is required whether or not `Process.clock_gettime` is supported. At that time, we can define a method that calls the appropriate time handler depending on what is supported by the current Ruby. This reduces the overhead of the `time` method because it doesn't have to check whether Process supports monotonic time on every call
Hello @tenderlove (nice nickname btw) and thanks a lot for your contribution! I'm going to do couple tests and I will review the PR. This is actually very smart optimizations! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great job! That's really smart and useful optimizations. But could you check the CI and fix the number of allocations comparing to each ruby versions:
https://travis-ci.org/github/DataDog/dogstatsd-ruby/builds/668978123?utm_source=github_status&utm_medium=notification
We can avoid allocating time objects when we need to test if it's time to send telemetry
3ef37ff
to
3187c3f
Compare
Hi @tenderlove, I have fixed the tests for you. Would you be okay for me to push to your branch so that I can update the PR? Or could you cherry-pick the commits on this branch? https://github.com/DataDog/dogstatsd-ruby/tree/tenderlove/small-optimizations |
@kbogtob I pulled your changes in. Thanks! |
Super! I'll merge the PR right after #142. |
Hi,
I've added a couple small optimizations. The first one removes a runtime check of a constant. We can know whether or not
Process.clock_gettime
is supported at file require time, so we can eliminate a conditional intime
.The other optimization eliminates object allocations inside the
Telemetry
class. We can (and should) use monotonic time there in order to eliminate allocations and also not be subject to clock changes. Unfortunately that file seems to be tested withTimecop
, and Timecop apparently doesn't support mockingProcess.clock_gettime
: travisjeffery/timecop#220The changes I made should be equivalent to the
Time.now.to_i
call. I will try to get the tests passing later, but maybe someone more familiar can do it too?I used this as a benchmark:
On master, I get:
On this branch I get:
I also measured allocations. Here is the benchmark for allocations:
On master:
On this branch:
I guess that's about 11 allocations saved per call to
time
.