Commit Graph

331 Commits

Author SHA1 Message Date
Dmitry Stogov
b250f46703 Optimized HashTable copy and cleanup function for cases without holes. 2015-04-24 22:43:50 +03:00
Dmitry Stogov
ef36d8a91e Optimized zend_hash_rehash(), added some exoectations to generate better code 2015-04-24 13:00:56 +03:00
Dmitry Stogov
6265687768 Added HashTable flag HASH_FLAG_STATIC_KEYS that is maintaned to be set if all hash table keys are numbers or interned strings.
Take this flag into account when copy or destroy HashTable to avoud useless checks for each key.
2015-04-24 09:34:50 +03:00
Dmitry Stogov
770cb1da71 Keep realpath and PCRE caches in consistency with opcache SHM. 2015-04-22 02:29:06 +03:00
Dmitry Stogov
c9da004a18 Slight zend_hash_find() optimization. 2015-04-21 23:27:44 +03:00
matason
52145ef8c6 Fix zend_hash_find documentation. 2015-04-07 22:26:16 +01:00
Dmitry Stogov
55cdde3dd6 Fixed bug #69376 (Wrong ref counting). 2015-04-07 02:35:52 +03:00
Xinchen Hui
ceb77b6472 Revert "Better to taverse reversely"
This reverts commit 51451ab84a.
2015-03-31 16:52:30 +08:00
Xinchen Hui
51451ab84a Better to taverse reversely 2015-03-30 15:26:59 +08:00
K
b3962ab940 zend_hash_do_resize: amortizing the cost of compaction
New implementation of hashtables introduced a compaction step which is
triggered when a hashtable is full but it contains at least one deleted
bucket. Therefore there is a possibility that a cleverly crafted code can
trigger this compaction step (which takes time proportional to the size of
hashtabe) by executing constatnt number of operations. When the hashtable
is full, deletion and subsequent addition or single element triggers a
table compaction and these two steps can be repeated ad infinitum. This
might be avenue for a DOS attack.

This patch allows compaction to be performed only if the hashtable contains
at least 1/32 deleted elements, otherwise the hashtable is doubled in size.
Linear amount of work caused by compaction is amortized over multiple
malicious additions and deletions.
2015-03-26 19:05:38 +01:00
Dmitry Stogov
87cec4ccc9 Fixed mess in names 2015-03-18 00:13:39 +03:00
Dmitry Stogov
55da1fb008 Fixed zend_hash_del() 2015-03-17 23:13:39 +03:00
Dmitry Stogov
db10b72523 Use fastcall calling convention for most critical ZE subsystems. 2015-03-13 21:10:09 +03:00
Dmitry Stogov
2b42d71908 Changed HashTable layout:
Removed HashTable->arHash (reduced memory consumption). Now hash slots may be accessed using HT_HASH() macro.
Hash slotas are allocated together with Buckets (before them) and lay in reverse order from HashTable->arData base address (see comments in Zend/zend_types.h)
Indexes in hash table and conflict resolution chains (Z_NEXT) may be stored as indeces or offsets in bytes, depending on system (32 or 64-bit).
HashTable data filelds are reordered to keep the most useful for zend_hash_find() data in the same CPU cache line.
2015-03-13 17:13:19 +03:00
Xinchen Hui
1ebdbdfd78 Unused var 2015-03-05 23:09:58 +08:00
Dmitry Stogov
bb91bf83f9 Minimize copying of HashTable realocation 2015-03-05 17:22:10 +03:00
Dmitry Stogov
8ec8648c91 Improved code for class property inheritance 2015-03-04 17:41:01 +03:00
Dmitry Stogov
a64e12e2a1 pefree() -> efree() 2015-03-03 09:09:28 +03:00
Dmitry Stogov
0ef5a6b409 Avoid unnecassary check 2015-02-24 19:21:21 +03:00
Dmitry Stogov
e97ae4fee8 Make zend_array_destroy() to free the corresponding zend_array 2015-02-24 18:34:37 +03:00
Dmitry Stogov
e10e151e9b Merged zend_array and HashTable into the single data structure.
Now each HashTable is also zend_array, so it's refcounted and may be a subject for Copy on Write
zend_array_dup() was changed to allocate and return HashTable, instead of taking preallocated HashTable as argument.
2015-02-13 22:20:39 +03:00
Xinchen Hui
2766dbaf6d Unused vars 2015-02-13 10:53:40 +08:00
Dmitry Stogov
12abac8bb7 Limit HashTable size to avoid integer overflow checks 2015-02-13 02:12:42 +03:00
Dmitry Stogov
97fe15db43 Fix "forech" statemt behaviour according to https://wiki.php.net/rfc/php7_foreach
Squashed commit of the following:

