Skip to content

Fix weak-ref caching - #649

Open
kkin-here wants to merge 2 commits into
v3from
lib_data_cache_fix
Open

Fix weak-ref caching#649
kkin-here wants to merge 2 commits into
v3from
lib_data_cache_fix

Conversation

@kkin-here

Copy link
Copy Markdown

No description provided.

@kkin-here kkin-here changed the title Regression test for weak-ref caching Fix weak-ref caching Aug 18, 2026
@kkin-here
kkin-here force-pushed the lib_data_cache_fix branch 4 times, most recently from 51c3ea7 to 74103a1 Compare August 28, 2026 11:18
@kkin-here
kkin-here marked this pull request as ready for review August 28, 2026 12:39
// All heap references should always be weak-referred to be collectable under memory pressure.
// The partial tuples should use only weak-references, they are very unhandy anyway and should be avoided.
private var tuplesByStorage = AtomicMap<Int64, AtomicMap<TupleNumber, WeakRef<Tuple>>>()
private var tuplesByStorage = AtomicMap<Int64, AtomicMap<TupleNumber, SoftReference<Tuple>>>()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I agree that Soft-References improve the situation, but they are not the final solution. Removing the TODO is not appropriate. The comment was still right, we should somehow control cache size. Having some form of minimal size is still the best solution. Even while there no real solution yet, it does not mean the current one is a good final one.
Please add the TODO back into, we should not use WeakReferences, because they will be collected at ones and go always all together, but we should not stick to only soft-references as well. The best is a mechanism that somehow uses a certain amount of strong references, a bunch of soft-references, and the rest weak-references. However, we need to write a better strategy guide, this is a long term goal.
So, for now, I'm totally fine with the switch from Weak- to Soft-References, but not with removing the TODO.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The comment is still correct for Soft-References, they can as well be selected at any time. It is just less likely.

val tupleNumber = write.tupleNumber
tupleNumbers[write.i] = tupleNumber
val tuple = write.tuple
if (tuple != null) tuplesByIndex[write.i] = tuple

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is a waste of CPU and memory. We have writes, which is a MutableList that is already in order, and every write gets the Tuple attached, why would we want to make another array in the exact same order containing the exact same objects?
P.S.: Be ware that pgWrites is a copy of writes, but sorted by collection, while writes is still the originally ordered list. So tuplesByIndex[write.i] == writes[write.i].tuple.

featureTuples.setCapacity(writes.size)
for (i in 0 until writes.size) {
val tupleNumber = tupleNumbers[i] ?: continue
val tuple = tuplesByIndex[i]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This can be replaced with writes[i].tuple, there is totally no need for yet another array.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why was the comment removed, it is still fully correct?

forceGc()

val cached = Naksha.cache[tn]
assertNotNull(cached, "a write-warmed tuple must survive GC via SoftReference (the weak-only tier dropped it)")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is wrong, there is no guarantee for a Soft-Reference to survive a GC. It is just unlikely that it gets collected. Creating a deterministic test on a (defined) none-deterministic behavior is not acceptable! This may work 99% of times, but I do not want it to fail in CICD for some odd reason, just because exactly there GC pressure is so high that the cache is collected.

Base automatically changed from lib_data to v3 September 1, 2026 08:26
Signed-off-by: kkin-here <284318677+kkin-here@users.noreply.github.com>
Signed-off-by: kkin-here <284318677+kkin-here@users.noreply.github.com>
@github-actions

github-actions Bot commented Sep 1, 2026

Copy link
Copy Markdown

Code Coverage

Overall Project 60.38% -0.02% 🍏
Files changed 85.03% 🍏

File Coverage
PgWriter.kt 86.36% -0.78% 🍏
TupleHeapCache.jvm.kt 81.41% -7.04% 🍏
PgSession.kt 60.99% 🍏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants