946
edits
(Import from http://www.mozilla.org/performance/footprint-reduction-techniques.html by Alec Flett <alecf@flett.org>) |
No edit summary |
||
(One intermediate revision by the same user not shown) | |||
Line 99: | Line 99: | ||
}; | }; | ||
Notice that there is no per-class method to determine the opaqueness of an object. The new subclasses are much simpler, and they declare their attributes during construction, rather than every time some virtual method is called. | |||
But there are tradeoffs! The base class now has a new member variable, mOpaque. You've just increased the size of this object by at least 1 byte, and perhaps you've increased it even more depending on the layout of the base class. For this example, the tradeoff is questionable, and the value of this tradeoff depends heavily on the use of these classes. | But there are tradeoffs! The base class now has a new member variable, mOpaque. You've just increased the size of this object by at least 1 byte, and perhaps you've increased it even more depending on the layout of the base class. For this example, the tradeoff is questionable, and the value of this tradeoff depends heavily on the use of these classes. | ||
Line 175: | Line 175: | ||
= Too much class specialization = | = Too much class specialization = | ||
= Huge switch statements = | |||
It is common to write a large switch statement to account for a number of common actions. However, a large switch statement can also result in slow, bloaty code as the compiler generates a series of test-and-jumps. This is equivalent to a large loop, and can result in blowing out the instruction cache. | It is common to write a large switch statement to account for a number of common actions. However, a large switch statement can also result in slow, bloaty code as the compiler generates a series of test-and-jumps. This is equivalent to a large loop, and can result in blowing out the instruction cache. | ||
Line 220: | Line 220: | ||
} | } | ||
= Heap-based readonly tables = | |||
In trying to create a lookup table, it is common to make a static list of name/value pairs and insert them into a hashtable. The hashtable ends up taking up space on the heap and may contain duplicated values from the original static list. | In trying to create a lookup table, it is common to make a static list of name/value pairs and insert them into a hashtable. The hashtable ends up taking up space on the heap and may contain duplicated values from the original static list. | ||
Line 230: | Line 230: | ||
A better mechanism would be to generate the hashtable at compile time such that it is entirely declared as static data. This is a work in progress. | A better mechanism would be to generate the hashtable at compile time such that it is entirely declared as static data. This is a work in progress. | ||
= Using pointers to member classes = | |||
When a class contains other classes as member variables, it is often unnecessary to include those members as pointers to the member classes. Instead, include the member variable directly. This will save 4 bytes (one pointer) in the total size of your class. | When a class contains other classes as member variables, it is often unnecessary to include those members as pointers to the member classes. Instead, include the member variable directly. This will save 4 bytes (one pointer) in the total size of your class. |
edits