#4311 - Make Tempcode use SPL, for reduced memory usage
| Identifier | #4311 |
|---|---|
| Issue type | Feature request or suggestion |
| Title | Make Tempcode use SPL, for reduced memory usage |
| Status | Closed (rejected) |
| Tags |
Type: Performance (custom) |
| Handling member | Chris Graham |
| Addon | core |
| Description | Tempcode will use significantly reduced memory if we switch from arrays to SPL's alternative efficient list mechanisms: SplDoublyLinkedList and SplFixedArray. The very flexible nature of PHP arrays means they use an incredible amount of memory, and Tempcode is built heavily around use of arrays. The SPL structures use a lot less because they are not supersets of every kind of list/map a programmer may ever want.
SPL is available in all PHP installs since IIRC PHP 5.4 and cannot be removed, so we can use it without problems. I have a patch for this that I am attaching, which was built against v10. The problem is it is a major compat break with any Tempcode that is already compiled. We've done this before, as Tempcode was rewritten over the years, but now Tempcode caching for content Comcode is individually stored in each content table, we cannot just flush a single table to have it regenerate. |
| Steps to reproduce | |
| Funded? | No |
The system will post a comment when this issue is modified (e.g., status changes). To be notified of this, click "Enable comment notifications".


Comments
It produces significant memory gains (perhaps 25% less usage by Composr overall, but I didn't gather proper figures). However, what I didn't consider is the CPU speed differences, and testing is showing me that SplDoublyLinkedList takes 3x time to do an array append and 2x time to do an array iteration. I'd rather optimise for speed than memory, you can easily add more RAM and generally Composr has more of a practical cost for speed than memory use.
PHP 7 is highly optimised with an engine-based array implementation. SPL cannot benefit from that and has to go through PHP's object interfaces for overriding array operations.
Here's an interesting discussion around a new PHP extension that does what SPL is doing but faster:
https://www.reddit.com/r/PHP/comments/44qsco/efficient_data_structures_for_php_7/
Maybe it will eventually reach core, or something similar will, in which case we can eventually use it.