php arrays
DESCRIPTION
The good, the bad and the ugly of the PHP Arrays. Lets try to fix it some, shall we?TRANSCRIPT
PHP Arrays.The Good, the Bad and the Ugly.
Mahesh Gattani
About Me
● Senior Software Engineer at Lucid Software Inc.● Bachelors degree from IIT Delhi, India in Computer Science.● Interests:
○ Sports, not the adventure types. Cricket anyone?○ Long Drives○ Reading
● Follow me on○ methinking.tumblr.com
What are arrays?
PHP arrays: The good.
● Exceptionally easy to use.● No need to allocate memory.● Indexed and associative at the same time.● Different types of keys allows in the same instance.● Ordered.● ...
PHP arrays are not arrays...They are... Ordered Hash Maps
PHP arrays: The bad
PHP arrays are bad because…
● Runtime○ O(n*n) worst case
■ Why you ask? Because they are maps. More about this later.○ Rehashing.
● Memory Usage○ Whole different beast.
UGLY!
Weird, huh!?
Size: 8?Next Free: 6?
Bucket2, Bucket4?● Collisions!
Hashmaps and collisions
PHP hash function● Integers: Integer mod size● Strings: Hash function DJBX33A
PHP DOS Attack by super colliding arrays!
Fixed in 5.3.9
What to do?
Find your use case.
Ex: Judy array
PHP Interfaces
● Array Access○ Interface to provide accessing objects as arrays
● Iterator○ Interface for external iterators or objects that can be iterated
themselves internally.
● Countable○ Classes implementing this can be used with count() function.
Example
SQLiteDBArray
A simple, in memory, indexed array implementation in PHP using SQLiteDB
https://github.com/maheshgattani/SQLiteDBArray
References
● https://sheriframadan.com/2012/10/a-closer-look-into-php-arrays/● http://nikic.github.io/2011/12/28/Supercolliding-a-PHP-array.html● www.php.net● https://github.com/maheshgattani
Thank you!
Lucidchart● Building the next generation of
collaborative web applications.● VC funded, high growth,
profitable.● Graduates from Harvard, MIT,
Stanford● Team has worked at Google,
Amazon, Microsoft.
https://www.lucidchart.com/jobs