Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2000-10-26
08:34 • Closed ticket [219102ffff]: Enhance Tcl hash table with Tcl_Obj* keys plus 4... artifact: a245807670 user: welch
01:10 • New ticket [219102ffff]. artifact: 5774378817 user: nobody

Enhance Tcl hash table with Tcl_Obj* keys

Closed / Fixed219102ffffffffffffffffffffffffffffffffffCreated 26 years agoUpdated 26 years ago
Descriptionplain2000-10-26 01:10:49
OriginalBugID: 5934 RFE
Version: 8.4a1
SubmitDate: '2000-06-22'
LastModified: '2000-06-23'
Severity: MED
Status: Released
Submitter: techsupp
ChangedBy: ericm
OS: All
FixedDate: '2000-06-23'
FixedInVersion: 8.4a2
ClosedDate: '2000-10-25'


Name:

Paul Duffin


DesiredBehavior:

The Tcl hash table allows strings, single words and multiple words as keys but not Tcl_Obj*. Tcl_Obj* have a number of advantages when used as a key because they do not have to be copied like string keys, comparisons can be speeded up by first comparing the Tcl_Obj * pointers and only if they are not the same do you have to check the lengths and only if they are the same do you have to compare the contents.


Patch:

Index: tcl/generic/tcl.h

diff -c tcl/generic/tcl.h:1.70 tcl/generic/tcl.h:1.71

*** tcl/generic/tcl.h:1.70Tue May 16 02:27:03 2000

--- tcl/generic/tcl.hTue May 16 02:30:01 2000

***************

*** 12,18 ****

   * See the file "license.terms" for information on usage and redistribution

   * of this file, and for a DISCLAIMER OF ALL WARRANTIES.

   *