commit 1e41295097
Author: Dmitry Stogov <dmitry@zend.com>
Date:   Sat Jan 31 07:28:58 2015 +0300

    Generalize HashTableIterator API to allows its usage without involvement of HashTable.nInternalPonter

commit 5406f21b11
Author: Dmitry Stogov <dmitry@zend.com>
Date:   Fri Jan 30 18:08:43 2015 +0300

    Reduced alghorithms complexity

commit b37f1d58d2
Author: Dmitry Stogov <dmitry@zend.com>
Date:   Fri Jan 30 18:08:30 2015 +0300

    Fixed test name

commit fb2d079645
Author: Dmitry Stogov <dmitry@zend.com>
Date:   Fri Jan 30 18:08:05 2015 +0300

    API cleanup

commit 08302c0d6d
Author: Dmitry Stogov <dmitry@zend.com>
Date:   Fri Jan 30 14:20:46 2015 +0300

    Make array_splice() to preserve foreach hash position

commit cc4b7be41e
Author: Dmitry Stogov <dmitry@zend.com>
Date:   Fri Jan 30 12:24:31 2015 +0300

    Make internal function, operation on array passed by reference, to preserve foreach hash position

commit 5aa9712b0a
Author: Dmitry Stogov <dmitry@zend.com>
Date:   Fri Jan 30 09:49:35 2015 +0300

    Implement consistent behavior for foreach by value over plain object

commit 4c5b385ff5
Author: Dmitry Stogov <dmitry@zend.com>
Date:   Fri Jan 30 07:56:37 2015 +0300

    More careful iterators update.

commit 721fc9e80d
Author: Dmitry Stogov <dmitry@zend.com>
Date:   Thu Jan 29 21:43:28 2015 +0300

    Added new test

