Opened 16 years ago

Closed 16 years ago

#123 closed enhancement (fixed)

improve performance of hash and equality functions

Reported by: Stephen Siegel Owned by:
Priority: major Milestone:
Component: multiple Version: 1.0
Keywords: Cc:

Description

Consider the ValueIF class. Even though values are immutable and produced by a factory using the flyweight pattern, every time someone puts a value in a hash table, the hash code has to be re-computed. This can be expensive since it involves recursive calls on the components of the value. (Ditto for the equals methods.) This is dumb.

Possible solutions:

create a "hashCode" field, initially -1. The hashCode method checks that field. If it is -1 then it computes the hashCode in the usual way and stores the result in the hashCode field, else it just returns the result from the hashCode field. This is OK because of immutability.

just use == for equality outside of the factory itself. (Disadvantage: what if one day we change our mind about using the flyweight pattern.)

Add a boolean field that is true iff this object is "the" canonical representative of its equivalence class. Then re-implement equals so that it checks if both those flags are true, and, if so, just returns the result of using ==.

Someone needs to think about this carefully, not just for values.

Change History (1)

comment:1 by Stephen Siegel, 16 years ago

Resolution: fixed
Status: newclosed

Done. Check out value package.

Note: See TracTickets for help on using tickets.