# Looking for an example of zend_hash_quick_add() or zend_hash_add()

- Thread: https://externals.io/message/73883
- Original archive: https://news-web.php.net/php.internals/73883
- Started by Aaron Lewis on 2014-05-05T09:29:06+00:00
- Messages: 2
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #73883 — Aaron Lewis — 2014-05-05T09:29:06+00:00

URL: https://externals.io/message/73883#73883

Yes I  can find examples in existing source code,

zend_hash_quick_add(EG(active_symbol_table), key->arKey,
key->nKeyLength, key->h, &tmp, sizeof(zval*), (void**)&p);

But I only know the HashTable, key, key length parameters, I'm not
sure what to fill in for the rest

Can anyone give me an example?

-- 
Best Regards,
Aaron Lewis - PGP: 0x13714D33 - http://pgp.mit.edu/
Finger Print:   9F67 391B B770 8FF6 99DC  D92D 87F6 2602 1371 4D33

---

## #73963 — Ferenc Kovacs — 2014-05-06T10:49:24+00:00

URL: https://externals.io/message/73883#73963 · In reply to: #73883

On Mon, May 5, 2014 at 11:29 AM, Aaron Lewis <the.warl0ck.1989@gmail.com>wrote:

> Yes I  can find examples in existing source code,
>
> zend_hash_quick_add(EG(active_symbol_table), key->arKey,
> key->nKeyLength, key->h, &tmp, sizeof(zval*), (void**)&p);
>
> But I only know the HashTable, key, key length parameters, I'm not
> sure what to fill in for the rest
>
> Can anyone give me an example?
>
> --
> Best Regards,
> Aaron Lewis - PGP: 0x13714D33 - http://pgp.mit.edu/
> Finger Print:   9F67 391B B770 8FF6 99DC  D92D 87F6 2602 1371 4D33
>
> --
> 
> 
>
>
hi,

http://lxr.php.net/xref/PHP_5_6/Zend/zend_hash.h#114
http://lxr.php.net/xref/PHP_5_6/Zend/zend_hash.c#308

#define zend_hash_quick_update(ht, arKey, nKeyLength, h, pData, nDataSize,
pDest)

ht is the HashTable
arKey is the key/index
nKeyLength is the length of the key
h is hash of the key
pData is a pointer to the data
nDataSize is the size of the data eg sizeof(zval *)
pDest is the destination pointer, usually `NULL` because we don't care about
it most of the times


some docs on the hashtables:
http://www.php.net/manual/en/internals2.variables.tables.php
http://nikic.github.io/2012/03/28/Understanding-PHPs-internal-array-implementation.html
http://www.phpinternalsbook.com/hashtables.html (
http://www.phpinternalsbook.com/hashtables/basic_structure.html is a good
start)
(and in Sara's book, Extending and Embedding PHP).

and a simple example:
http://www.phpinternalsbook.com/hashtables/hashtable_api.html#string-keys

For all of the above functions there exists a second quick variant that
> accepts a precomputed hash value after the string length. This allows you
> to compute the hash of a string once and then reuse it across multiple
> calls:
> ulong h; /* hash value */
> /* ... zval init */
> h = zend_get_hash_value("foo", sizeof("foo"));
> zend_hash_quick_update(myht, "foo", sizeof("foo"), h, &zv, sizeof(zval *),
> NULL);
> if (zend_hash_quick_find(myht, "foo", sizeof("foo"), h, (void **)
> &zv_dest) == SUCCESS) {
>     php_printf("Fetched value at key \"foo\" into zv_dest\n");
> }
> if (zend_hash_quick_del(myht, "foo", sizeof("foo"), h) == SUCCESS) {
>     php_printf("Removed value at key \"foo\"\n");
> }
> Using the quick API improves performance as the hash value does not have
> to be recomputed on every call. It should be noted though that this only
> becomes significant if you are accessing the key a lot (e.g. in a loop).
> The quick functions are mostly used in the engine where precomputed hash
> values are available through various caches and optimizations.


ps: somebody please correct me if I'm wrong somewhere!

-- 
Ferenc Kovács
@Tyr43l - http://tyrael.hu
