# RFC: Fast Parameter Parsing API

- Status: Implemented (PHP 7.0)
- Authors: Dmitry Stogov dmitry@zend.com, Bob Weinand bobwei9@hotmail.com
- Date: 2014-05-23
- Version: 0.9
- Original: https://wiki.php.net/rfc/fast_zpp
- Page: https://externals.io/rfc/fast_zpp
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

## Votes

### Should PHP 7 have Fast Parameter Parsing API? (closed)

Yes: 19 · No: 1

Voters: ajf (Yes), bukka (Yes), bwoebi (Yes), dmitry (Yes), guilhermeblanco (No), gwynne (Yes), jedibc (Yes), jgmdev (Yes), jpauli (Yes), kalle (Yes), krakjoe (Yes), laruence (Yes), leigh (Yes), levim (Yes), lstrojny (Yes), mj (Yes), patrickallaert (Yes), rasmus (Yes), rdlowrey (Yes), treffynnon (Yes)

## Discussions on the mailing list

- [\[RFC\] Fast Parameter Parsing API](https://externals.io/message/74444.md) — started on 2014-05-23, 52 messages
- [\[RFC\] \[VOTE\] Fast Parameter Parsing API](https://externals.io/message/80819.md) — started on 2015-01-19, 3 messages
- [Improvements to array.c code base](https://externals.io/message/80865.md) — started on 2015-01-20, 1 messages
- [Move to Fast ZPP?](https://externals.io/message/86826.md) — started on 2015-06-24, 23 messages
- [The death of `#ifndef FAST_ZPP`?](https://externals.io/message/93072.md) — started on 2016-05-04, 18 messages

---

## Introduction

PHP internal functions use zend\_parse\_parameters() API to receive values of actual parameters into C variables. This function uses scanf() like approach for parameter definition. The number and types of required data defined by a string that contains a list of specifiers (“s” - for string, “l” for long, etc). Unfortunately, this string has to be parsed each time when php calls this function, and this makes significant performance overhead.

For example zend\_parse\_parameters() takes ~6% of CPU time in serving wordpress home page.

For some really simple function like is\_string() or ord() the overhead of zend\_parse\_parameters() may be about 90%.

## Proposal

We propose an additional fast API for parameter parsing, that should be used for the most useful functions. The API is based on C macros that lead to inlining of optimized code directly into the body of internal function.

**We don't propose to remove the existing API, and would suggest to use fast API only for most often used functions to get performance boost.**

I'll explain API on the following example:

```
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "al|zb", &input, &offset, &z_length, &preserve_keys) == FAILURE) {
	return;
}
```

```
ZEND_PARSE_PARAMETERS_START(2, 4)
	Z_PARAM_ARRAY(input)
	Z_PARAM_LONG(offset)
	Z_PARAM_OPTIONAL
	Z_PARAM_ZVAL(z_length)
	Z_PARAM_BOOL(preserve_keys)
ZEND_PARSE_PARAMETERS_END();
```

The first code fragment is just taken from PHP\_FUNCTION(array\_slice), the second is its replacement using new API. The code is actually self explainable.

ZEND\_PARSE\_PARAMETERS\_START() takes two arguments minimal and maximal parameters count.

Z\_PARAM\_ARRAY() - takes the next argument as array, Z\_PARAM\_LONG - as long, Z\_PARAM\_OPTIONAL - tells that the remaining arguments are optional.

The new API covers all possibilities of the existing API. The following table shows the correspondence between old specifiers and new macros.

| specifier | Fast ZPP API macro | args |
|---|---|---|
| `\|` | Z\_PARAM\_OPTIONAL |
| a | Z\_PARAM\_ARRAY(dest) | dest - zval\* |
| A | Z\_PARAM\_ARRAY\_OR\_OBJECT(dest) | dest - zval\* |
| b | Z\_PARAM\_BOOL(dest) | dest - zend\_bool |
| C | Z\_PARAM\_CLASS(dest) | dest - zend\_class\_entry\* |
| d | Z\_PARAM\_DOUBLE(dest) | dest - double |
| f | Z\_PARAM\_FUNC(fci, fcc) | fci - zend\_fcall\_info, fcc - zend\_fcall\_info\_cache |
| h | Z\_PARAM\_ARRAY\_HT(dest) | dest - HashTable\* |
| H | Z\_PARAM\_ARRAY\_OR\_OBJECT\_HT(dest) | dest - HashTable\* |
| l | Z\_PARAM\_LONG(dest) | dest - long |
| L | Z\_PARAM\_STRICT\_LONG(dest) | dest - long |
| o | Z\_PARAM\_OBJECT(dest) | dest - zval\* |
| O | Z\_PARAM\_OBJECT\_OF\_CLASS(dest, ce) | dest - zval\* |
| p | Z\_PARAM\_PATH(dest, dest\_len) | dest - char\*, dest\_len - int |
| P | Z\_PARAM\_PATH\_STR(dest) | dest - zend\_string\* |
| r | Z\_PARAM\_RESOURCE(dest) | dest - zval\* |
| s | Z\_PARAM\_STRING(dest, dest\_len) | dest - char\*, dest\_len - int |
| S | Z\_PARAM\_STR(dest) | dest - zend\_string\* |
| z | Z\_PARAM\_ZVAL(dest) | dest - zval\* |
|  | Z\_PARAM\_ZVAL\_DEREF(dest) | dest - zval\* |
| + | Z\_PARAM\_VARIADIC('+', dest, num) | dest - zval\*, num int |
| \* | Z\_PARAM\_VARIADIC('\*', dest, num) | dest - zval\*, num int |

The effect of **!** and reference modifiers may be achieved using extended version of the same macros e.g. Z\_PARAM\_ZVAL\_EX(dest, check\_null, separate).

### Performance Evaluation

The following numbers collected for 100 and 1000 requests to wordpress home page.

|  | # of CPU instructions | time \[sec\] |
|---|---|---|
| phpng | 5,132,181,672 | 18.90 |
| phpng + fast\_zpp | 4,901,907,204 | 18.40 |

So the proposal provides 4.5% improvement in terms of CPU instructions and 2.5% in terms of time.

The effect of code size increase is negligible. Just ~20KB for ~70 function (that's below 0.1%)

## Backward Incompatible Changes

The new API provides exactly the same behavior. Also, we don't propose to remove the old API. So there must not be any compatibility problems.

## Proposed PHP Version(s)

This proposal is targeted to php-7

## Vote

State whether this project requires 50%+1 majority (see [voting](https://externals.io/rfc/voting)). The vote is a straight Yes/No vote.

**Vote: Should PHP 7 have Fast Parameter Parsing API?** — Yes: 19 · No: 1 (closed)

The vote concluded on January 27th.

## Patches and Tests

- Original Proposal: <https://gist.github.com/dstogov/1131691470a4c37540a6>

The patch is already included into PHP-7 and wrapped with ifdef/ifndef FAST\_ZPP.
