Welcome! Please see the About page for a little more info on how this works.

0 votes
in Clojure by

just want to clarify my understanding of that code

	Reference<Keyword> existingRef = table.get(sym);
if(existingRef == null) // keyword was not found
	{
	Util.clearCache(rq, table);  // force cleaning of the dead weak references in the whole cache

           ....create new keyword and store it wrapped into WeakReference in the cache
	}

Maybe i am missing something but what are the reasons/assumptions to force cache clearing from dead weak references on every cache miss if rq is not empty, i.e. there is seq scan of the whole cache to locate dead references and to remove them.
And seq scan is executed during Keyword function calling, i.e. blocks the caller.

Naive alternative is to dedicate a thread which collects the garbage from the cache with a some period (~10 seconds or smth like that)

1 Answer

+1 vote
by
selected by
 
Best answer

clearCache only clears the cache when it has dead weak references in the queue (ie gc ran and forced some to become gc'ed). If those dead references exist, they should be cleared in case one of them is the keyword being interned.

Several strategies were examined when this was implemented, including using a separate thread, which has its own issues with initialization and cleanup.

by
Thanks for explanation!

As i understand hte perfect case for keywords cache usage is using a more-or-less defined set of keyword ideally _all_ fixed during a design-time. So cache is warmed up and working with "light" WeakReference maintenance cost
...