{"id":21028748,"url":"https://github.com/jgmdev/lessram","last_synced_at":"2025-05-15T11:31:45.648Z","repository":{"id":56998669,"uuid":"193581781","full_name":"jgmdev/lessram","owner":"jgmdev","description":"Pure PHP implementation of array data structures that use less memory.","archived":false,"fork":false,"pushed_at":"2019-07-19T23:03:37.000Z","size":129,"stargazers_count":19,"open_issues_count":0,"forks_count":0,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-04-03T08:04:52.243Z","etag":null,"topics":["data","extension","less","memory","php","ram","structures"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jgmdev.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-06-24T21:16:06.000Z","updated_at":"2024-01-10T20:23:24.000Z","dependencies_parsed_at":"2022-08-21T14:50:21.202Z","dependency_job_id":null,"html_url":"https://github.com/jgmdev/lessram","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jgmdev%2Flessram","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jgmdev%2Flessram/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jgmdev%2Flessram/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jgmdev%2Flessram/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jgmdev","download_url":"https://codeload.github.com/jgmdev/lessram/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254330720,"owners_count":22053034,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["data","extension","less","memory","php","ram","structures"],"created_at":"2024-11-19T11:58:52.618Z","updated_at":"2025-05-15T11:31:45.258Z","avatar_url":"https://github.com/jgmdev.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# LessRam\n\nPHP implementations of array data structures that consume less memory\nthan native php arrays with the drawback of been less performant. For a faster\nversion check the C implementation on the \n[ext](https://github.com/jgmdev/lessram/tree/master/ext) directory.\n\n## Usage\n\nCurrently the project only includes two array structures that implement\nArrayAccess, Countable, Serializable and Iterator, these two structures are:\n\n* StaticArray - Can dynamically grow but elements on it can not be modified.\n* DynamicArray - Can dynamically grow and elements on it can be modified or removed.\n\nBoth of them only support numeric keys, additional implementations with support\nfor string indexes may be added later.\n\n**StaticArray Example:**\n\n```php\n$list = new LessRam\\StaticArray();\n$list[] = \"value1\";\n$list[] = \"value2\";\n$list[] = [\"index\" =\u003e \"somearray\"];\n\nforeach($list as $value)\n{\n  echo $value;\n}\n\n$list-\u003eclear();\n```\n\n**DynamicArray Example:**\n\n```php\n$list = new LessRam\\DynamicArray();\n$list[] = \"value1\";\n$list[] = \"value2\";\n$list[] = [\"index\" =\u003e \"somearray\"];\n\nforeach($list as $value)\n{\n  echo $value;\n}\n\n$list[0] = \"newvalue\";\nunset($list[1]); // Remove value2\nprint_r($list[1]); // show array which now took place of value2\n\n$list-\u003eclear();\n```\n\nAs you can see on the examples above, beside storing strings they can also\nstore arrays and even objects, when inserting such variables they are serialized\nwhich results in memory savings, and unserilizing when access to it is needed.\nThis saves a lot of memory but impacts performance, so you should weight your\nrequirements. For more info on performance check the benchmarks below.\n\n## Benchmark\n\nThe first results shown below are inserting a string to each of the tested\ndata structures 1048577 times, the first test doesn't uses gzip compression while\nthe second test does for SplFixedArray and PHP native array:\n\n```php\n$list[] = \"hello world\" . $i;\n```\n\n### Results\n\n```\n=======================================================================\nString store test:\n=======================================================================\n        Measure | StaticArray     | DynamicArray    | SplFixedArray   | Native\n    add 1048577 | 0.42s           | 0.29s           | 0.10s           | 0.15s\n  get two items | 0.00s           | 0.00s           | 0.00s           | 0.00s\n       loop all | 1.31s           | 2.11s           | 0.03s           | 0.02s\n      serialize | 0.08s           | 0.04s           | 0.20s           | 0.31s\n    unserialize | 0.05s           | 0.03s           | 0.34s           | 0.15s\n     total time | 1.87s           | 2.48s           | 0.69s           | 0.66s\n   memory usage | 29MB            | 19MB            | 129MB           | 113MB\n\n=======================================================================\nString store test gzipped (StaticArray and DynamicArray not compressed):\n=======================================================================\n        Measure | StaticArray     | DynamicArray    | SplFixedArray   | Native\n    add 1048577 | 0.44s           | 0.35s           | 31.95s          | 10.72s\n  get two items | 0.00s           | 0.00s           | 0.00s           | 0.00s\n       loop all | 1.28s           | 2.10s           | 0.03s           | 0.02s\n      serialize | 0.08s           | 0.05s           | 0.44s           | 0.16s\n    unserialize | 0.05s           | 0.03s           | 0.28s           | 0.15s\n     total time | 1.86s           | 2.53s           | 32.71s          | 11.09s\n   memory usage | 29MB            | 19MB            | 129MB           | 113MB\n```\n\nThis second test stores an array inside the data structure 1048577 times\nas follows:\n\n```php\n$list[] = [\"name\" =\u003e \"hello world\" . $i];\n```\n\n### Results\n\n```\n=======================================================================\nArray store test:\n=======================================================================\n        Measure | StaticArray     | DynamicArray    | SplFixedArray   | Native\n    add 1048577 | 0.79s           | 0.60s           | 0.34s           | 0.24s\n  get two items | 0.00s           | 0.00s           | 0.00s           | 0.00s\n       loop all | 1.99s           | 4.36s           | 0.23s           | 0.12s\n      serialize | 0.12s           | 0.08s           | 0.43s           | 0.84s\n    unserialize | 0.08s           | 0.06s           | 1.24s           | 0.67s\n     total time | 2.98s           | 5.10s           | 2.25s           | 2.06s\n   memory usage | 42MB            | 32MB            | 505MB           | 489MB\n\n=======================================================================\nArray store test gzipped (StaticArray and DynamicArray not compressed):\n=======================================================================\n        Measure | StaticArray     | DynamicArray    | SplFixedArray   | Native\n    add 1048577 | 0.76s           | 0.66s           | 70.51s          | 13.86s\n  get two items | 0.00s           | 0.00s           | 0.00s           | 0.00s\n       loop all | 1.96s           | 4.36s           | 0.03s           | 0.03s\n      serialize | 0.11s           | 0.08s           | 0.60s           | 0.74s\n    unserialize | 0.07s           | 0.05s           | 0.38s           | 0.17s\n     total time | 2.91s           | 5.15s           | 71.53s          | 14.85s\n   memory usage | 42MB            | 32MB            | 161MB           | 145MB\n```\n\nI can say that the best balance between memory usage and performance is\nachieved with the **StaticArray** implementation. To run your own benchmarks run:\n\n```sh\ncd lessram\nphp bench.php\n```\n\n## Testing\n\nSome tests have been written to check the reliability of these structures but\nmaybe more tests are needed. To run the tests execute the php unit executable\nas follows:\n\n```sh\ncd lessram\n./vendor/bin/phpunit\n```\n\n## PHP C Extension (WIP)\n\nI started implementing the same algorithm used on the PHP implementation\nof the data structures, lots of stuff needs to be done but the base work\nshould be in place. Check the [ext](https://github.com/jgmdev/lessram/tree/master/ext) \ndirectory for more information and benchmarks.\n\n\n## TODO\n\nAdd associative data structures also known as maps where the index could\nbe any string.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjgmdev%2Flessram","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjgmdev%2Flessram","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjgmdev%2Flessram/lists"}