On 2008.03.25., at 15:11, helge wrote:
> Hi,
>
> I wonder whether it makes sense to cache wrapAsJavaObject() results in
> a HashMap. Key would be the Java object reference, value would be the
> Scriptable Wrapper. Both would be weak references.

You can safely use a map with strong keys and weakref values provided  
you actually use a custom subclass of WeakReference that also has an  
extra field for the wrapped object, so you can remove the entries by  
wrapped object key when weak references are cleared/enqueued (you'd of  
course register the references with a refqueue). Reasoning being that  
while the wrapper is more than weakly reachable, so is the wrapped  
object, because the wrapper has a strong reference to the wrapped.

And you'll likely want to use IdentityHashMap :-)

> Did someone already try this? (is the Map/WeakRef overhead worth the
> reduced allocation?)

Well, I wrote a similar wrapper caching mechanism in FreeMarker, and  
it was on by default many years ago. Quite a lot users actually  
measured performance with it and without it and concluded the  
performance is overall better without caching, so it is now off by  
default. I'm not saying it applies to Rhino too, as the price of  
creating a wrapper might be different, but certainly shows that  
intuition might not always be right.

Attila.
--

home: http://www.szegedi.org
weblog: http://constc.blogspot.com

>
>
> Thanks,
>  Helge
> --
> http://www.helgehess.eu/
_______________________________________________
dev-tech-js-engine-rhino mailing list
[email protected]
https://lists.mozilla.org/listinfo/dev-tech-js-engine-rhino

Reply via email to