Struct pools for colors, events, points, and rects
It is common for games to need temporary points and rects, and sometimes colors and events. These temporary structs are allocated, used briefly for some calculation, and then discarded. This is causes "churn" which negatively affects performance. Performance can be improved if existing structs are re-used, but this adds complexity to the user's code, and prevents them from coding in a functional style. It would be nice if chicken-sdl2 offered a simple and functional way to re-use color, event, point, and rect instances.
One possible way to do this would be by providing "pools", which hold a collection of pre-allocated struct pointers. The user could "borrow" an instance from the pool, and the pool would choose an available pointer, and wrap it in a record instance. The user would then use the instance for some temporary purpose, then discard it when no longer needed. When the record instance is garbage collected, the struct pointer is not freed, but instead it is marked as being available in the pool. Then the next time the user requests an instance, that struct pointer could be reused.
Record instances that are borrowed from the pool would need a flag to mark the fact that the memory should not be freed if the user tries to manually free them, e.g. with
This feature could go well with #33 (efficient arrays). A pool could be implemented as an array of structs, plus a vector of booleans for recording which structs are currently being used.
As a possible extension to this idea, we could make the pools expandable by storing a list of arrays of structs, and a list of vectors of booleans. Then if all available structs are being used, a new array and a new vector can be created and added to the pool.