Commit graph

211 commits

Author SHA1 Message Date
Niels Dossche
6cc21c4ee6
Optimize pack()
Instead of using lookup tables, we can use a combination of shifts and
byte swapping to achieve the same thing in less cycles and with less
code.

Benchmark files
---------------

pack1.php:
```php
for ($i = 0; $i < 10_000_000; ++$i) {
    pack("J", 0x7FFFFFFFFFFFFFFF);
}
```

pack2.php:
```php
for ($i = 0; $i < 4000000; ++$i) {
    pack("nvc*", 0x1234, 0x5678, 65, 66);
}
```

On an i7-4790:
```
Benchmark 1: ./sapi/cli/php pack1.php
  Time (mean ± σ):     408.8 ms ±   3.4 ms    [User: 406.1 ms, System: 1.6 ms]
  Range (min … max):   403.6 ms … 413.6 ms    10 runs

Benchmark 2: ./sapi/cli/php_old pack1.php
  Time (mean ± σ):     451.7 ms ±   7.7 ms    [User: 448.5 ms, System: 2.0 ms]
  Range (min … max):   442.8 ms … 461.2 ms    10 runs

Summary
  ./sapi/cli/php pack1.php ran
    1.11 ± 0.02 times faster than ./sapi/cli/php_old pack1.php

Benchmark 1: ./sapi/cli/php pack2.php
  Time (mean ± σ):     239.3 ms ±   6.0 ms    [User: 236.2 ms, System: 2.3 ms]
  Range (min … max):   233.2 ms … 256.8 ms    12 runs

Benchmark 2: ./sapi/cli/php_old pack2.php
  Time (mean ± σ):     271.9 ms ±   3.3 ms    [User: 269.7 ms, System: 1.3 ms]
  Range (min … max):   267.4 ms … 279.0 ms    11 runs

Summary
  ./sapi/cli/php pack2.php ran
    1.14 ± 0.03 times faster than ./sapi/cli/php_old pack2.php
```

On an i7-1185G7:
```
Benchmark 1: ./sapi/cli/php pack1.php
  Time (mean ± σ):     263.7 ms ±   1.8 ms    [User: 262.6 ms, System: 0.9 ms]
  Range (min … max):   261.5 ms … 268.2 ms    11 runs

Benchmark 2: ./sapi/cli/php_old pack1.php
  Time (mean ± σ):     303.3 ms ±   6.5 ms    [User: 300.7 ms, System: 2.3 ms]
  Range (min … max):   297.4 ms … 318.1 ms    10 runs

Summary
  ./sapi/cli/php pack1.php ran
    1.15 ± 0.03 times faster than ./sapi/cli/php_old pack1.php

Benchmark 1: ./sapi/cli/php pack2.php
  Time (mean ± σ):     156.7 ms ±   2.9 ms    [User: 154.7 ms, System: 1.7 ms]
  Range (min … max):   151.6 ms … 164.7 ms    19 runs

Benchmark 2: ./sapi/cli/php_old pack2.php
  Time (mean ± σ):     174.6 ms ±   3.3 ms    [User: 171.9 ms, System: 2.3 ms]
  Range (min … max):   170.7 ms … 180.4 ms    17 runs

Summary
  ./sapi/cli/php pack2.php ran
    1.11 ± 0.03 times faster than ./sapi/cli/php_old pack2.php
```

Closes GH-18524.

Co-authored-by: divinity76 <divinity76@gmail.com>
2025-07-07 22:28:12 +02:00
David Carlier
9644ba6dcd
Merge branch 'PHP-8.4' 2025-06-29 16:59:04 +01:00
David Carlier
ef08bce645
Merge branch 'PHP-8.3' into PHP-8.4 2025-06-29 16:58:05 +01:00
David Carlier
865739e5b1
Fix GH-18976: pack with h or H format string overflow.
adding with its own remainder, INT_MAX overflows here (negative values are
discarded).

