Skip to content

Instantly share code, notes, and snippets.

@MarkMaldaba
Last active July 9, 2019 10:29
Show Gist options
  • Save MarkMaldaba/a6f44f1da32080328b11 to your computer and use it in GitHub Desktop.
Save MarkMaldaba/a6f44f1da32080328b11 to your computer and use it in GitHub Desktop.
Long key names - memory usage demonstration
<?php
ReportMemoryUsage("Start Usage");
$arr = array();
for ($i = 0; $i < 10000; $i++) {
$arr[] = array(true);
}
ReportMemoryUsage("After no keys");
unset($arr);
ReportMemoryUsage("Array reset");
///////////////////////////////////////////////////////////////////////////////
$arr = array();
for ($i = 0; $i < 10000; $i++) {
$arr[] = array('a' => true);
}
ReportMemoryUsage("After short keys");
unset($arr);
ReportMemoryUsage("Array reset");
///////////////////////////////////////////////////////////////////////////////
$arr = array();
for ($i = 0; $i < 10000; $i++) {
$arr[] = array('1234567890abcdefghijklmnopqrstuvwxyz' => true);
}
ReportMemoryUsage("After long keys");
///////////////////////////////////////////////////////////////////////////////
function ReportMemoryUsage($Label) {
$Label = str_pad($Label . ":", 18, " ");
$Bytes = memory_get_usage();
$Bytes = str_pad(number_format($Bytes), 12, " ", STR_PAD_LEFT);
print($Label . $Bytes . "\n");
}
?>
@MarkMaldaba
Copy link
Author

This file demonstrates how choice of key names can affect memory usage requirements quite drastically when building large arrays in PHP.

Example output:

Start Usage:           363,824
After no keys:       2,909,624
Array reset:           363,904
After short keys:    2,909,616
Array reset:           363,904
After long keys:     3,229,608

I know that PHP performs some amount of internal variable optimisation - for example copying a variable by value won't create a new instance of that variable's contents in memory until the variable is modified, which keeps the memory footprint down for a lot of common situations.

I was wondering if there was a similar optimisation for array keys, whereby the key name was stored in only one place in memory, and internally the array index was a fixed-size pointer to this key. If this were the case, choice of key names would make no difference to memory usage when you scale it up.

However, it appears that each instance of the key is being stored separately, and therefore when dealing with large structured arrays, short key names will be preferable if memory-usage is a concern.

This is a bit of a blow, as I strongly feel that maintainability is the most important factor in software design and really don't want to encourage the use of cryptic short names for array keys. However, for very large arrays it may be that this becomes a practical necessity.

Important Note: I don't recommend switching to short array keys for standard arrays in the slightest. The memory saving will be small and the detrimental affect on maintainability will be big. This advice should only be followed for very large structured arrays, and even then you should profile (e.g. using a variant of the above script) to see how much difference it makes, before committing to cryptifying your code.

These tests were carried out on PHP 5.3.8. It will be interesting to see if the memory requirements are improved by later PHP versions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment