NOTES ON DICTIONARIES================================Principal Use Cases for Dictionaries------------------------------------Passing keyword argumentsTypically, one read and one write for 1 to 3 elements.Occurs frequently in normal python code.Class method lookupDictionaries vary in size with 8 to 16 elements being common.Usually written once with many lookups.When base classes are used, there are many failed lookupsfollowed by a lookup in a base class.Instance attribute lookup and Global variablesDictionaries vary in size. 4 to 10 elements are common.Both reads and writes are common.BuiltinsFrequent reads. Almost never written.About 150 interned strings (as of Py3.3).A few keys are accessed much more frequently than others.UniquificationDictionaries of any size. Bulk of work is in creation.Repeated writes to a smaller set of keys.Single read of each key.Some use cases have two consecutive accesses to the same key.* Removing duplicates from a sequence.dict.fromkeys(seqn).keys()* Counting elements in a sequence.for e in seqn:d[e] = d.get(e,0) + 1* Accumulating references in a dictionary of lists:for pagenumber, page in enumerate(pages):for word in page:d.setdefault(word, []).append(pagenumber)Note, the second example is a use case characterized by a get and setto the same key. There are similar use cases with a __contains__followed by a get, set, or del to the same key. Part of thejustification for d.setdefault is combining the two lookups into one.Membership TestingDictionaries of any size. Created once and then rarely changes.Single write to each key.Many calls to __contains__() or has_key().Similar access patterns occur with replacement dictionariessuch as with the % formatting operator.Dynamic MappingsCharacterized by deletions interspersed with adds and replacements.Performance benefits greatly from the re-use of dummy entries.Data Layout-----------Dictionaries are composed of 3 components:The dictobject struct itselfA dict-keys object (keys & hashes)A values arrayTunable Dictionary Parameters-----------------------------See comments for PyDict_MINSIZE_SPLIT, PyDict_MINSIZE_COMBINED,USABLE_FRACTION and GROWTH_RATE in dictobject.cTune-ups should be measured across a broad range of applications anduse cases. A change to any parameter will help in some situations andhurt in others. The key is to find settings that help the most commoncases and do the least damage to the less common cases. Results willvary dramatically depending on the exact number of keys, whether thekeys are all strings, whether reads or writes dominate, the exacthash values of the keys (some sets of values have fewer collisions thanothers). Any one test or benchmark is likely to prove misleading.While making a dictionary more sparse reduces collisions, it impairsiteration and key listing. Those methods loop over every potentialentry. Doubling the size of dictionary results in twice as manynon-overlapping memory accesses for keys(), items(), values(),__iter__(), iterkeys(), iteritems(), itervalues(), and update().Also, every dictionary iterates at least twice, once for the memset()when it is created and once by dealloc().Dictionary operations involving only a single key can be O(1) unlessresizing is possible. By checking for a resize only when thedictionary can grow (and may *require* resizing), other operationsremain O(1), and the odds of resize thrashing or memory fragmentationare reduced. In particular, an algorithm that empties a dictionaryby repeatedly invoking .pop will see no resizing, which mightnot be necessary at all because the dictionary is eventuallydiscarded entirely.The key differences between this implementation and earlier versions are:1. The table can be split into two parts, the keys and the values.2. There is an additional key-value combination: (key, NULL).Unlike (<dummy>, NULL) which represents a deleted value, (key, NULL)represented a yet to be inserted value. This combination can only occurwhen the table is split.3. No small table embedded in the dict,as this would make sharing of key-tables impossible.These changes have the following consequences.1. General dictionaries are slightly larger.2. All object dictionaries of a single class can share a single key-table,saving about 60% memory for such cases.Results of Cache Locality Experiments--------------------------------------Experiments on an earlier design of dictionary, in which all tables werecombined, showed the following:When an entry is retrieved from memory, several adjacent entries are alsoretrieved into a cache line. Since accessing items in cache is *much*cheaper than a cache miss, an enticing idea is to probe the adjacententries as a first step in collision resolution. Unfortunately, theintroduction of any regularity into collision searches results in morecollisions than the current random chaining approach.Exploiting cache locality at the expense of additional collisions failsto payoff when the entries are already loaded in cache (the expenseis paid with no compensating benefit). This occurs in small dictionarieswhere the whole dictionary fits into a pair of cache lines. It alsooccurs frequently in large dictionaries which have a common access patternwhere some keys are accessed much more frequently than others. Themore popular entries *and* their collision chains tend to remain in cache.To exploit cache locality, change the collision resolution sectionin lookdict() and lookdict_string(). Set i^=1 at the top of theloop and move the i = (i << 2) + i + perturb + 1 to an unrolledversion of the loop.For split tables, the above will apply to the keys, but the value willalways be in a different cache line from the key.
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。