close GH-18977
2025-06-29 16:57:10 +01:00
Gina Peter Banyard
def3a95b14 [skip ci] ext/standard/pack: Fix indentation to use tabs 2025-06-12 10:47:54 +01:00
Gina Peter Banyard
a297a44d2f ext/standard/pack: Remove unused header includes 2025-06-12 10:47:54 +01:00
Gina Peter Banyard
34e22c54bc ext/standard/pack: Reduce scope of variable 2025-06-12 10:47:54 +01:00
Gina Peter Banyard
e96a7f0dfb ext/standard/pack: Remove useless casts
And use char instead of widening to int for no reason
2025-06-12 10:47:54 +01:00
Gina Peter Banyard
2a77e282f8 ext/standard/pack: Inline constant single use variables
They serve no purpose and are just confusing
2025-06-12 10:47:54 +01:00
Niels Dossche
559858c822
Improve performance of unpack() with nameless repetitions (#18803)
We can avoid creating temporary strings, and then reparsing them into
numbers with zend_symtable_update() by using zend_hash_index_update()
directly.

For the following benchmark on an i7-4790:
```php
$file = str_repeat('A', 100000);
for ($i=0;$i<100;$i++) unpack('C*',$file);
```

I get:
```
Benchmark 1: ./sapi/cli/php y.php
  Time (mean ± σ):      85.8 ms ±   1.8 ms    [User: 74.5 ms, System: 10.4 ms]
  Range (min … max):    83.8 ms …  92.4 ms    33 runs

Benchmark 2: ./sapi/cli/php_old y.php
  Time (mean ± σ):     318.3 ms ±   2.7 ms    [User: 306.7 ms, System: 9.9 ms]
  Range (min … max):   314.9 ms … 321.6 ms    10 runs

Summary
  ./sapi/cli/php y.php ran
    3.71 ± 0.08 times faster than ./sapi/cli/php_old y.php
```

On an i7-1185G7 I get:
```
Benchmark 1: ./sapi/cli/php test.php
  Time (mean ± σ):      60.1 ms ±   0.7 ms    [User: 47.8 ms, System: 12.0 ms]
  Range (min … max):    59.2 ms …  63.8 ms    48 runs

  Warning: Statistical outliers were detected. Consider re-running this benchmark on a quiet system without any interferences from other programs. It might help to use the '--warmup' or '--prepare' options.

Benchmark 2: ./sapi/cli/php_old test.php
  Time (mean ± σ):     220.8 ms ±   2.2 ms    [User: 209.6 ms, System: 10.7 ms]
  Range (min … max):   218.5 ms … 224.5 ms    13 runs

Summary
  ./sapi/cli/php test.php  ran
    3.67 ± 0.06 times faster than ./sapi/cli/php_old test.php
```
2025-06-10 22:33:33 +02:00
David Carlier
ea0937b550
Merge branch 'PHP-8.3' 2024-09-11 21:16:26 +01:00
David Carlier
b975b6c9ab
Merge branch 'PHP-8.2' into PHP-8.3 2024-09-11 21:15:42 +01:00
David Carlier
791a6ef19c
Fix GH-15613: unpack on format hex strings repeater value.
close GH-15615
2024-09-11 21:14:27 +01:00
Gina Peter Banyard
25a5146180
Clean-up unused headers (#14365)
* ext/mbstring.c: clean-up headers and include intrinsics
2024-06-01 17:12:42 +01:00
Niels Dossche
f8ecb80e84 Merge branch 'PHP-8.2'
* PHP-8.2:
  Fix undefined behaviour in unpack()
2023-03-28 22:48:21 +02:00
Niels Dossche
e9195b21cc Merge branch 'PHP-8.1' into PHP-8.2
* PHP-8.1:
  Fix undefined behaviour in unpack()
2023-03-28 22:43:53 +02:00
Niels Dossche
87862835e2 Fix undefined behaviour in unpack()
atoi()'s return value is actually undefined when an underflow or
overflow occurs. For example on 32-bit on my system the overflow test
which inputs "h2147483648" results in repetitions==2147483647 and on
64-bit this gives repetitions==-2147483648. The reason the test works on
32-bit is because there's a second undefined behaviour problem:
in case 'h' when repetitions==2147483647, we add 1 and divide by 2.
This is signed-wrap undefined behaviour and accidentally triggers the
overflow check like we wanted to.

Avoid all this trouble and use strtol with explicit error checking.

This also fixes a semantic bug where repetitions==INT_MAX would result
in the overflow check to trigger, even though there is no overflow.

Closes GH-10943.
2023-03-28 22:43:27 +02:00
Niels Dossche
6ec69d727a
Improve the warning message for unpack() in case not enough values were provided (#10949) 2023-03-27 00:04:42 +02:00
David Carlier
db64c1cb70 zend introduce const GNUC attribute. sub optimisation where there is no pointers, nor particular memory layout, thread local/volatile ... involved. usage concealed for now into little pack helpers.
Closes #9326.
2022-08-15 19:49:24 +01:00
George Peter Banyard
5171cb435a Fix [-Wundef] warnings in standard extension 2022-04-01 15:48:41 +01:00
Remi Collet
e31c54d025
fix GH-7899 Regression in unpack for negative int value 2022-01-13 11:47:19 +01:00
Remi Collet
524ce90418
fix GH-7899 Regression in unpack for negative int value 2022-01-13 11:44:34 +01:00
Patrick Allaert
aff365871a Fixed some spaces used instead of tabs 2021-06-29 11:30:26 +02:00
K
21422e8536
Optimize unpack() for named fields (#6958)
Create name using either zend_init_string_fast (no repetitions) or by concatenating the name with zend_print_ulong_to_buf. This is much more efficient than using snprintf. We also avoid repeated strlen() calculations.
2021-05-14 11:32:46 +02:00
K
efe79e0de6
Simplify unpack logic (#6908)
- move endiannes check to compile time
- remove php_unpack function
- the compiler take care of sign extension
2021-05-06 15:20:05 +02:00
KsaR
01b3fc03c3
Update http->https in license (#6945)
1. Update: http://www.php.net/license/3_01.txt to https, as there is anyway server header "Location:" to https.
2. Update few license 3.0 to 3.01 as 3.0 states "php 5.1.1, 4.1.1, and earlier".
3. In some license comments is "at through the world-wide-web" while most is without "at", so deleted.
4. fixed indentation in some files before |
2021-05-06 12:16:35 +02:00
Nikita Popov
422d1665a2 Make convert_to_*_ex simple aliases of convert_to_*
Historically, the _ex variants separated the zval first, if a
conversion was necessary. This distinction no longer makes sense
since PHP 7.

The only difference that was still left is that _ex checked whether
the type is the same first, but the usage of these macros did not
actually distinguish on whether such an inlined check is valuable
or not in a given context.

Also drop the unused convert_to_explicit_type macros.
2021-01-14 12:11:11 +01:00
Nikita Popov
c2a2d2438a Fix double free on unpack error 2020-09-22 23:31:56 +02:00
Nikita Popov
de77344de2 Promote pack/unpack format errors
Errors related to invalid format strings (unlike data mismatch
errors) should throw ValueError.

Closes GH-6185.
2020-09-22 15:12:05 +02:00
Nikita Popov
048cc9ba78 Convert unpack offset warning to ValueError 2020-09-22 14:13:26 +02:00
Max Semenik
2b5de6f839
Remove proto comments from C files
Closes GH-5758
2020-07-06 21:13:34 +02:00
Máté Kocsis
7b4a4d2ace
Use RETURN_THROWS() after try_convert_to_string() 2020-01-03 17:04:06 +01:00
Christoph M. Becker
0dba3a8e43 Merge branch 'PHP-7.4'
* PHP-7.4:
  Fix #78833: Integer overflow in pack causes out-of-bound access
2019-12-02 11:21:37 +01:00
Christoph M. Becker
4ff242a9a7 Merge branch 'PHP-7.3' into PHP-7.4
* PHP-7.3:
  Fix #78833: Integer overflow in pack causes out-of-bound access
2019-12-02 11:21:07 +01:00
Christoph M. Becker
3d81c54879 Merge branch 'PHP-7.2' into PHP-7.3
* PHP-7.2:
  Fix #78833: Integer overflow in pack causes out-of-bound access
2019-12-02 11:19:15 +01:00
Christoph M. Becker
db420cb6a1 Fix #78833: Integer overflow in pack causes out-of-bound access
We check for potential signed integer overflow, and bail out
gracefully, in that case.
2019-12-02 11:18:19 +01:00
Gabriel Caruso
5d6e923d46
Remove mention of PHP major version in Copyright headers
Closes GH-4732.
2019-09-25 14:51:43 +02:00
Nikita Popov
7686b0b889 Merge branch 'PHP-7.4' 2019-06-05 14:53:50 +02:00
Nikita Popov
a31f46421d Allow exceptions in __toString()
RFC: https://wiki.php.net/rfc/tostring_exceptions

And convert some object to string conversion related recoverable
fatal errors into Error exceptions.

Improve exception safety of internal code performing string
conversions.
2019-06-05 14:25:07 +02:00
Nikita Popov
001d434449 Finish func_info updates for ext/standard 2019-06-03 11:26:25 +02:00
Peter Kokot
92ac598aab Remove local variables
This patch removes the so called local variables defined per
file basis for certain editors to properly show tab width, and
similar settings. These are mainly used by Vim and Emacs editors
yet with recent changes the once working definitions don't work
anymore in Vim without custom plugins or additional configuration.
Neither are these settings synced across the PHP code base.

A simpler and better approach is EditorConfig and fixing code
using some code style fixing tools in the future instead.

This patch also removes the so called modelines for Vim. Modelines
allow Vim editor specifically to set some editor configuration such as
syntax highlighting, indentation style and tab width to be set in the
first line or the last 5 lines per file basis. Since the php test
files have syntax highlighting already set in most editors properly and
EditorConfig takes care of the indentation settings, this patch removes
these as well for the Vim 6.0 and newer versions.

With the removal of local variables for certain editors such as
Emacs and Vim, the footer is also probably not needed anymore when
creating extensions using ext_skel.php script.

Additionally, Vim modelines for setting php syntax and some editor
settings has been removed from some *.phpt files.  All these are
mostly not relevant for phpt files neither work properly in the
middle of the file.
2019-02-03 21:03:00 +01:00
Zeev Suraski
0cf7de1c70 Remove yearly range from copyright notice 2019-01-30 11:03:12 +02:00
Peter Kokot
902d39a3a7 Trim trailing whitespace in source code files 2018-10-13 14:14:50 +02:00
Peter Kokot
8d3f8ca12a Remove unused Git attributes ident
The $Id$ keywords were used in Subversion where they can be substituted
with filename, last revision number change, last changed date, and last
user who changed it.

In Git this functionality is different and can be done with Git attribute
ident. These need to be defined manually for each file in the
.gitattributes file and are afterwards replaced with 40-character
hexadecimal blob object name which is based only on the particular file
contents.

This patch simplifies handling of $Id$ keywords by removing them since
they are not used anymore.
2018-07-25 00:53:25 +02:00
Dmitry Stogov
4a475a4976 Replace legacy zval_dtor() by zval_ptr_dtor_nogc() or even more specialized destructors.
zval_dtor() doesn't make a lot of sense in PHP-7.* and it's used incorrectly in some places.
Its occurances should be replaced by zval_ptr_dtor() or zval_ptr_dtor_nogc(), or even more specialized destructors.
2018-07-04 19:22:24 +03:00
Gabriel Caruso
6400264856 Trailing whitespaces
Signed-off-by: Gabriel Caruso <carusogabriel34@gmail.com>
2018-01-03 14:38:00 +01:00
Xinchen Hui
a6519d0514 year++ 2018-01-02 12:57:58 +08:00
Xinchen Hui
7a7ec01a49 year++ 2018-01-02 12:55:14 +08:00
Dmitry Stogov
ccc12b82da Avoid unnecessary reference-counting on strings. 2017-11-16 17:09:32 +03:00
Xinchen Hui
05de40a651 Merge branch 'PHP-7.1' into PHP-7.2
* PHP-7.1:
  Update NEWS
  Fixed bug #75075 (unpack with X* causes infinity loop)
2017-08-15 12:35:10 +08:00