Skip to content
Tcl Source Code Artifact [2bdaba3a44]
Login

Artifact [2bdaba3a44]

Artifact 2bdaba3a4424d3c96ec4df27ab1d89b08c2f70ba:

Wiki page [Hash Tables] by dgp on 2014-09-09 21:56:24.
D 2014-09-09T21:56:24.162
L Hash\sTables
P 023ca64f1d3aab35d48e7d644c3131b64c113b12
U dgp
W 4138
<h1>Source Files</h1>
  *  [/finfo?name=generic/tclHash.c|tclHash.c]

<h1>Public Interface</h1>
  *  struct Tcl_HashTable
  *  struct Tcl_HashEntry
  *  Tcl_InitHashTable
  *  Tcl_InitCustomHashTable
  *  Tcl_FindHashEntry
  *  Tcl_CreateHashEntry
  *  Tcl_DeleteHashEntry
  *  Tcl_DeleteHashTable
  *  Tcl_FirstHashEntry
  *  Tcl_NextHashEntry
  *  Tcl_HashStats
  *  Tcl_GetHashValue
  *  Tcl_SetHashValue
  *  Tcl_GetHashKey

<h1>Private Interface</h1>
  *  <i>None</a>

<h1>Directly Depends On Public Interface</h1>
  *  [Panic]
  *  [Allocation]

<h1>Directly Depends On Private Interface of</h1>
  *  <i>None</i>

<h1>Discussion</h1>

The key feature of the interface of this module is that the central struct
definitions are open.  This severely limits the freedom to further develop
the functionality.  Opaque structs tend to make for a better encapsulation
of private vs public concerns.

The open structs do offer some features that opaque structs would not
permit.  The caller of these routines may place their <b>Tcl_HashTable</b>
in static storage and eliminate whatever cost is paid in dynamic allocation
of the storage, as well as the housekeeping burden of arranging for the
release of that storage.  Also, many "functions" in the interface may be
implemented as macros that directly access the known fields of the open
struct, and that might have some performance benefit.

It is possible to still have evolution with open struct definitions, but it
requires significant effort to maintain all the necessary compatibilities.
The <b>Tcl_HashKeyType</b> struct possesses the first enabler of such
a scheme -- a version field, but there's been either no need or no ability
to revise that struct.  In contrast, the <b>Tcl_HashEntry</b> struct
has effectively had its bucketPtr field replaced by a hash field and without
a version field in place, the illusion that we still support callers who want
to keep using the bucketPtr field is something of a cruel joke.  The Tcl
library (and the tclHash.c file) gets built with <b>TCL_HASH_KEY_STORE_HASH</b>
either set to 1 or not.  After that, the routines do what they do, and any caller
expecting the other setting is just going to be broken.  What's in place is at
best a measure of source compatibility helpful only to those who lift Tcl's
source code and copy it into their own programs.

The advantage of static Tcl_HashTable allocation by callers is diminished by
the fact that the routines in this module only function if thread isolation is
preserved.  Any particular (Tcl_HashTable *) may only be passed into
these routines by one thread.  This suggests HashTables belong in thread
specific memory, which already removes the static option of storage.

The customizable hash function is defined to return an unsigned int, so
only 2^32 distinct hashes can be used to sift data.  According to the internal
logic of the hash table implementation, this means the existing architecture
is suitable for holding at most  3 * 2^32 entries.  If we aim to store more than
that, the Tcl_HashKeyType will need to evolve.

In practice, that's not a problem because before we reach those sizes we
run into a fatal bug in RebuildTable, where the combination of the limited
range of ckalloc() and a failure to manage overflow of the numBuckets value
brings the program crashing down.

The bucket array only grows, never shrinks.

There has been some interest in addressing the potential vulnerability of
hashing collision attacks.  The difficulty is that the existing hashing regime
has a record of performing very well, and having a large impact on overall
Tcl performance.  Solutions that truly reduce the hazards of collision attacks
tend to be costly in performance.  It has been difficult to generate evidence
to support the claims of any alternatives to simultaneously perform well and
offer protection.

Other hashing criticisms have been raised occasionally that on some input sets,
the existing hashes do not randomize into the full 32 bits of hash value as
they should for best operations.

Z 270cf2622a675cb60540b38c4ade9221