Re: Looking for an example of zend_hash_quick_add() or zend_hash_add()

From: Date: Tue, 06 May 2014 10:49:24 +0000
Subject: Re: Looking for an example of zend_hash_quick_add() or zend_hash_add()
References: 1  Groups: php.internals 
Request: Send a blank email to internals+get-73963@lists.php.net to get a copy of this message
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 > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > > 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

« previous php.internals (#73963) next »