DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Coding
  3. Languages
  4. The Internals of PHP Associative Arrays

The Internals of PHP Associative Arrays

John Esposito user avatar by
John Esposito
·
Dec. 30, 11 · Interview
Like (0)
Save
Tweet
Share
5.57K Views

Join the DZone community and get the full member experience.

Join For Free

You may understand arrays, and you may have used PHP arrays a thousand times before. But did you know that, because of the way PHP uses hashtables to store arrays internally, certain kinds of array insertions, of a given length, can take tens of thousands of times longer than other insertions, of the same length?

To understand why this happens, take a look at this code by Nikita Popov (who has actually written a PHP parser in PHP):

<?php echo '<pre>';

$size = pow(2, 16); // 16 is just an example, could also be 15 or 17

$startTime = microtime(true);

$array = array();
for ($key = 0, $maxKey = ($size - 1) * $size; $key <= $maxKey; $key += $size) {
    $array[$key] = 0;
}

$endTime = microtime(true);

echo 'Inserting ', $size, ' evil elements took ', $endTime - $startTime, ' seconds', "\n";

$startTime = microtime(true);

$array = array();
for ($key = 0, $maxKey = $size - 1; $key <= $maxKey; ++$key) {
    $array[$key] = 0;
}

$endTime = microtime(true);

echo 'Inserting ', $size, ' good elements took ', $endTime - $startTime, ' seconds', "\n";

The crucial lines are 8 and 19 (as you may already have guessed). Inserting the 2^16 evil elements takes 33 seconds on Nikita's machine,versus 0.014 seconds for the good elements.

What's the difference between good and evil elements? Notice the maxKey calculation. The number of elements inserted is the same in both cases, but the max key value is 65535*65536 in the first case, and 65535 in the second; and while the first key is the same in both cases, the second evil element is 65535+65536, while the second good element is 65536. And so forth, with all evil elements as multiples of 65536.

So what's special about these 'evil' values? 

Nikita's explanation is a little complicated, but completely clear; it takes a few steps, but touches on some pretty low-level PHP issues, so it's an intriguing read.

Later in the article Nikita discusses how this peculiarity of PHP's array hashtabling allows for e.g. a DOS attack (though this will disappear soon). Comments are interesting too, so check out the full article and peek under the hood of those ubiquitous PHP arrays.

PHP

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Browser Engines: The Crux of Cross-Browser Compatibility
  • Best Practices for Writing Clean and Maintainable Code
  • Beyond Coding: The 5 Must-Have Skills to Have If You Want to Become a Senior Programmer
  • Specification by Example Is Not a Test Framework

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: