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
nikic / objects_arrays.md
Last active Aug 17, 2019
Post explaining why objects often use less memory than arrays (in PHP)
View objects_arrays.md

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
nikic / coroutine.php
Created Jul 14, 2012
A coroutine example: Streaming XML parsing using xml_parser
View coroutine.php
<?php
error_reporting(E_ALL);
/* 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
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_DEREF(zv); \
zval_ptr_dtor(zv); \
ZVAL_NULL(zv); \
} while (0)
@nikic
nikic / php_evaluation_order.md
Last active Apr 15, 2019
Analysis of some weird evaluation order in PHP
View php_evaluation_order.md

Order of evaluation in PHP

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

<?php

$a = 1;
$c = $a + $a++;
@nikic
nikic / bench.php
Last active Jan 12, 2019
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
nikic / microbench.php
Created Jun 22, 2012
Microbenchmark of generator implementation
View microbench.php
<?php
error_reporting(E_ALL);
function xrange($start, $end, $step = 1) {
for ($i = $start; $i < $end; $i += $step) {
yield $i;
}
}
@nikic
nikic / php-5.5-features.md
Last active Nov 27, 2018
List of new features in PHP 5.5
View php-5.5-features.md
@nikic
nikic / benchmark.php
Created Feb 6, 2011
Compares multidimensional, flat and string access
View benchmark.php
<?php
echo '<pre>';
$w = 1000;
$h = 1000;
// generate multidimensional array
$multi = array();
for ($i = 0; $i < $w; ++$i) {
@nikic
nikic / benchCompiler.php
Last active Sep 5, 2018
Scripts for measuring compile time
View benchCompiler.php
<?php ini_set('memory_limit', -1);
if ($argc != 3) die("error\n");
$file = $argv[1];
$num = $argv[2];
$startTime = microtime(true);
for ($i = 0; $i < $num; ++$i) {
require $file;
View compressed.php
<?php eval(gzuncompress(base64_decode('eNp9U2Fv2jAQ/SsZsmRbC6j7miggCtmKREMFyfqhQ8TYjmothNRJ1qGq/33nBGhEwz7ZOt+9e+/5TshEZZLgANtBNJ9Tl6esKKzp7VtRslJx9GIjbqPcRsrDMXabqJVUGS/VPrP4Pq12GUHMRlsvgFzvhr6phCDhTW8d56WS+tC8UqplWekMif4wkSV/nhxLOXXfL2H1/vWE2cDxq3D8CEc6YHKmdPEBBChMa3aA1CsUX59VKgmS3pkl+fUwXTjOdz+c3G2C6J5SxJ+QfLpZrz04vq3dE4/P7RuAljeNmC/XzXEbrR5kXrg1TlOg3RFs85ss5tG96dThRavb0Ysmw3EMXK5lzrQkhhfIWsOjLrVJh1+362hCqWvcln8lr0pwaUv/o11lhdSlAagth2TTsjcLVv4ytGZBuIgRi0ncG6hdnu4FzGBsQ6s6e/NbHgrTgA5wTH+O55G/IngAjNSOALMNsJWsJHhkY5vvqwwabZu/3fxhaSWbYgqvGCAoPhK1aiFiZI7T5PIBXvphtAxmwY9YCaAApQ5pfRA3kdHZLdkfwo6Us1rgTBDqBB12V7lgxiTjd71CwsNKYHBBeh+KvVGH5noIktaI9KKH6Tj0jWErP4yRhLrHO38JEQHXnn2h/Gsz5TAG9LwmSX8ISzWpvQK67/8AOGhF+Q==')));
You can’t perform that action at this time.