-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
ChunkedAssociativeLongArray re-use expired nodes #1145
ChunkedAssociativeLongArray re-use expired nodes #1145
Conversation
Hi @arteam, |
Here is great illustration of chunks reuse 😄 . link |
Hi @ryantenney, thank you for merge. |
I didn't merge, but only because I didn't want to screw up the release @arteam was planning! Just waiting for him to review and merge! |
@@ -22,11 +43,34 @@ | |||
this.defaultChunkSize = chunkSize; | |||
} | |||
|
|||
private Chunk allocateChunk() { | |||
SoftReference<Chunk> chunkRef = chunksCache.pollLast(); |
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.
What do you think about simplifying this method to:
while (true) {
final SoftReference<Chunk> chunkRef = chunksCache.pollLast();
if (chunkRef == null) {
return new Chunk(defaultChunkSize);
}
final Chunk chunk = chunkRef.get();
if (chunk != null) {
chunk.cursor = 0;
chunk.startIndex = 0;
chunk.chunkSize = chunk.keys.length;
return chunk;
}
}
|
||
private final int defaultChunkSize; | ||
/* | ||
We use this ArrayDeque as cache to store chunks that are expired and removed from main data structure. |
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.
Please use multi-line comments with an asterix on every line. We are trying to follow the Google Java Style Guide. See section about comments.
Hi @arteam, |
This change gives
ChunkedAssociativeLongArray
ability to store chunks that are expired and removed from main data structure.Then instead of allocating new Chunk immediately we are trying to poll one from this cache.
So if you have constant or slowly changing load ChunkedAssociativeLongArray will never throw away old chunks or allocate new ones which makes this data structure almost garbage free. Also chunks stored in cache as
SoftReference
s so this memory can actually be freed if nesesary.Numbers:
In
ReservoirBenchmark
: updates are 5.9x faster and ~370x less time spent in GC thanSlidingTimeWindowReservoir
.In
SlidingTimeWindowReservoirsBenchmark
: updates are 3.2x faster, reads - 2.7x faster, 37x less time spent in GC. (here we see small difference because most time we spent in cleaning snapshots)Graphs:
Comparison of implementations under constant load [duration 1h]
Comparison of implementations under changing load [duration 1h]