serafim / packed-array
Typed arrays with reduced memory consumption
Requires
- php: ^8.1
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.15
- phpbench/phpbench: ^1.2
- phpunit/phpunit: ^10.0.14
- symfony/var-dumper: ^6.2
- twig/twig: ^3.5
- vimeo/psalm: ^5.8
This package is auto-updated.
Last update: 2024-12-03 06:10:04 UTC
README
Introduction
PHP packed (typed) arrays are array-like objects that provide a mechanism for reading and writing raw binary data in memory buffers with reduced memory consumption on large amounts of data.
Common PHP arrays grow and shrink dynamically and can have any value. PHP Zend VM perform optimizations so that these arrays are fast. However, in some cases, the standard functionality is not enough and the standard PHP array can take up a very large amount of data, for example, when working with audio, image and video. This is where typed arrays come in. Each entry in a PHP typed array is a raw binary value in one of a number of supported formats, from 8-bit integers to 64-bit floating-point numbers.
For example, an image with a size of 44.968Kb, when loaded into a typed array, takes 45.168Kb of memory. However, if it is unpacked into a native PHP array, then the size of such an image in memory will take 5,633.360Kb. That is more than 120 times!
Below is a graph of the size of the consumed RAM depending on the size of the array (number of elements).
See the bin/memory-usage.php for details on how the this RAM consumption was calculated.
Read/Write
Please note that such arrays are designed to store a large amount of data, however, they are noticeably slower than those builtin PHP arrays during reading and writing.
Reading
Writing
Installation
This library is available as Composer repository and can be installed using the following command in a root of your project:
$ composer require serafim/packed-array
Quick Start
TODO