What's Faster? WeakHashMap or Reflection?
I'm considering using reflection in my logging API to get a certain value out of an object (by invoking a method). This would be happening *every time* a log statement is output. Seeing as reflection has previously been reported to be really slow, it seems like an obvious idea to cache the returned values (as there will be very high re-use). However, I've heard at some point that the performance of reflection has been increased significantly, almost to the point where it matches normal dynamic invocation.
So, the question is:
Would it be faster to retrieve the value using reflection every time, or will I increase performance by caching the value (in a WeakHashMap)?
Please provide either references to support your reply or credentials to back up your authority on the subject. : )
[816 byte] By [
_grlea_a] at [2007-10-2 22:05:36]

im curious as to how you plan to use WeakHashMap as I have yet to find a way to make use of it!?Also, i dont think you can make a 'cache' with a 'weak' reference.
You use a WeakHashMap when you a creating an object that is expensive to create, but the object used as the input may disappear, and in that case you want your created object to disappear as well.
It is a cache. The weak reference will only be cleared when the object no longer has any hard references.
Anybody have any thoughts on performance?
I do not know the answer but it might depend on the version you are using.
> You use a WeakHashMap when you a creating an object
> that is expensive to create, but the object used as
> the input may disappear, and in that case you want
> your created object to disappear as well.
>
Then why not use a WeakReference? Where does the mapping come into play?
> It is a cache. The weak reference will only be
> cleared when the object no longer has any hard
> references.
>
Thats not a cache, that is a multiton. A cached object would stick around when its not necessarily required. Weak references will not.
> Anybody have any thoughts on performance?
Sorry no. Have never had cause to use reflection. Mostly I see it abused.
I don't even see how the two options do the same thing, or even what it is they do, so I don't see how anybody could give a professional opinion on the relative speeds of the two options. And you weren't interested in ignorant opinions.
In my view (particularly in this case) the only opinion that counts is the one that you form after looking at benchmarks of the two possibilities.
> Then why not use a WeakReference? Where does the mapping come into play?A WeakHashMap is basically a Map<WeakReference><K>, V> which does some extra work to clear out GC'd keys.
> > Then why not use a WeakReference? Where does the
> mapping come into play?
>
> A WeakHashMap is basically a Map<WeakReference><K>, V>
> which does some extra work to clear out GC'd keys.
Yes. I have yet to figure out a good use for this. If you want the object to disappear when the one holding it disappears, then just make the one holding it the only reference to it. Normal reference lifecycle will take care of that.
The only example I have seen published for WeakHashMap (a collection of listeners that can be dropped if they have no references) is essentially a use case for WeakHashSet, as it uses the listeners as keys and null as values.
Lol, no values? Just as soon use a Set of WeakReferences. I have looked for years and I have to conclude that it was just a poor example class someone strung together, but it has no real purpose. Well it did help me to understand some of the issues around managing References by looking at its source code.
> If you want the object to disappear when the one holding it disappears, then just make the one
> holding it the only reference to it.
Sometimes you can't modify the "one holding it". I think I once used a WeakHashMap<Thread, Something> where the threads were on the server side of an RMI-style system and I needed to track what the client knew about. Can't remember any details now, though.
Interesting discussion on WeakHashMap (not), but nothing to do with my question, which is about performance:Is a lookup in a WeakHashMap going to be faster or slower than invoking a get method by reflection?Anyone care to take a guess?
> Interesting discussion on WeakHashMap (not), but
> nothing to do with my question, which is about
> performance:
>
> Is a lookup in a WeakHashMap going to be faster or
> slower than invoking a get method by reflection?
>
> Anyone care to take a guess?
Sure. I'll guess it depends on the quality of your hashes and the size of your map.