Skip to content

Cache size call for DenseLocalDateDoubleTimeSeries#2162

Merged
wjnicholson merged 3 commits into
masterfrom
topic/densetimeseries-cache-size
May 27, 2020
Merged

Cache size call for DenseLocalDateDoubleTimeSeries#2162
wjnicholson merged 3 commits into
masterfrom
topic/densetimeseries-cache-size

Conversation

@wjnicholson
Copy link
Copy Markdown
Contributor

@wjnicholson wjnicholson commented May 22, 2020

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.

…(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)
@wjnicholson wjnicholson requested a review from jodastephen May 22, 2020 08:01
@Override
public int size() {
return (int) validIndices().count();
int s = size;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add comment "threadsafe via racy single check" or similar

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that was my thought, just do an offset by 1!

Will Nicholson added 2 commits May 27, 2020 11:55
 - static import
 - comment on thread safety
@wjnicholson wjnicholson merged commit 3400235 into master May 27, 2020
@delete-merged-branch delete-merged-branch Bot deleted the topic/densetimeseries-cache-size branch May 27, 2020 14:18
@jodastephen jodastephen added this to the v2.8 milestone Sep 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants