Closed shuhei closed 5 years ago
Oh, we can also remove another step (extracting values from the heap in the order of priority). The values are re-sorted by value anyway.
I pushed a commit and updated the PR description accordingly.
Hi @shuhei, thanks for contributing! At first glance this looks great, I'll give it a closer look tonight.
This looks great, and the benchmark was super helpful for showing the nice improvement from this.
I think there is still a case where there is some value doing a clone/copy, and that's when used with Histogram.prototype.values
. Unfortunately, the documentation doesn't indicate whether getValues()
returns a snapshot or the live evolving data, but that it was doing a copy indicates to me that this was the intent.
If someone were to use that to essentially get a 'snapshot' of the sample for future reference, i.e. show me the values at this point in time, if we don't do a clone future updates to the ExponentiallyDecayingSample
will update these values in place which could be problematic. I noticed that UniformSample
suffers from this same issue, since getValues
just returns the underlying values and not a copy.
I think one work around for this would be to add an optional boolean parameter to both Sample.getValues()
and Histogram.values
.
We can name the parameter snapshot
, and if the parameter isn't provided, we can assume that the value is true
(do a deep copy), this is for backwards compatibility. In the case of Histogram.percentiles
invocation of getValues()
we can simply call getValues(false)
which will not return a copy, as we end up creating a new list with values converted to floats.
What do you think of that solution? I think this way we could maintain the previous behavior if someone was using Histogram.values
and expecting a snapshot, and we would still observe this nice performance benefit when using Histogram.percentiles
.
Thanks again for contributing this improvement! Released as v0.1.21
@tolbertam Thanks a lot for your thorough review and publishing a new version!
Happy Holidays!
The deep-clone of the binary heap seems to be the bottleneck of
Histogram#percentiles()
.I think we can just use the heap items without cloning or sorting by priority because they will be sorted by value in
Histogram
anyway.Benchmark
The updated version is roughly 5x-6x faster on my Macbook Air, Early 2015, 2,2 GHz Intel Core i7.
with the following benchmark code: