Cache size call for DenseLocalDateDoubleTimeSeries#2162
Merged
Conversation
…(N) lookup time Profiling revealed the `isEmpty()` check in `.get(LocalDate)` to be a hotspot as it could potentially traverse a reasonable amount of the array (density is required to be >0.7 to become dense, so potentially up to 0.3 of a large array could be traversed, but additionally this isn't preserved when doing head or tail of the timeseries)
jodastephen
suggested changes
May 26, 2020
| @Override | ||
| public int size() { | ||
| return (int) validIndices().count(); | ||
| int s = size; |
Contributor
There was a problem hiding this comment.
Add comment "threadsafe via racy single check" or similar
Contributor
Author
There was a problem hiding this comment.
@jodastephen Just thinking - is this correct if we're using -1 as the unset value rather than 0? I can't seem to find documentation on whether one will always see a "set" value for the field or whether it can see the "unset value" of 0 before we would set it to -1.
Contributor
There was a problem hiding this comment.
True. I've never seen a case with -1 as the default. 0 has special meaning in the JVM, so best to stick with 0 as the default and adjust the size to cope (add one or size 0 -> -1)
Contributor
Author
There was a problem hiding this comment.
Yeah that was my thought, just do an offset by 1!
added 2 commits
May 27, 2020 11:55
- static import - comment on thread safety
jodastephen
approved these changes
May 27, 2020
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Current implementation has an O(N) lookup time.
Profiling revealed the
isEmpty()check in.get(LocalDate)to be a hotspot as it could potentially traverse a reasonable amount of the array (density is required to be >0.7 to become dense, so potentially up to 0.3 of a large array could be traversed, but additionally this isn't preserved when doing head or tail of the timeseries)Could use a
Suppliers.memoize()call instead but decided against as in several cases we can preserve the size information. Could switch back though if that would be clearer.Also could have cached the
isEmpty()check instead of piggy-backing on size() and a full traversal but that feels like over optimization.