Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:21729 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 47578 invoked by uid 1010); 30 Jan 2006 03:49:37 -0000 Delivered-To: ezmlm-scan-internals@lists.php.net Delivered-To: ezmlm-internals@lists.php.net Received: (qmail 47563 invoked from network); 30 Jan 2006 03:49:37 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 30 Jan 2006 03:49:37 -0000 X-Host-Fingerprint: 80.74.107.235 mail.zend.com Linux 2.5 (sometimes 2.4) (4) Received: from ([80.74.107.235:15548] helo=mail.zend.com) by pb1.pair.com (ecelerity 2.0 beta r(6323M)) with SMTP id FA/E0-37522-FCC8DD34 for ; Sun, 29 Jan 2006 22:49:36 -0500 Received: (qmail 32249 invoked from network); 30 Jan 2006 03:49:31 -0000 Received: from localhost (HELO ANDI-NOTEBOOK.zend.com) (127.0.0.1) by localhost with SMTP; 30 Jan 2006 03:49:31 -0000 Message-ID: <7.0.0.16.2.20060129194905.0423dce0@zend.com> X-Mailer: QUALCOMM Windows Eudora Version 7.0.0.16 Date: Sun, 29 Jan 2006 19:49:28 -0800 To: "Sara Golemon" ,"Stefan Esser" Cc: In-Reply-To: <000c01c62534$d48aef80$7d051fac@stumpy> References: <43DD2D20.5010001@php.net> <000c01c62534$d48aef80$7d051fac@stumpy> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii"; format=flowed Subject: Re: [PHP-DEV] Re: zend_hash.c fishy code From: andi@zend.com (Andi Gutmans) Yep correct. We always prefer to also check p->h so that we minimize the chances for reaching the memcmp()... At 04:33 PM 1/29/2006, Sara Golemon wrote: >>I wonder If I am completely missing the point, but the following piece >>of code seems fishy to me: >> >>ZEND_API int zend_hash_del_key_or_index(HashTable *ht, char *arKey, uint >>nKeyLength, ulong h, int flag) >>{ >> uint nIndex; >> Bucket *p; >> >> ... >> >> while (p != NULL) { >> if ((p->h == h) && ((p->nKeyLength == 0) || /* Numeric >>index */ >> ((p->nKeyLength == nKeyLength) && >>(!memcmp(p->arKey, arKey, nKeyLength))))) { >> HANDLE_BLOCK_INTERRUPTIONS(); >> ... >> >>If I am not completely mistaken this means: If this is a bucket with a >>numeric index with the hash value of the key we want to delete, then >>delete it, even when we wanted to delete a key with a string as index. >There's a fairly important bit in that first ...: > if (flag == HASH_DEL_KEY) { > h = zend_inline_hash_func(arKey, nKeyLength); > } > >When deleting a numeric index nKeyLength is passed as zero and h >contains the numeric index so this if only matches when p->h == h >and p->nKeyLength == 0 (indicating a numericly keyed bucket). > >When deleting a string index, nKeyLength is non-zero and h as passed >is unimportant since it's overridden with the actual hash of arKey >and nKeyLength. The if then only evaluates true when the bucket >hash matches the computed hash and the real key data in the bucket >matches the real key data passed. > >Yes, comparing p->h to h seems unnecessary since p->arKey is being >compared to arKey anyway, but that seemingly redundant comparison >saves a more costly memcmp() when the hashes differ. > >-Sara >-- >PHP Internals - PHP Runtime Development Mailing List >To unsubscribe, visit: http://www.php.net/unsub.php