Path: csiph.com!usenet.pasdenom.info!gegeweb.org!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'subject:error': 0.03; 'cpython': 0.05; 'say,': 0.05; 'pypy': 0.07; 'string': 0.09; 'arrays': 0.09; 'compact': 0.09; 'indexes': 0.09; 'insertion': 0.09; 'interpreted': 0.09; 'key.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'structure,': 0.09; 'jan': 0.12; 'bytes).': 0.16; 'cached': 0.16; 'dict': 0.16; 'dictionaries': 0.16; 'different,': 0.16; 'entries,': 0.16; 'equal.': 0.16; 'index.': 0.16; 'iteration': 0.16; 'pointers,': 0.16; 'processors': 0.16; 'ptr': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'wrote:': 0.18; '(where': 0.19; 'basically': 0.19; 'seems': 0.21; 'proposed': 0.22; 'header:User-Agent:1': 0.23; 'bytes': 0.24; 'entries': 0.24; 'switched': 0.24; 'second': 0.26; 'least': 0.26; 'header:X -Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'array': 0.29; 'raymond': 0.30; 'comparison': 0.31; "d'aprano": 0.31; 'keys': 0.31; 'steven': 0.31; 'this.': 0.32; 'classes': 0.35; 'test': 0.35; 'instances': 0.36; 'leads': 0.36; 'entry': 0.36; 'two': 0.37; 'being': 0.38; 'needed': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'space': 0.40; 'above,': 0.60; 'cost.': 0.60; 'details.': 0.61; 'first': 0.61; 'making': 0.63; 'real': 0.63; 'such': 0.63; 'maximum': 0.63; 'specialized': 0.65; '7:00': 0.84; 'received:fios.verizon.net': 0.84; 'reductions': 0.84; 'items,': 0.91; 'received:108': 0.93 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: __bases__ misleading error message Date: Sun, 25 Jan 2015 16:35:48 -0500 References: <1a194e0a0b738d205de54180fa7@nntp.aioe.org> <54c39366$0$13006$c3e8da3$5496439d@news.astraweb.com> <54c4606a$0$13002$c3e8da3$5496439d@news.astraweb.com> <54c4dae1$0$13005$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-108-16-203-145.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.4.0 In-Reply-To: <54c4dae1$0$13005$c3e8da3$5496439d@news.astraweb.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 54 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1422221769 news.xs4all.nl 2902 [2001:888:2000:d::a6]:48437 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:84581 On 1/25/2015 7:00 AM, Steven D'Aprano wrote: > What happens inside the dictionary? Dictionaries are "hash tables", so they > are basically a big array of cells, and each cell is a pair of pointers, > one for the key and one for the value: > > [dictionary header] > [blank] > [blank] > [ptr to the string 'y', ptr to the int 42] At the moment, for CPython, each entry has 3 items, with the first being the cached hash of the key. Hash comparison is first used to test whether keys are equal. [hash('y'), ptr('y'), ptr(42)] > [blank] > [ptr to 'x', ptr to 23] > [blank] > [blank] > [blank] > [ptr to 'colour', ptr to 'red'] > [blank] As you say, these are implementation details. CPython dicts for the instances of at least some classes have a different, specialized structure, with two arrays. In the above, [blank] entries, which are about 1/2 to 2/3 of the entries, take the same space as real entries (12 to 24 bytes). Raymond H. has proposed that the standard dict have two arrays like so: 1. the first array is a sparse array of indexes into the second array: [b, b, 2, b, 0, b, b, b, 1, b] (where b might be -1 interpreted as ), using only as many bytes as needed for the maximum index. 2. the second array is a compact array of entries in insertion order, such as [hash, ptr to 'x', ptr to 23] [hash, ptr to 'colour', ptr to 'red'] [hash, ptr to the string 'y', ptr to the int 42] Iteration would use the compact array, making all dicts OrderedDicts. Pypy has already switched to this. It seems that on modern processors with multilevel on-chip caches, the space reduction leads to cache-miss reductions that compensate for the indirection cost. -- Terry Jan Reedy