PHP 7 in 2017

Techweek

Bucharest

May 26, 2017

http://talks.php.net/techweek

Rasmus Lerdorf
@rasmus

1980s

1990s

1993

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

#define ishex(x) (((x) >= '0' && (x) <= '9') || ((x) >= 'a' && \
                   (x) <= 'f') || ((x) >= 'A' && (x) <= 'F'))

int htoi(char *s) {
	int     value;
	char    c;

	c = s[0];
	if(isupper(c)) c = tolower(c);
	value=(c >= '0' && c <= '9' ? c - '0' : c - 'a' + 10) * 16;

	c = s[1];
	if(isupper(c)) c = tolower(c);
	value += c >= '0' && c <= '9' ? c - '0' : c - 'a' + 10;

	return(value);
}

void main(int argc, char *argv[]) {
	char *params, *data, *dest, *s, *tmp;
	char *name, *age;

	puts("Content-type: text/html\r\n");
	puts("<HTML><HEAD><TITLE>Form Example</TITLE></HEAD>");
	puts("<BODY><H1>My Example Form</H1>");
	puts("<FORM action=\"form.cgi\" method=\"GET\">");
	puts("Name: <INPUT type=\"text\" name=\"name\">");
	puts("Age: <INPUT type=\"text\" name=\"age\">");
	puts("<BR><INPUT type=\"submit\">");
	puts("</FORM>");

	data = getenv("QUERY_STRING");
	if(data && *data) {
		params = data; dest = data;
    	while(*data) {
			if(*data=='+') *dest=' ';
			else if(*data == '%' && ishex(*(data+1))&&ishex(*(data+2))) {
				*dest = (char) htoi(data + 1);
				data+=2;
			} else *dest = *data;
			data++;
			dest++;
		}
		*dest = '\0';
		s = strtok(params,"&");
		do {
			tmp = strchr(s,'=');
			if(tmp) {
				*tmp = '\0';
				if(!strcmp(s,"name")) name = tmp+1;
				else if(!strcmp(s,"age")) age = tmp+1;
			}
		} while(s=strtok(NULL,"&"));

		printf("Hi %s, you are %s years old\n",name,age);
	}
	puts("</BODY></HTML>");
}

1993

use CGI qw(:standard);
print header;
print start_html('Form Example'),
    h1('My Example Form'),
    start_form,
    "Name: ", textfield('name'),
    p,
    "Age: ", textfield('age'),
    p,
    submit,
    end_form;
if(param()) {
    print "Hi ",em(param('name')),
        "You are ",em(param('age')),
        " years old";
}
print end_html;

1994-1995

<html><head><title>Form Example</title></head>
<body><h1>My Example Form</h1>
<form action="form.phtml" method="POST">
Name: <input type="text" name="name">
Age: <input type="text" name="age">
<br><input type="submit">
</form>
<?if($name):?>
Hi <?echo $name?>, you are <?echo $age?> years old
<?endif?>
</body></html>

Story Time

  • 1995 computers were slow!
  • 32M of memory was a lot! (typical home PC had 8)
  • 100 Mbps ethernet didn't exist yet
  • The data volume wasn't necessarily smaller
SELECT * FROM logins
  • 1 second per MB transferred
  • Ate up server memory quickly
  • Rarely needed them all
mSQL 1.0 Patch 9         20 - Oct - 1995
---------------------------------------

    o Fixed close of stdin in keyFD handling (new key code)
    o Fixed memory leak in the client library
    o Fixed force close of UNIX socket when over-ridden by the
        environment variable
    o Fixed a couple of core dumps introduced by the new key code
    o Replace sorting code with a new algorithm (mmap only)
    o Fixed bug in msqlUpdate() that reported a duplicate key error
        if you updated the key field to the same value.
    o Merged in the LIMIT patch from Rasmus Lerdorf <rasmus@io.org>
    o Fixed locale problem with LANG make variable
    o Inlined a few calls to bcopy and bcmp for performance
    o Removed libmisc.a  All misc finctions are now in libmsql.a
    o Fixed problem with field definition info (eg IS_PRI_KEY) being
        destroyed after a join.
    o Added a msql_tmpnam() function to get around the NeXT's
        problem with it's own tmpnam().

Focus on the Ecosystem

  • LAMP wasn't an accident
  • shared hosting ISPs

✔ engine improvements

  • 100%+ performance gain on most real-world applications
  • Lower memory usage, sometimes drastically lower

JIT?

Improve CPU cache usage

  • Step 1: Decrease overall data
  • Step 2: Better data locality and less indirections
  • Step 3: Save the world!
  • zval size reduced from 24 to 16 bytes
  • Hashtable size reduced from 72 to 56 bytes
  • Hashtable bucket size reduced from 72 to 32 bytes
  • Immutable array optimization
$a = [];
for($i=0; $i < 100000;$i++) {
    $a[] = ['abc','def','ghi','jkl','mno','pqr'];
}
echo memory_get_usage(true);

// PHP 5.x  109M
// PHP 7.0   42M no opcache
// PHP 7.0    6M with opcache enabled
  • New memory allocator similar to jemalloc
  • Faster hashtable iteration API
  • Array duplication optimization
  • PCRE JIT enabled by default
  • Precomputed string hashes
  • Fast ZPP (ZendParseParameters) implementation
  • Faster stack-allocated zvals (instead of heap)
  • Optimized VM calling
  • Global register variables with gcc 4.8+
  • plus hundreds of micro-optimizations

