> I'm unsure of exactly how this might work so defer to an Internals export, > but having previously read @Nikita Popov <nikita....@gmail.com>'s great > post on PHP's arrays, I did wonder if by knowing the data type within an > array and that it'd conform to a strict structure, could the array itself > be stored in an alternative way in C? Perhaps a more memory efficient way, > or one that's faster to iterate over rather than just a hash table?
> Link to this article for reference: > https://nikic.github.io/2012/03/28/Understanding-PHPs-internal-array-implementation.html A significant improvement that could be made to memory efficiency is to reduce the minimum array size for *dynamically* created arrays. Currently, it's always 8 and must be a power of 2, even if there's only one element or the array is packed (i.e. a list without gaps) - Array constants cached in opcache already have this optimization. I have a PR that reduces the minimum size to 2 - the largest blockers were getting a realistic idea of whether commonly used applications would see a decrease or increase in runtime, and needing code review. ("I'd want to know how this would be benchmarked before adding more changes, in case additional optimizations somehow turn out to be a performance regression.") > For example, Phan's memory usage for self-analysis (./phan > --print-memory-usage-summary) > went from 444MB/576MB to 387MB/475MB (end memory/max memory) with this patch. > (This is a static analyzer for PHP which heavily uses small arrays) ---- > I did wonder if by knowing the data type within an > array and that it'd conform to a strict structure > could the array itself be stored in an alternative way in C? It might theoretically be possible to optimize the memory of an `int[]` (64 bits per element) without references or gaps in elements. If a reference in an element got added, or it stopped being a packed array without gaps (i.e. a list), then such an implementation would use an unoptimized version that also enforces the type constraint (128 bits per list element with zvals). It would probably require changing a lot of APIs and macros, and I'd have no idea how to implement it. I think JavaScript engines do something similar by having specializations for JS arrays of integers, arrays of floats, arrays of small (e.g. 16-bit) integers, etc. -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php