!  * RCS: @(#) $Id: tcl.h,v 1.70 2000/05/16 01:27:03 pduffin Exp $

   */


  #ifndef _TCL

--- 12,18 ----

   * See the file "license.terms" for information on usage and redistribution

   * of this file, and for a DISCLAIMER OF ALL WARRANTIES.

   *

!  * RCS: @(#) $Id: tcl.h,v 1.71 2000/05/16 01:30:01 pduffin Exp $

   */


  #ifndef _TCL

***************

*** 969,974 ****

--- 969,975 ----

   * with Tcl_SetHashValue. */

      union {/* Key has one of these forms: */

  char *oneWordValue;/* One-word value for key. */

+         Tcl_Obj *objPtr;/* Tcl_Obj * key value. */

  int words[1];/* Multiple integer words for key.

   * The actual size will be as large

   * as necessary for this table's

***************

*** 1005,1014 ****

      int mask;/* Mask value used in hashing

   * function. */

      int keyType;/* Type of keys used in this table. 

!  * It's either TCL_STRING_KEYS,

!  * TCL_ONE_WORD_KEYS, or an integer

!  * giving the number of ints that

!                                          * is the size of the key.

   */

      Tcl_HashEntry *(*findProc) _ANSI_ARGS_((struct Tcl_HashTable *tablePtr,

      CONST char *key));

--- 1006,1015 ----

      int mask;/* Mask value used in hashing

   * function. */

      int keyType;/* Type of keys used in this table. 

!  * It's either TCL_OBJ_KEYS,

!  * TCL_STRING_KEYS, TCL_ONE_WORD_KEYS,

!  * or an integer giving the number of

!  * ints that is the size of the key.

   */

      Tcl_HashEntry *(*findProc) _ANSI_ARGS_((struct Tcl_HashTable *tablePtr,

      CONST char *key));

***************

*** 1033,1038 ****

--- 1034,1040 ----

   * Acceptable key types for hash tables:

   */


+ #define TCL_OBJ_KEYS-1

  #define TCL_STRING_KEYS0

  #define TCL_ONE_WORD_KEYS1


***************

*** 1043,1050 ****

  #define Tcl_GetHashValue(h) ((h)->clientData)

  #define Tcl_SetHashValue(h, value) ((h)->clientData = (ClientData) (value))

  #define Tcl_GetHashKey(tablePtr, h) \

!     ((char *) (((tablePtr)->keyType == TCL_ONE_WORD_KEYS) ? (h)->key.oneWordValue \

! : (h)->key.string))


  /*

   * Macros to use for clients to use to invoke find and create procedures

--- 1045,1054 ----

  #define Tcl_GetHashValue(h) ((h)->clientData)

  #define Tcl_SetHashValue(h, value) ((h)->clientData = (ClientData) (value))

  #define Tcl_GetHashKey(tablePtr, h) \

!     ((char *) (((tablePtr)->keyType == TCL_ONE_WORD_KEYS || \

! (tablePtr)->keyType == TCL_OBJ_KEYS) \

!        ? (h)->key.oneWordValue \

!        : (h)->key.string))


  /*

   * Macros to use for clients to use to invoke find and create procedures

Index: tcl/generic/tclHash.c

diff -c tcl/generic/tclHash.c:1.3 tcl/generic/tclHash.c:1.4

*** tcl/generic/tclHash.c:1.3Tue May 16 02:26:38 2000

--- tcl/generic/tclHash.cTue May 16 02:30:01 2000

***************

*** 10,16 ****

   * See the file "license.terms" for information on usage and redistribution

   * of this file, and for a DISCLAIMER OF ALL WARRANTIES.

   *

!  * RCS: @(#) $Id: tclHash.c,v 1.3 2000/05/16 01:26:38 pduffin Exp $

   */


  #include "tclInt.h"

--- 10,16 ----

   * See the file "license.terms" for information on usage and redistribution

   * of this file, and for a DISCLAIMER OF ALL WARRANTIES.

   *

!  * RCS: @(#) $Id: tclHash.c,v 1.4 2000/05/16 01:30:01 pduffin Exp $

   */


  #include "tclInt.h"

***************

*** 56,61 ****

--- 56,66 ----

      CONST char *key));

  static Tcl_HashEntry *OneWordCreate _ANSI_ARGS_((Tcl_HashTable *tablePtr,

      CONST char *key, int *newPtr));

+ static unsigned intHashObj _ANSI_ARGS_((Tcl_Obj *objPtr));

+ static Tcl_HashEntry *ObjFind _ANSI_ARGS_((Tcl_HashTable *tablePtr,

+     CONST char *key));

+ static Tcl_HashEntry *ObjCreate _ANSI_ARGS_((Tcl_HashTable *tablePtr,

+     CONST char *key, int *newPtr));


  /*

   *----------------------------------------------------------------------

***************

*** 97,103 ****

      tablePtr->downShift = 28;

      tablePtr->mask = 3;

      tablePtr->keyType = keyType;

!     if (keyType == TCL_STRING_KEYS) {

  tablePtr->findProc = StringFind;

  tablePtr->createProc = StringCreate;

      } else if (keyType == TCL_ONE_WORD_KEYS) {

--- 102,111 ----

      tablePtr->downShift = 28;

      tablePtr->mask = 3;

      tablePtr->keyType = keyType;

!     if (keyType == TCL_OBJ_KEYS) {

! tablePtr->findProc = ObjFind;

! tablePtr->createProc = ObjCreate;

!     } else if (keyType == TCL_STRING_KEYS) {

  tablePtr->findProc = StringFind;

  tablePtr->createProc = StringCreate;

      } else if (keyType == TCL_ONE_WORD_KEYS) {

***************

*** 147,152 ****

--- 155,164 ----

      }

  }

      }

+     

+     if (entryPtr->tablePtr->keyType == TCL_OBJ_KEYS) {

+ Tcl_DecrRefCount (entryPtr->key.objPtr);

+     }

      entryPtr->tablePtr->numEntries--;

      ckfree((char *) entryPtr);

  }

***************

*** 183,188 ****

--- 195,203 ----

  hPtr = tablePtr->buckets[i];

  while (hPtr != NULL) {

      nextPtr = hPtr->nextPtr;

+     if (tablePtr->keyType == TCL_OBJ_KEYS) {

+ Tcl_DecrRefCount (hPtr->key.objPtr);

+     }

      ckfree((char *) hPtr);

      hPtr = nextPtr;

  }

***************

*** 844,849 ****

--- 859,1092 ----

  /*

   *----------------------------------------------------------------------

   *

+  * HashObj --

+  *

+  *Compute a one-word summary of the string representation of the

+  *Tcl_Obj, which can be used to generate a hash index.

+  *

+  * Results:

+  *The return value is a one-word summary of the information in

+  *the string representation of the Tcl_Obj.

+  *

+  * Side effects:

+  *None.

+  *

+  *----------------------------------------------------------------------

+  */

+ 

+ static unsigned int

+ HashObj(objPtr)

+     Tcl_Obj *objPtr;

+ {

+     register CONST char *string;

+     register int length;

+     register unsigned int result;

+     register int c;

+ 

+     string = Tcl_GetStringFromObj (objPtr, NULL);

+     length = objPtr->length;

+     

+     /*

+      * I tried a zillion different hash functions and asked many other

+      * people for advice.  Many people had their own favorite functions,

+      * all different, but no-one had much idea why they were good ones.

+      * I chose the one below (multiply by 9 and add new character)

+      * because of the following reasons:

+      *

+      * 1. Multiplying by 10 is perfect for keys that are decimal strings,

+      *    and multiplying by 9 is just about as good.

+      * 2. Times-9 is (shift-left-3) plus (old).  This means that each

+      *    character's bits hang around in the low-order bits of the

+      *    hash value for ever, plus they spread fairly rapidly up to

+      *    the high-order bits to fill out the hash value.  This seems

+      *    works well both for decimal and non-decimal strings.

+      */

+ 

+     result = 0;

+     while (length) {

+ c = *string;

+ string++;

+ length--;

+ if (length == 0) {

+     break;

+ }

+ result += (result<<3) + c;

+     }

+     return result;

+ }

+  

+ /*

+  *----------------------------------------------------------------------

+  *

+  * ObjFind --

+  *

+  *Given a hash table with string keys, and a string key, find

+  *the entry with a matching key.

+  *

+  * Results:

+  *The return value is a token for the matching entry in the

+  *hash table, or NULL if there was no matching entry.

+  *

+  * Side effects:

+  *None.

+  *

+  *----------------------------------------------------------------------

+  */

+ 

+ static Tcl_HashEntry *

+ ObjFind(tablePtr, key)

+     Tcl_HashTable *tablePtr;/* Table in which to lookup entry. */

+     CONST char *key;/* Key to use to find matching entry. */

+ {

+     Tcl_Obj *objPtr = (Tcl_Obj *) key;

+     register Tcl_HashEntry *hPtr;

+     register CONST char *p1, *p2;

+     register int l1, l2;

+     int index;

+ 

+     index = HashObj(objPtr) & tablePtr->mask;

+ 

+     /*

+      * Search all of the entries in the appropriate bucket.

+      */

+ 

+     for (hPtr = tablePtr->buckets[index]; hPtr != NULL;

+     hPtr = hPtr->nextPtr) {

+ /*

+  * If the object pointers are the same then they match.

+  */

+ if (objPtr == hPtr->key.objPtr) {

+     return hPtr;

+ }

+ 

+ p1 = Tcl_GetStringFromObj (objPtr, (int *) 0);

+ l1 = objPtr->length;

+ p2 = Tcl_GetStringFromObj (hPtr->key.objPtr, (int *) 0);

+ l2 = hPtr->key.objPtr->length;

+ 

+ /*

+  * If the lengths are different then they do not match.

+  */

+ if (l1 != l2) {

+     continue;

+ }

+ 

+ for (;; p1++, p2++, l1--) {

+     if (*p1 != *p2) {

+ break;

+     }

+     if (l1 == 0) {

+ return hPtr;

+     }

+ }

+     }

+     return NULL;

+ }

+  

+ /*

+  *----------------------------------------------------------------------

+  *

+  * ObjCreate --

+  *

+  *Given a hash table with string keys, and a string key, find

+  *the entry with a matching key.  If there is no matching entry,

+  *then create a new entry that does match.

+  *

+  * Results:

+  *The return value is a pointer to the matching entry.  If this

+  *is a newly-created entry, then *newPtr will be set to a non-zero

+  *value;  otherwise *newPtr will be set to 0.  If this is a new

+  *entry the value stored in the entry will initially be 0.

+  *

+  * Side effects:

+  *A new entry may be added to the hash table.

+  *

+  *----------------------------------------------------------------------

+  */

+ 

+ static Tcl_HashEntry *

+ ObjCreate(tablePtr, key, newPtr)

+     Tcl_HashTable *tablePtr;/* Table in which to lookup entry. */

+     CONST char *key;/* Key to use to find or create matching

+  * entry. */

+     int *newPtr;/* Store info here telling whether a new

+  * entry was created. */

+ {

+     Tcl_Obj *objPtr = (Tcl_Obj *) key;

+     register Tcl_HashEntry *hPtr;

+     register CONST char *p1, *p2;

+     register int l1, l2;

+     int index;

+ 

+     index = HashObj(objPtr) & tablePtr->mask;

+ 

+     /*

+      * Search all of the entries in this bucket.

+      */

+ 

+     for (hPtr = tablePtr->buckets[index]; hPtr != NULL;

+     hPtr = hPtr->nextPtr) {

+ /*

+  * If the object pointers are the same then they match.

+  */

+ if (objPtr == hPtr->key.objPtr) {

+     *newPtr = 0;

+     return hPtr;

+ }

+ 

+ p1 = Tcl_GetStringFromObj (objPtr, (int *) 0);

+ l1 = objPtr->length;

+ p2 = Tcl_GetStringFromObj (hPtr->key.objPtr, (int *) 0);

+ l2 = hPtr->key.objPtr->length;

+ 

+ /*

+  * If the lengths are different then they do not match.

+  */

+ if (l1 != l2) {

+     continue;

+ }

+ 

+ for (;; p1++, p2++, l1--) {

+     if (*p1 != *p2) {

+ break;

+     }

+     if (l1 == 0) {

+ *newPtr = 0;

+ return hPtr;

+     }

+ }

+     }

+ 

+     /*

+      * Entry not found.  Add a new one to the bucket.

+      */

+ 

+     *newPtr = 1;

+     hPtr = (Tcl_HashEntry *)

+ Tcl_Alloc((unsigned) sizeof(Tcl_HashEntry));

+     hPtr->tablePtr = tablePtr;

+     hPtr->bucketPtr = &(tablePtr->buckets[index]);

+     hPtr->nextPtr = *hPtr->bucketPtr;

+     hPtr->clientData = 0;

+     hPtr->key.objPtr = objPtr;

+     Tcl_IncrRefCount (objPtr);

+     *hPtr->bucketPtr = hPtr;

+     tablePtr->numEntries++;

+ 

+     /*

+      * If the table has exceeded a decent size, rebuild it with many

+      * more buckets.

+      */

+ 

+     if (tablePtr->numEntries >= tablePtr->rebuildSize) {

+ RebuildTable(tablePtr);

+     }

+     return hPtr;

+ }

+  

+ /*

+  *----------------------------------------------------------------------

+  *

   * RebuildTable --

   *

   *This procedure is invoked when the ratio of entries to hash

***************

*** 896,902 ****

      for (oldChainPtr = oldBuckets; oldSize > 0; oldSize--, oldChainPtr++) {

  for (hPtr = *oldChainPtr; hPtr != NULL; hPtr = *oldChainPtr) {

      *oldChainPtr = hPtr->nextPtr;

!     if (tablePtr->keyType == TCL_STRING_KEYS) {

  index = HashString(hPtr->key.string) & tablePtr->mask;

      } else if (tablePtr->keyType == TCL_ONE_WORD_KEYS) {

  index = RANDOM_INDEX(tablePtr, hPtr->key.oneWordValue);

--- 1139,1147 ----

      for (oldChainPtr = oldBuckets; oldSize > 0; oldSize--, oldChainPtr++) {

  for (hPtr = *oldChainPtr; hPtr != NULL; hPtr = *oldChainPtr) {

      *oldChainPtr = hPtr->nextPtr;

!     if (tablePtr->keyType == TCL_OBJ_KEYS) {

! index = HashObj(hPtr->key.objPtr) & tablePtr->mask;

!     } else if (tablePtr->keyType == TCL_STRING_KEYS) {

  index = HashString(hPtr->key.string) & tablePtr->mask;

      } else if (tablePtr->keyType == TCL_ONE_WORD_KEYS) {

  index = RANDOM_INDEX(tablePtr, hPtr->key.oneWordValue);


PatchFiles:

tclHash.c tcl.h


diff -c -r1.3 Hash.3

*** Hash.3      1999/12/21 23:57:33     1.3

--- Hash.3      2000/06/24 00:16:11

***************

*** 52,58 ****

  previous call to \fBTcl_InitHashTable\fR).

  .AP int keyType in

  Kind of keys to use for new hash table.  Must be either

! TCL_STRING_KEYS, TCL_ONE_WORD_KEYS, or an integer value

  greater than 1.

  .AP char *key in

  Key to use for probe into table.  Exact form depends on

--- 52,58 ----

  previous call to \fBTcl_InitHashTable\fR).

  .AP int keyType in

  Kind of keys to use for new hash table.  Must be either

! TCL_STRING_KEYS, TCL_OBJ_KEYS, TCL_ONE_WORD_KEYS, or an integer value

  greater than 1.

  .AP char *key in

  Key to use for probe into table.  Exact form depends on

***************

*** 107,119 ****

  Keys are null-terminated ASCII strings.

  They are passed to hashing routines using the address of the

  first character of the string.

  .IP \fBTCL_ONE_WORD_KEYS\fR 25

  Keys are single-word values;  they are passed to hashing routines

  and stored in hash table entries as ``char *'' values.

  The pointer value is the key;  it need not (and usually doesn't)

  actually point to a string.

  .IP \fIother\fR 25

! If \fIkeyType\fR is not TCL_STRING_KEYS or TCL_ONE_WORD_KEYS,

  then it must be an integer value greater than 1.

  In this case the keys will be arrays of ``int'' values, where

  \fIkeyType\fR gives the number of ints in each key.

--- 107,126 ----

  Keys are null-terminated ASCII strings.

  They are passed to hashing routines using the address of the

  first character of the string.

+ .IP \fBTCL_OBJ_KEYS\fR 25

+ Keys are Tcl_Obj *.  Hashing and comparison are done on the string

+ representation of the object.  The differences between this type and

+ TCL_STRING_KEYS are:  the key is not copied, instead the reference

+ count is incremented; and the extra information associated with the

+ Tcl_Obj * is used to optimize comparisons.  The string is only

+ compared if the two Tcl_Obj * are different and have the same length.

  .IP \fBTCL_ONE_WORD_KEYS\fR 25

  Keys are single-word values;  they are passed to hashing routines

  and stored in hash table entries as ``char *'' values.

  The pointer value is the key;  it need not (and usually doesn't)

  actually point to a string.

  .IP \fIother\fR 25

! If \fIkeyType\fR is not one of the above,

  then it must be an integer value greater than 1.

  In this case the keys will be arrays of ``int'' values, where

  \fIkeyType\fR gives the number of ints in each key.




Paul -- 

Thanks for the patch.  I have integrated it with the CVS repository, so it will go out with 8.4a2.  I have a couple of questions about it.  First, in a few places you use the following syntax to extract the pointer to the string rep of an object and the object's length:

p1 = Tcl_GetStringFromObj (objPtr, (int *) 0);
l1 = objPtr->length;

Why not use this instead:

p1 = Tcl_GetStringFromObj (objPtr, &l1);

Also, have you experimented at all with using this enhancement with any core bits, or is this mostly an enabling modification for other Feather work?  In particular, I'm curious whether this might be used to improve performance of Tcl arrays.  I look forward to hearing from you, and thanks.

   Eric Melski                            The Other Tcl Guy
   ericm at ajubasolutions.com            Ajuba Solutions

-- 06/23/2000 ericm
welch Closed ticket [219102ffff]. [History diff]2000-10-26 08:34:56
Log in (anonymous is fine) to add a comment.