JIT?

GCC Feedback-Directed Optimization (FDO)

$ gcc --version
gcc (Debian 6.3.0-14) 6.3.0 20170415

$ make clean
$ make -j8 prof-gen
...
$ sapi/cgi/php-cgi -T 1000 /var/www/wordpress/index.php > /dev/null
$ make prof-clean
$ make -j8 prof-use

PHP 7 in production


Saving the Planet?

  • Around 2 billion sites on the web
  • On 10 million physical machines
  • PHP drives at least 50%
  • Currently ~5% PHP 7 Adoption
  • which is about 250k physical servers
  • 3000 KWH/year per server costs approx. US$400
  • Data center cooling doubles that
  • 0.5kg CO2 per KWH

At 5% Adoption

  • US $200M savings
  • 750M KWH Savings
  • 375M kg less CO2

At 100% Adoption

  • $4B savings
  • 15B KWH Savings
  • 7.5B kg less CO2

Do your part

Upgrade to PHP 7!

PHP 7.1

void return type

function should_return_nothing(): void {
    return 1; // Fatal error: A void function must not return a value
}

Nullable types

function answer(int $a, ?int $b): ?int  {
    if($a > $b) return $a;
    else return null;
}

Iterable pseudo-type

function foo(iterable $iterable) {
    foreach ($iterable as $value) {
        // ...
    }
}
function bar(): iterable {
    return [1, 2, 3];
}

Negative string offsets

$str='abcdef';
var_dump($str[-2]); // => string(1) "e"

List keys

list($first, $second, $third) = [1, 2, 3];
[$first, $second, $third] = [1, 2, 3];

Warn about invalid strings in arithmetic

$numberOfApples = "10 apples" + "5 pears";
Notice: A non well formed numeric string encountered in example.php on line 2
Notice: A non well formed numeric string encountered in example.php on line 2

Class constant visibility

class Token {
    // Constants default to public
    const PUBLIC_CONST = 0;
 
    private const PRIVATE_CONST = 0;
    protected const PROTECTED_CONST = 0;
    public const PUBLIC_CONST_TWO = 0;

Things that may break your code

  • new 'void' and 'iterable' keywords
  • rand() and srand() are now aliased to mt_rand() and mt_srand()
  • Mersenne Twister algorithm has been tweaked
  • Calling a function with too few args is now an Error instead of a Warning
  • Dropped support for sslv2 streams
  • Dropped support for mcrypt

Static Analysis



github.com/etsy/phan

It can catch dumb mistakes

$a = [1,2,3];
if(count($a > 1)) {
    echo "Test";
}
% phan test.php
test.php:2 PhanTypeComparisonFromArray array to int comparison

Check phpdoc comments

class C {
    /** @var int $prop */
    public $prop;

    /**
     * @param string $arg
     * @return int
     */
    function test($arg) {
        $this->prop = $arg;
        return $arg;
    }
}
% phan test.php
test.php:10 PhanTypeMismatchProperty Assigning string to property but \C::prop is int
test.php:11 PhanTypeMismatchReturn Returning type string but test() is declared to return int

Help with refactoring

class C {
    /**
     * @deprecated
     */
    static function legacy_function() { }
}

C::legacy_function();
% phan test.php
test.php:8 PhanDeprecatedFunction Call to deprecated function \C::legacy_function() defined at test.php:5

Install with composer

$ composer require --dev etsy/phan

Create .phan/config.php

<?php
use \Phan\Issue;
return [
    'should_visit_all_nodes' => true,
    'minimum_severity' => Issue::SEVERITY_LOW,
    'directory_list' => [ 'src', 'vendor' ],
    'exclude_analysis_directory_list' => [ 'vendor' ]
];
$ ./vendor/bin/phan

Type Safety

Legacy code

class Data {
    function __construct($data) {
        $this->haystack = $data;
    }
    function find($needle) {
        return in_array($needle, $this->haystack, true);
    }
}
$storage = new Data(['apple','orange','banana']);

$fruit = false;
$storage->find($fruit);

Going straight to strict types risks runtime fatals

<?php declare(strict_types=1);
class Data {
    function __construct(array $data) {
        $this->haystack = $data;
    }
    function find(string $needle):bool {
        return in_array($needle, $this->haystack, true);
    }
}
$storage = new Data(['apple','orange','banana']);

$fruit = false;
$storage->find($fruit);
Fatal error: Uncaught TypeError: Argument 1 passed to Data::find() must be of the type string, boolean given,
                                 called in test.php on line 13 and defined in test.php:6
Stack trace:
#0 test.php(13): Data->find(false)
#1 {main}
thrown in test.php on line 6

Intermediate step

class Data {
    /** @var array $haystack */
    public $haystack;

    /**
     * @param array $data
     */
    function __construct($data) {
        $this->haystack = $data;
    }
    /**
     * @param string $needle
     * @return bool
     */
    function find($needle) {
        return in_array($needle, $this->haystack, true);
    }
}
$storage = new Data(['apple','orange','banana']);

$fruit = false;
$storage->find($fruit);
$ phan test.php
test.php:22 PhanTypeMismatchArgument Argument 1 (needle) is bool but \Data::find() takes string defined at test.php:15

Thank You

https://github.com/rlerdorf/phan
https://bugs.php.net
http://talks.php.net/techweek



Report Bugs

Useful bug reports, please!