commit 15a23b1218
Author: Dmitry Stogov <dmitry@zend.com>
Date:   Thu Jan 29 21:05:02 2015 +0300

    Reimplement iteration magic with HashTableIterators (see https://wiki.php.net/rfc/php7_foreach#implementation_details)

commit 10a3260b1f
Author: Dmitry Stogov <dmitry@zend.com>
Date:   Thu Jan 29 21:04:44 2015 +0300

    New test

commit eef80c5837
Author: Dmitry Stogov <dmitry@zend.com>
Date:   Wed Jan 28 16:52:21 2015 +0300

    Fixed foreach by reference iteration over constant array

commit 61e7391873
Author: Dmitry Stogov <dmitry@zend.com>
Date:   Wed Jan 28 14:59:54 2015 +0300

    Fixed temporary variable re-allocation pass

commit 92e90c09f0
Author: Dmitry Stogov <dmitry@zend.com>
Date:   Wed Jan 28 12:44:57 2015 +0300

    Fixed operand destruction in case of exceptions in iterator

commit dd2a36a207
Author: Dmitry Stogov <dmitry@zend.com>
Date:   Wed Jan 28 10:02:34 2015 +0300

    Use GET_OP1_ZVAL_PTR_DEREF() (IS_TMP_VAR and IS_CONST can't be IS_REFERENCE)

commit 4638f7b914
Author: Dmitry Stogov <dmitry@zend.com>
Date:   Wed Jan 28 07:43:28 2015 +0300

    Change "foreach" statement behavior (this is just a PoC yet)

    - "foreach by value" don't relay on internal array/object pointer and doesnt perform array duplication. It just locks it incrementing reference counter. If the original array is modified by some code, the copy on write is performed and "foreach" still work with the old copy.

    - it makes no difference if array given to "foreach by value" is reference itself

    - "foreach by reference" still use internal array/object pointer and should work similar to PHP-5. (This id not completely implemented)
2015-02-12 13:57:12 +03:00
Xinchen Hui
7062465be0 Merge branch 'PHP-5.5' into PHP-5.6 2015-02-12 10:59:59 +08:00
Xinchen Hui
f2940b7120 Fixed bug #69017 (Fail to push to the empty array with the constant value defined in class scope) 2015-02-12 10:59:28 +08:00
Xinchen Hui
fc33f52d8c bump year 2015-01-15 23:27:30 +08:00
Xinchen Hui
0579e8278d bump year 2015-01-15 23:26:37 +08:00
Xinchen Hui
73c1be2653 Bump year 2015-01-15 23:26:03 +08:00
Dmitry Stogov
34723d1b75 Fixed malloc/emalloc mess 2015-01-15 18:05:58 +03:00
Dmitry Stogov
989daeb5bb Initialize ht->nTableMask 2015-01-15 15:21:38 +03:00
Xinchen Hui
8a0bc38535 Merge branch 'zend_sort' of https://github.com/laruence/php-src 2015-01-14 22:15:41 -05:00
Anatol Belski
8e3764e287 fixed zend_hash_init() when the lzcnt instruction isn't supported 2015-01-14 12:32:23 +01:00
Xinchen Hui
2193de0d18 Faster sorting algo 2015-01-14 18:02:41 +08:00
Stanislav Malyshev
b7a7b1a624 trailing whitespace removal 2015-01-10 15:07:38 -08:00
Dmitry Stogov
37466b0368 Use special flag (HASH_FLAG_INITIALIZED) instead of (nTableMask == 0) to indicate that HashTable is allocated.
Make nTableMask to be 0 for packed arrays.
Remove checks fo HASH_FLAG_PACKED in zend_hash_find/zend_hash_del and family (string keys are resolved through uninitialized_bucket).
Change HashTable layout for better locality.
2014-12-27 11:07:12 +03:00
Nikita Popov
2d212b426a Drop duplicate arg from hash_get_current_key_ex 2014-12-26 21:06:18 +01:00
Nikita Popov
3d12fc796b Small cleanups in hash implementation
* Drop unnecessary checks in non-inlined part of numeric key
   detection
 * Drop zend_hash_apply_deleter in favor of zend_hash_del_el.
   Also move block/unblock interruption macros in there.
2014-12-26 21:06:18 +01:00
K.
c462ff7ac2 zend_hash_do_resize: fix compacting condition
nNumUsed should always be greater or equal to nNumOfElements so original condition is never true and arrays are always doubled in size and compaction is never triggered
2014-12-23 15:32:15 +01:00
Anatol Belski
e60b7207db cleanup 2014-12-23 13:00:29 +01:00
Dmitry Stogov
441270db2c Simplify condition 2014-12-19 13:06:34 +03:00
Anatol Belski
bdeb220f48 first shot remove TSRMLS_* things 2014-12-13 23:06:14 +01:00
Dmitry Stogov
3727e26456 Improved zend_hash_clean() and added new optimized zend_symtable_clean() 2014-11-25 14:17:21 +03:00
Dmitry Stogov
ab7b38e336 Added new optimized zend_array_destroy() function 2014-11-25 12:58:29 +03:00
Anatol Belski
6b3b4fc944 joined identical conditional blocks 2014-11-18 13:19:46 +01:00
Anatol Belski
b9f1daa976 basic clang compatibility on windows 2014-11-07 10:17:59 +01:00
Dmitry Stogov
4f3763e202 Micro optimizations 2014-11-06 17:45:41 +03:00
Anatol Belski
e70dce7d99 fix datatype mismatch 2014-10-23 10:29:57 +02:00
Anatol Belski
53797ec1c9 move the more common case up 2014-10-23 10:29:56 +02:00
Dmitry Stogov
8793aa407e Optimized HashTable size calculation 2014-10-23 00:26:32 +04:00
Anatol Belski
d5ccf4d276 eliminate one branch when computing the initial table size 2014-10-22 19:23:29 +02:00
Anatol Belski
f59daf2104 micro optimized initial hash size calculation
most of the calls are with a size <= 8, so don't even bother any
bitshifts. For the bigger sizes, use an intrinsic (should be extended
with gcc ones though).
2014-10-22 17:56:24 +02:00
Dmitry Stogov
2a1250fa57 Micro optimization for zend_hash_next_index_insert_new() 2014-10-16 19:19:10 +04:00
Dmitry Stogov
bccc653185 Avoid double IS_INTERNED() check 2014-09-19 17:32:50 +04:00
Dmitry Stogov
1e0e1b4a73 Split big _zend_handle_numeric_str() into small always inlined function that makes initial check and regular big function 2014-09-18 18:50:05 +04:00
Dmitry Stogov
c2decc6afc optimized colision list traverse 2014-09-17 00:52:03 +04:00
Nikita Popov
311a67a2ed Remove HASH_NEXT_INSERT flag
We already pass ht->nNextFreeElement and the rest is handled by
ZEND_HASH_ADD.
2014-09-16 18:29:00 +02:00
Nikita Popov
0bf55b65d4 Remove two more always-true conditions 2014-09-16 18:28:59 +02:00
Dmitry Stogov
6991780793 Optimized FE_RESET and FE_FETCH 2014-09-16 01:34:27 +04:00
Nikita Popov
a7bfd006bd Remove always-true comparisons 2014-09-15 23:07:31 +02:00
Nikita Popov
ca43b99fc6 Update types in zend_hash
* String lengths are size_t
 * Indexes are uint32_t
 * Flags are uint32_t
 * Booleans are zend_bool
2014-09-15 23:07:31 +02:00
Dmitry Stogov
219794e1cd Split and optimize loop 2014-08-27 00:52:50 +04:00
Anatol Belski
4d997f63d9 master renames phase 3 2014-08-25 20:22:49 +02:00
Anatol Belski
6f9f0bf205 master renames phase 2 2014-08-25 19:28:33 +02:00
Anatol Belski
c3e3c98ec6 master renames phase 1 2014-08-25 19:24:55 +02:00
Anatol Belski
864172d9a4 further fixes to ext/standard and zend 2014-08-16 17:31:40 +02:00
Anatol Belski
8ee2a4a9b5 first shot on merging the core fro the int64 branch 2014-08-16 11:16:11 +02:00
Dmitry Stogov
2ece977c80 Fixed conversion of empty packed array into hash 2014-08-14 13:44:37 +04:00
Dmitry Stogov
c1965f58d4 Use reference counting instead of zval duplication 2014-06-05 16:04:11 +04:00
Dmitry Stogov
b3b616cf7e Introduced immutable arrays. They don't need to be copyed and may be used directly from SHM. 2014-05-29 18:21:56 +04:00
Dmitry Stogov
40256e0f9c Use specialized functions instead of macros 2014-05-26 17:16:16 +04:00
Dmitry Stogov
b66728aabb Removed useless initializations 2014-05-26 11:42:58 +04:00
Dmitry Stogov
9eb89dddb1 Use optimized zend_array_dup() function. convert zend_hash_num_elements() and zend_hash_next_free_element() into macros. 2014-05-23 20:37:53 +04:00
Dmitry Stogov
21da8af578 Removed the old hack that was originally developed to support constants in array indeces, and isn't needed anymore after the Bob's patch. 2014-05-19 01:20:56 +04:00
Dmitry Stogov
d07c1941ec Use new hash iteration API and avoid old zend_hash_update_current_key() hack 2014-05-19 00:50:00 +04:00
Tjerk Meesters
e7b50a8ee9 Convert to unpacked before deleting elements
Return FAILURE immediately if a string index removal is attempted.

Moved condition higher up
2014-05-13 11:58:28 +08:00
Nikita Popov
b30c7fe263 Port JSON 2014-05-05 01:00:59 +02:00
Nikita Popov
69b5ee61d0 Fixed bug #67169: []= after_array_splice incorrect
This fixes a regression I introduced in beta 1.
2014-05-02 12:49:51 +02:00
Dmitry Stogov
b1ff152782 Reimplement strtr() 2014-04-24 19:14:29 +04:00
Nikita Popov
c98d373eb6 Fix strict array comparison
Fixes Symfony test failures
2014-04-23 20:34:45 +02:00
Dmitry Stogov
72c287bd23 Combine HashTable.flags and HashTable.nApplyCount into single 32-bit word 2014-04-21 18:25:34 +04:00
Dmitry Stogov
050d7e38ad Cleanup (1-st round) 2014-04-15 15:40:40 +04:00
Nikita Popov
ad2ef3d97e Add only_integer_keys option to zend_hash_reindex
This allows to either do a full reindex, or only reindex the integer
keys.
2014-04-09 12:31:21 +02:00
Nikita Popov
22d3eb3117 Add zend_hash_splice
This implements the original functionality of php_splice, but
as an in-place operation, thus avoiding copying the HT.

This is much faster (~10x) if the splice removes a small portion
of the array and doesn't insert many elements.
2014-04-09 12:31:21 +02:00
Nikita Popov
1aa8719e32 Add zend_hash_reindex
The implementation differs from the original in array.c in that it
rehashes the hashtable in the same loop. This is approximately two
times faster (not counting the rare case of a purely associative
array).
2014-04-09 12:31:21 +02:00
Dmitry Stogov
3d17219cd8 Refactored zend_hash_* iteration API zend_hash_fove_forward_ex(ht, pos) and family require second argument to be real pointer.
&(ht)->nInternalPointer should be passed instead of NULL.
zend_hash_update_current_key() may work only with internal pointer.
2014-04-07 23:14:17 +04:00
Dmitry Stogov
3167b49ce9 Allocate HashTable->arData and HashTable->arHash at once 2014-04-07 18:22:03 +04:00
Dmitry Stogov
3485b11499 "incremental" hash bucket initialization 2014-04-07 13:31:44 +04:00
Dmitry Stogov
76cc99fe60 Refactored ZVAL flags usage to simplify various checks (e.g. Z_REFCOUNTED(), candidate for GC, etc) 2014-04-03 15:26:23 +04:00
Dmitry Stogov
d8099d0468 Changed data layout to allow more efficient operations 2014-04-02 14:34:44 +04:00
Dmitry Stogov
60961bba3d Avoid useless memset() 2014-04-01 23:56:45 +04:00
Dmitry Stogov
042c937f69 Use STR_COPY() instead of STR_DUP() where possible 2014-04-01 17:10:15 +04:00
Dmitry Stogov
887189ca31 Refactored IS_INDIRECT usage for CV and object properties to support HashTable resizing 2014-03-26 18:07:31 +04:00
Anatol Belski
9d4001f1c8 fix C89 compat 2014-03-22 10:44:01 +01:00
Nikita Popov
f331ed1414 Revert "Add helper function for updating bucket contents"
Reverting this for now, because it would require further changes
thanks to the zend signals tsrms-but-only-sometimes awesomeness.
2014-03-21 01:32:49 +01:00
Nikita Popov
4e7e301dda Rename+move zend_hash_apply_deleter and reuse it
The interruption handling was normalized to wrap the entire deletion
operation (instead of leaving out the destructor call and/or the
deallocation) per the recommendation of somebody (don't remember who)
familiar with zend signals.

There is an always-inlined variant of the function for use in
the main deletion function, to ensure there is no performance
impact.
2014-03-21 00:14:52 +01:00
Nikita Popov
4efa58dd59 Make zend_hash_apply_deleter() void, to allow reuse
Also remove lies in the comment above the function. apply_deleter
is not safe against modification, if the modification involves
removing the next bucket.

Also modified the implementation of zend_hash_graceful_destroy()
to be fully graceful (the reverse variant already was).
2014-03-21 00:14:51 +01:00
Nikita Popov
c3a317117a Add helper function for updating bucket contents 2014-03-21 00:14:46 +01:00
Nikita Popov
994fcfcff9 Remove unnecessary check
In case of an update the key already existed, so the next free
element can not change.
2014-03-20 22:58:10 +01:00
Dmitry Stogov
62c448ab8b Fixed serialize/unserialize problems 2014-03-17 17:23:27 +04:00