1
0
mirror of https://github.com/php/php-src.git synced 2026-03-24 00:02:20 +01:00

328 Commits

Author SHA1 Message Date
Gina Peter Banyard
38cbbee4aa Zend: add const qualifiers to the majority of compare functions (#21304)
This sadly does not include zend_compare() and functions relying on it because the default compare object hook (zend_std_compare_objects()) relies on zend_compare(), and adds recursion protections to OP1.
Thus making it seems difficult or even impossible for zend_compare to take const parameters.
2026-03-19 20:36:19 +00:00
Niels Dossche
b69025807b standard: Avoid double hash table lookup in iptcparse()
Closes GH-20420.
2025-11-08 13:33:12 +01:00
Niels Dossche
7b6b233ce8 Optimize array_fill_keys() (#20347)
Move the refcount update outside of the loop.

For the following benchmark:
```php
$r = range(0, 1000);
$v = new stdClass();

for ($i = 0; $i < 100000; $i++) {
    array_fill_keys($r, $v);
}
```

On an i7-4790:
```
Benchmark 1: ./sapi/cli/php_old ../x.php
  Time (mean ± σ):     507.5 ms ±   4.8 ms    [User: 505.1 ms, System: 1.2 ms]
  Range (min … max):   501.2 ms … 518.4 ms    10 runs

Benchmark 2: ./sapi/cli/php ../x.php
  Time (mean ± σ):     479.8 ms ±   3.1 ms    [User: 476.8 ms, System: 1.8 ms]
  Range (min … max):   475.0 ms … 486.7 ms    10 runs

Summary
  ./sapi/cli/php ../x.php ran
    1.06 ± 0.01 times faster than ./sapi/cli/php_old ../x.php
```

On an i7-1185G7:
```
Benchmark 1: ./sapi/cli/php x.php
  Time (mean ± σ):     343.9 ms ±   3.1 ms    [User: 341.1 ms, System: 2.3 ms]
  Range (min … max):   337.9 ms … 347.8 ms    10 runs

Benchmark 2: ./sapi/cli/php_old x.php
  Time (mean ± σ):     357.8 ms ±   2.3 ms    [User: 355.7 ms, System: 1.6 ms]
  Range (min … max):   355.0 ms … 362.6 ms    10 runs

Summary
  ./sapi/cli/php x.php ran
    1.04 ± 0.01 times faster than ./sapi/cli/php_old x.php
```
2025-11-03 22:35:26 +01:00
Tim Düsterhus
28fc06be49 zend_hash: Assert that the interned parameter is not a lie (#19843)
* zend_hash: Assert that the `interned` parameter is not a lie

While investigating php/php-src#19842 I was wondering why non-interned string
didn't cause troubles, until I realized it was the value instead of the key.
Nevertheless it appears useful to check that the key is actually interned as
claimed by the caller to prevent hard-to-find bugs.

* zend_hash: Rename `interned` parameter name to `key_guaranteed_interned`
2025-09-16 10:51:07 +02:00
Alexandre Daubois
bf64dfcd99 Add an enum for HASH_KEY_IS_* constants (GH-19376) 2025-08-13 13:59:01 +02:00
Gina Peter Banyard
6329248217 Zend/hash: Add yet more const qualifiers (#18084) 2025-03-18 17:36:40 +00:00
Ilija Tovilo
4b517a969a [skip ci] Document HASH_ constants (#16806)
Not all of these are straight-forward to understand.
2024-11-18 19:28:15 +01:00
Ilija Tovilo
dbcdd4e4a7 Merge branch 'PHP-8.4'
* PHP-8.4:
  Postpone zend_array_sort() in ext-intl
  Use bool for zend_array_sort()
2024-11-04 16:02:47 +01:00
Ilija Tovilo
085c151481 Merge branch 'PHP-8.3' into PHP-8.4
* PHP-8.3:
  Postpone zend_array_sort() in ext-intl
  Use bool for zend_array_sort()
2024-11-04 16:02:31 +01:00
Ilija Tovilo
745684290e Merge branch 'PHP-8.2' into PHP-8.3
* PHP-8.2:
  Postpone zend_array_sort() in ext-intl
  Use bool for zend_array_sort()
2024-11-04 16:02:16 +01:00
Ilija Tovilo
4b9e59b430 [skip ci] Use bool for zend_array_sort()
zend_bool is removed in higher branches.
2024-11-04 15:59:18 +01:00
Ilija Tovilo
ecb87ca5f1 Mark new zend_array_sort_ex() as ZEND_API 2024-11-04 15:53:51 +01:00
Ilija Tovilo
d82fa6adf6 Merge branch 'PHP-8.4'
* PHP-8.4:
  Fix array going away during sorting
2024-11-04 15:53:03 +01:00
Ilija Tovilo
230defc198 Merge branch 'PHP-8.3' into PHP-8.4
* PHP-8.3:
  Fix array going away during sorting
2024-11-04 15:51:24 +01:00
Ilija Tovilo
f033cf75e4 Merge branch 'PHP-8.2' into PHP-8.3
* PHP-8.2:
  Fix array going away during sorting
2024-11-04 15:51:03 +01:00
Ilija Tovilo
2bdce61390 Fix array going away during sorting
Fixes GH-16648
Closes GH-16654
2024-11-04 15:50:35 +01:00
Dmitry Stogov
95dbc33fb2 Merge branch 'PHP-8.4'
* PHP-8.4:
  Making zend_hash_str_find_ptr_lc && zend_hash_find_ptr_lc C++ friendly (#16476)
2024-10-17 15:18:44 +03:00
Kasey Jenkins
cf3ecfff25 Making zend_hash_str_find_ptr_lc && zend_hash_find_ptr_lc C++ friendly (#16476) 2024-10-17 15:18:14 +03:00
Gina Peter Banyard
531b94359e Zend/zend_hash.h: Mark zend_array* parameter of zend_array_is_list() as const 2024-09-25 23:30:20 +01:00
Gina Peter Banyard
2c3c2381e1 Zend/zend_hash.(c|h): Mark various parameters and variables const (#16003) 2024-09-25 16:05:28 +01:00
Niels Dossche
c3a5b98771 Add zend_hash_get_current_pos_ex() (#14770) 2024-07-02 19:11:41 +02:00
David CARLIER
bc585cd87a Fix GH-14643 ext/standard: segfault on user shutdown function release. (#14656) 2024-06-25 21:13:15 +01:00
Tim Düsterhus
7c2a4dbd72 Replace ZEND_ASSUME() by ZEND_ASSERT() in zend_hash_*_ptr setters (#14466)
I had a case where I was intentionally storing a `NULL` pointer within a
HashTable to mark an entry as “blocked”, without paying for the overhead of
checking the entry type when reading the pointer, resulting in semantics that
are similar to using `isset()` instead of `array_key_exists()` in userland.

This worked fine in unoptimized test builds, but due to the `ZEND_ASSUME()` in
the `zend_hash_find_ptr` functions, the optimized release builds turned the
logic of:

    my_pointer = zend_hash_find_ptr(ht, key);
    if (my_pointer == NULL) {
        return;
    }
    *my_pointer;

into

    zv = zend_hash_find(ht, key);
    if (zv) {
        *Z_PTR_P(zv);
    } else {
        return;
    }

thus introducing a hard-to-debug and compiler-dependent crash when the entry
exists, but the stored pointer is `NULL`.

Change the `ZEND_ASSUME()` in the setters to `ZEND_ASSERT()`. This would have
made my mistake immediately obvious in debug builds when storing the pointer.
The getters still use `ZEND_ASSUME()` under the assumption that they are called
much more often, reducing the impact on debug builds: Assuming the developer
uses the `_ptr` variants for both reading and writing the entries, the mistake
will be reliably caught during writing, making the assert during reading
unnecessary.

For release builds the `ZEND_ASSERT()` will be equivalent to `ZEND_ASSUME()`,
avoiding any performance impact for those.
2024-06-05 11:08:11 +02:00
George Peter Banyard
76a773b8dd Add some const qualifiers in HashTable foreach macros (#8671) 2023-10-09 17:20:09 +01:00
George Peter Banyard
5c3a6eaec9 Zend: Remove dependency on zend.h for certain headers (#12166) 2023-09-11 12:27:21 +01:00
Niels Dossche
2ef1930ad3 Fix number of elements after packed hash filling (#11022)
After a hash filling routine the number of elements are set to the fill
index. However, if the fill index is larger than the number of elements,
the number of elements are no longer correct. This is observable at
least via count() and var_dump(). E.g. the attached test case would
incorrectly show int(17) instead of int(11).

Solve this by only increasing the number of elements by the actual
number that got added. Instead of adding a variable that increments per
iteration, I wanted to save some cycles in the iteration and simply
compute the number of added elements at the end.

I discovered this behaviour while fixing GH-11016, where this filling
routine is easily exposed to userland via a specialised VM path [1].
Since this seems to be more a general problem with the macros, and may
be triggered outside of the VM handlers, I fixed it in the macros
instead of modifying the VM to fixup the number of elements.

[1] b2c5acbb01/Zend/zend_vm_def.h (L6132-L6141)
2023-04-06 21:54:59 +02:00
Dmitry Stogov
0e70693701 Revert "Zend/zend_types.h: move IS_* to zend_type_code.h"
This reverts commit 0270a1e54c.
2023-04-04 22:48:26 +03:00
Max Kellermann
0270a1e54c Zend/zend_types.h: move IS_* to zend_type_code.h
More decoupling of circular header dependencies.
2023-02-26 14:16:53 +00:00
Max Kellermann
413844d626 Zend/zend_types.h: deprecate zend_bool, zend_intptr_t, zend_uintptr_t (#10597)
These types are standard C99.

For compatibility with out-of-tree extensions, keep the typedefs
in main/php.h.
2023-02-18 19:31:28 +00:00
Christoph M. Becker
2d3427c507 Revert "#include cleanup (#10216)"
Cf. <https://github.com/php/php-src/pull/10220#issuecomment-1383739816>.

This reverts commit e628c66f9d.
2023-01-16 12:29:41 +01:00
Max Kellermann
e628c66f9d #include cleanup (#10216)
Shift header include

In the C file, include the header first so missing #includes are
detected by the compiler, and use lighter header dependencies in the
header, to speed up compile times.
2023-01-04 13:24:28 +00:00
Jorg Adam Sowa
db012a8ba7 Use function HT_IS_PACKED where it's missing (#9658) 2022-10-04 13:37:06 +01:00
George Peter Banyard
faa83f2f34 Convert some macros to zend_always_inline functions (#8288)
This doesn't have an effect really, but humans and IDEs can struggle to see through the macro soup when they first interact with PHP's source code.

Moreover, this reduces some of the macro expansion hell when they appear in compiler warnings.
2022-08-04 13:24:12 +01:00
George Peter Banyard
bdf5a4e478 The hashvalue/index of a bucket is a zend_ulong 2022-07-26 11:54:33 +01:00
Tim Düsterhus
75a9a5f311 Add zend_array_to_list() (#8976)
* Add zend_array_to_list()

* Use `zend_array_to_list()` in `PHP_FUNCTION(array_values)`
2022-07-11 15:29:40 +02:00
George Peter Banyard
c2547ab7dc Add some const qualifiers in zend_string/hash (#8304)
Co-authored-by: Levi Morrison <morrison.levi@gmail.com>
2022-04-20 15:56:51 +01:00
Bob Weinand
a8b9dbc632 Fix ZEND_HASH_REVERSE_FOREACH_PTR definition 2021-11-18 00:10:28 +01:00
Dmitry Stogov
90b7bde615 Use more compact representation for packed arrays.
- for packed arrays we store just an array of zvals without keys.
- the elements of packed array are accessible throuf as ht->arPacked[i]
  instead of ht->arData[i]
- in addition to general ZEND_HASH_FOREACH_* macros, we introduced similar
  familied for packed (ZEND_HASH_PACKED_FORECH_*) and real hashes
  (ZEND_HASH_MAP_FOREACH_*)
- introduced an additional family of macros to access elements of array
  (packed or real hashes) ZEND_ARRAY_ELEMET_SIZE, ZEND_ARRAY_ELEMET_EX,
  ZEND_ARRAY_ELEMET, ZEND_ARRAY_NEXT_ELEMENT, ZEND_ARRAY_PREV_ELEMENT
- zend_hash_minmax() prototype was changed to compare only values

Because of smaller data set, this patch may show performance improvement
on some apps and benchmarks that use packed arrays. (~1% on PHP-Parser)

TODO:
    - sapi/phpdbg needs special support for packed arrays (WATCH_ON_BUCKET).
    - zend_hash_sort_ex() may require converting packed arrays to hash.
2021-11-03 15:18:26 +03:00
Nikita Popov
8356da600b Remove dynamic defs from methods as well
We need to remove DECLARE_FUNCTION + dynamic_defs for functions
defined in methods as well, not just for those declared in the
main script.
2021-08-02 10:29:58 +02:00
Nikita Popov
67b5d8fcdc Don't reverse class order during preloading
We don't guarantee any particular order, but this reduces test
failures under --preload that are sensitive to class order.

Add some ZEND_HASH_FOREACH_*_FROM macros to allow skipping the
persistent classes while iterating in forward direction.
2021-07-30 15:14:17 +02:00
Levi Morrison
ae8647d9d3 Remove leading underscore for _zend_hash_find_known_hash (#7260)
Convert zend_hash_find_ex(..., 1) to zend_hash_find_known_hash(...)
Convert zend_hash_find_ex(..., 0) to zend_hash_find(...)

Also add serializable changes to UPGRADING.INTERNALS summary
2021-07-20 17:07:17 -06:00
Patrick Allaert
ceb6fa6dc0 Convert some recently introduced zend_bool to bool
As well as `scripts/dev/check_parameters.php` utility.

Cfr. 3e01f5afb1
2021-06-18 15:21:39 +01:00
Dmitry Stogov
e86bea8d45 Extend ZEND_HASH_FILL_* API with ZEND_HASH_FILL_GROW and use it to optimize get_declared_classes() 2021-04-06 16:18:47 +03:00
Dmitry Stogov
340013ad01 Add zend_hash_lookup() and zend_hash_index_lookup() functions.
Thet search for an element with given key/index and add an empty one (NULL), if no found.
2021-03-19 22:36:24 +03:00
Dusk
13c430b1db Add array_is_list(array $array) function
This function tests if an array contains only sequential integer keys. While
list isn't an official type, this usage is consistent with the community usage
of "list" as an annotation type, cf.
https://psalm.dev/docs/annotating_code/type_syntax/array_types/#lists

Rebased and modified version of #4886

- Use .stub.php files
- Add opcache constant evaluation when argument is a constant
- Change from is_list(mixed $value) to array_is_list(array $array)

RFC: https://wiki.php.net/rfc/is_list

Co-Authored-By: Tyson Andre <tysonandre775@hotmail.com>
Co-Authored-By: Dusk <dusk@woofle.net>

Closes GH-6070
2021-01-20 18:53:48 -05:00
Nikita Popov
3e01f5afb1 Replace zend_bool uses with bool
We're starting to see a mix between uses of zend_bool and bool.
Replace all usages with the standard bool type everywhere.

Of course, zend_bool is retained as an alias.
2021-01-15 12:33:06 +01:00
Remi Collet
8710429283 fix zend_hash_get_current_key_type_ex proto 2020-09-10 12:40:47 +02:00
Remi Collet
7be70c6bfc fix zend_hash_get_current_key_ex proto 2020-09-10 12:36:44 +02:00
twosee
40e5238a00 Flesh out ZEND_HASH_REVERSE_FOREACH_* macros
Closes GH-6080.
2020-09-09 15:12:18 +08:00
George Peter Banyard
fa8d9b1183 Improve type declarations for Zend APIs
Voidification of Zend API which always succeeded
Use bool argument types instead of int for boolean arguments
Use bool return type for functions which return true/false (1/0)
Use zend_result return type for functions which return SUCCESS/FAILURE as they don't follow normal boolean semantics

Closes GH-6002
2020-08-28 15:41:27 +02:00