Eviscerate Parameters

One of the JavaIdioms

Your Java program creates a large number of objects. These objects themselves contain references to ValueObjects as member variables.

                                       * * *

Each Java object requires an extra two or three words of memory to hold the per-object mutex, the reference to the object's Class and other housekeep data used by the runtime. A ValueObject also requires a reference to be stored in the object that is using it, which requires another word of memory. So compared to a raw value, a ValueObject has an overhead of three of four words of memory. This overhead is a problem if you have a very large number of objects -- tens or hundreds of thousands for example. (See ObjectOverheadIsRidiculous?.)

How can you reduce the overhead of using ValueObject instance variables?

                                       * * *

Many Java ValueObject classes provide methods for getting a copy the raw representation of the object. For example, the Date class provides a getTime method that returns the date as the number of milliseconds since the "epoch", as a long value.

However, raw values don't have any methods, and so are far from convenient.

Therefore:

Within an object store the raw representation of a ValueObject property and expose the property as a ValueObject at the object's interface.

For example:

 class Example
 {
     long _timestamp;

public Date getTimestamp() { return new Date( _timestamp ); }

public void setTimestamp( Date date ) { _timestamp = date.getTime(); } }

The use of this pattern saves 3 or 4 words per field, which can be important if you are creating thousands of objects.

However:

In conclusion, only use this pattern if testing has shown that the pattern is absolutely necessary.

CrossSection is a related pattern that uses a similar technique to reduce the memory requirements of an array of objects.


Two words about the title. Eschew Obfuscation!

I don't understand? "Eviscerate Parameters" seems a good description for the pattern. What problem do you have with the title? Could you explain?

I'm not the one who complained, but I have a problem with the title as well. "Eviscerate", in the context, seems to be an obfuscated (and unnecessarily gory) synonym for "change the form of". But the real beef I have is that this pattern seems to have little to do with parameters, but instead with instance variables. A better title might be "Use Primitive Types in Value Objects", or just "Use Primitive Types Instead of Objects" as the context doesn't seem to matter much--the real point of the pattern is that primitive types are more efficient in terms of memory usage than are Object types. --KrisJohnson

How about 'Inline Value Objects' or 'Flatten Value Objects'? -- TomAnderson


EviscerateParameters is a specific use of EvisceratedObjects.

See also: FlyweightPattern, for another solution to this problem.


EditText of this page (last edited January 22, 2003) or FindPage with title or text search