Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:64552 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 65019 invoked from network); 5 Jan 2013 16:49:20 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 5 Jan 2013 16:49:20 -0000 Authentication-Results: pb1.pair.com smtp.mail=nikita.ppv@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=nikita.ppv@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.215.49 as permitted sender) X-PHP-List-Original-Sender: nikita.ppv@gmail.com X-Host-Fingerprint: 209.85.215.49 mail-la0-f49.google.com Received: from [209.85.215.49] ([209.85.215.49:33744] helo=mail-la0-f49.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id DF/30-62408-D8958E05 for ; Sat, 05 Jan 2013 11:49:18 -0500 Received: by mail-la0-f49.google.com with SMTP id fk20so11744733lab.36 for ; Sat, 05 Jan 2013 08:49:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=CDbXIR+XyUtNfFlYKir/SxMtWndY5HL1VPkpnszREAc=; b=yAu8U+y+BYBMyal/rhjHEpnMjQ7Q1MC68/thjOQmAGYQ4IgXCZ9kDoXukSqkMvvTfA gJN8ubO/xLfsm6rNPKN1cVmEjE20uhD88iaaAGhmZgI1hlmSmvDKpNSWryPZyWMzkjAl nocTn7+tbfE7wAZkFHqJ0BWPVfZnwcZmGohJp/w5qS5A1IVIXcO5T9t6v9ahOdnRqLVr nWfQOTHCR7eP5tC35UfMUMULKIjKJFbPV8ykSWHCaympkS/hnL/1Sx2j+lZBnnr9kDmp aC/Vyv0Al8Kn+yvxvkDF0tFmfGLWxysxpWZE3DcWI2x7QMpgwV2EDOJT7YGYEg/tNU59 Oluw== MIME-Version: 1.0 Received: by 10.112.16.69 with SMTP id e5mr22929602lbd.43.1357404554552; Sat, 05 Jan 2013 08:49:14 -0800 (PST) Received: by 10.112.4.168 with HTTP; Sat, 5 Jan 2013 08:49:14 -0800 (PST) In-Reply-To: References: Date: Sat, 5 Jan 2013 17:49:14 +0100 Message-ID: To: Nikita Nefedov Cc: "internals@lists.php.net" Content-Type: multipart/alternative; boundary=f46d0401fb414a67b104d28d6032 Subject: Re: [PHP-DEV] Ruby's symbols From: nikita.ppv@gmail.com (Nikita Popov) --f46d0401fb414a67b104d28d6032 Content-Type: text/plain; charset=ISO-8859-1 On Sat, Jan 5, 2013 at 6:58 PM, Nikita Nefedov wrote: > ** > Though there would be a little speed-up because with Symbols array's > Buckets will keep numeric key, so instead of memcmp you will need to just > compare two longs when retrieving element. > Before memcmping the array keys PHP will first compare the pointers. For interned strings the pointers will be the same so the memcmp is not done. See http://lxr.php.net/xref/PHP_TRUNK/Zend/zend_hash.c#950. > Actually this is looks a little bad now, AFAIK this is what happens when > you trying to receive value from array by string key: > > 1. Calling zend_new_interned_string_int for interning or getting > already interned same string, receiving pointer to the stored string from > it: > 1. Hash the string (O(n)) > 2. Retrieve bucket from arBuckets > 3. Find needed bucket by iterating over all retrieved buckets (over > *pLast) and comparing its keys with memcmp > 4. If found - return pointer to string, else create new bucket... > 2. Now that we have an interned string, we can try to retrieve value > from array with string: > 1. Hash the string again (O(n)) > 2. Retrieve bucket by hash fro arBuckets > 3. And again memcmp used for comparing strings > > The string interning happens at compile time, so for all practical purposes it does not matter much how long it takes. At runtime the hash is already precalculated and the ptrs are the same (see above) so the array access goes through pretty much the fastest possible code-path. I quickly tried out how fast they are compared to numeric keys: http://codepad.viper-7.com/Km9hPz String and integer keys perform pretty much the same there. Nikita --f46d0401fb414a67b104d28d6032--