Skip to content

Instantly share code, notes, and snippets.

Nikita Popov nikic

Block or report user

Report or block nikic

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
nikic /
Last active May 24, 2020
Post explaining why objects often use less memory than arrays (in PHP)

Why objects (usually) use less memory than arrays in PHP

This is just a small post in response to [this tweet][tweet] by Julien Pauli (who by the way is the release manager for PHP 5.5). In the tweet he claims that objects use more memory than arrays in PHP. Even though it can be like that, it's not true in most cases. (Note: This only applies to PHP 5.4 or newer.)

The reason why it's easy to assume that objects are larger than arrays is because objects can be seen as an array of properties and a bit of additional information (like the class it belongs to). And as array + additional info > array it obviously follows that objects are larger. The thing is that in most cases PHP can optimize the array part of it away. So how does that work?

The key here is that objects usually have a predefined set of keys, whereas arrays don't:

nikic / bench.php
Last active Apr 14, 2020
Benchmark of call_user_func_array vs switch optimization vs argument unpacking syntax
View bench.php
<?php error_reporting(E_ALL);
function test() {}
$nIter = 1000000;
$argNums = [0, 1, 2, 3, 4, 5, 100];
$func = 'test';
foreach ($argNums as $argNum) {
nikic /
Last active Mar 4, 2020
Analysis of some weird evaluation order in PHP

Order of evaluation in PHP

Yesterday I found some people on my [favorite reddit][lolphp] wonder about the output of the following code:


$a = 1;
$c = $a + $a++;
View diff_llvm_timings.php
// Extracting last timings as a convenience here,
// as we're usually interested in the last crate.
// We could also aggregate instead.
$metric = 'user_system';
$timings1 = parse_llvm_timing(extract_last_pass_timings(file_get_contents($argv[1])));
$timings2 = parse_llvm_timing(extract_last_pass_timings(file_get_contents($argv[2])));
$timings1 = array_column($timings1, null, 'name');
nikic / coroutine.php
Created Jul 14, 2012
A coroutine example: Streaming XML parsing using xml_parser
View coroutine.php
/* Data can be send to coroutines using `$coroutine->send($data)`. The sent data will then
* be the result of the `yield` expression. Thus it can be received using a code like
* `$data = yield;`.
/* What we're building in this script is a coroutine-based streaming XML parser. The PHP
nikic / demo.php
Created Dec 26, 2016
Demo for format preserving AST transformation
View demo.php
/* This demo replaces all unqualified function calls and namespaced lookups
* with fully qualified ones. Apart from this change all formatting should
* be preserved. */
use PhpParser\Lexer;
use PhpParser\Parser;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitor;
nikic / benchmark.php
Created Feb 6, 2011
Compares multidimensional, flat and string access
View benchmark.php
echo '<pre>';
$w = 1000;
$h = 1000;
// generate multidimensional array
$multi = array();
for ($i = 0; $i < $w; ++$i) {
nikic / shell_exec
Created Oct 7, 2019
Usage of shell exec operation in packagist top 2k
View shell_exec
`iconv -f utf8 -t ascii --byte-subst="<0x%x>" --unicode-subst="<U+%04X>" 'IBAN_Registry.txt'`
nikic / microbench.php
Created Jun 22, 2012
Microbenchmark of generator implementation
View microbench.php
function xrange($start, $end, $step = 1) {
for ($i = $start; $i < $end; $i += $step) {
yield $i;
nikic / php74_shim.h
Last active Apr 29, 2019
PHP 7.4 shim for the typed properties RFC
View php74_shim.h
#ifndef PHP74_SHIM_H
#define PHP74_SHIM_H
#if PHP_VERSION_ID < 70400
#define ZEND_TRY_ASSIGN_NULL(zv) do { \
zval_ptr_dtor(zv); \
ZVAL_NULL(zv); \
} while (0)
You can’t perform that action at this time.