Content deleted Content added
m Changing {{cleanup-date}} to {{cleanup-date|December 2005}} |
Clean up |
||
Line 1:
{{wikify}}
In [[computer programming]], an '''object pool''' is a
Object pooling can offer a significant performance boost; it is most effective in situations where the cost of initializing a class instance is high, the rate of instantiation of a class is high, and the number of instantiations in use at any one time are low.
== Handling of empty pools ==
Object pools employ one of three strategies to handle a request when there are no spare objects in the pool.
When writing an object pool, the programmer has to be careful to make sure the state of the objects returned to the pool is reset back to a useable state for the next use of the object. Often if this is not observed, the object will be in some state that was unexpected by the client program and that will cause the client program to fail.▼
# Fail to provide an object (and return an error to the client)
The presence of stale state is not always an issue; it becomes dangerous when the presence of stale state causes the object to behave differently. For example, an object that represents authentication details may break if the "successfully authenticated" flag is not reset before it is passed out, since it will indicate that a user is correctly authenticated (possibly as someone else) when they haven't yet attempted to authenticate. However, it will work just fine if you fail to reset the identity of the last authentication server used, since this is not important in normal operation, but is used for debugging. ▼
# Allocate a new object, thus increasing the size of the pool. Pools that do this usually allow to you see the [[high water mark]] (the maximum number of objects ever used).
# In a multithreaded environment, a pool may block the client until another thread returns an object to the pool.
== Pitfalls ==
▲When writing an object pool, the programmer has to be careful to make sure the state of the objects returned to the pool is reset back to a
▲The presence of stale state is not always an issue; it becomes dangerous when the presence of stale state causes the object to behave differently. For example, an object that represents authentication details may break if the "successfully authenticated" flag is not reset before it is passed out, since it will indicate that a user is correctly authenticated (possibly as someone else) when they haven't yet attempted to authenticate. However, it will work just fine if you fail to reset the identity of the last authentication server used, since this is not important in normal operation, but is used for debugging.
Inadequate resetting of objects may also provide an information leak. If an object contains confidential data (e.g. a user's credit card numbers) that isn't cleared before the object is passed to a new client, a malicious or buggy client may disclose the data to an unauthorized party.
[[Category:Optimizations]]
|