|
I am looking for a good/the best way to loop up objects that I have stored
based on a combination of attribute values. I want to do this to avoid
creating duplicate objects (instances of the same class with the same
attribute values). The objects concerned will be effectively immutable, and
I want to share them.
Interning as I know it is used for such objects as Symbols, which the
compiler looks up based on their representation as a sequence of characters.
"Interning" objects based on multiple attributes
Also, any time you create a Symbol from a String, something like this
happens:
Symbol>>fromString: aString
^
ExistingSymbols
at: aString
ifAbsentPut: (self newFromString: aString)
It doesn't really work that way, but the idea is the same.
As we can see, it is trivial to intern objects based on one object/one
object's value (in the case of a Symbols, based on one String). You can just
us a dictionary keyed on the lookup object, which will tell you quickly if a
corresponding Symbol already exists.
Now, what I want to do is "intern" objects based on multiple attributes (I
don't know if interning is a usual name for that process, hence the
explanation and the quotes around the word). So, if I wanted an object with
a particular combination of attribute values, I would pass that combination
of attribute values to a provider/factory object and it would return either
an existing instance of the desired class which has those attribute values,
or a newly created one (and keep track of that). Like so:
Provider>>getObject_attribute1: attribute1Boolean
attribute2: attribute2Object
attribute3: attribute3Integer
attribute4: attribute4String
"what to write here..."
"it has to be fast and not generate a lot of garbage"
It looks like the question that needs answering is, what is a good data
structure to use for looking up objects on multiple keys, but I don't want
to prejudge that. Currently my objects have 6 attributes, 4 of which are
booleans, and certain combinations of booleans are much more prevalent than
others (if that is any help).
Thanks is advance,
Peter van Rooijen
Amsterdam
|