libtasn1-commit
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[SCM] GNU libtasn1 branch, master, updated. libtasn1_2_13-31-g80863f7


From: Nikos Mavrogiannopoulos
Subject: [SCM] GNU libtasn1 branch, master, updated. libtasn1_2_13-31-g80863f7
Date: Sun, 23 Sep 2012 10:56:16 +0000

This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "GNU libtasn1".

http://git.savannah.gnu.org/cgit/libtasn1.git/commit/?id=80863f75dfa06b3a256760da86850bcc720a5e61

The branch, master has been updated
       via  80863f75dfa06b3a256760da86850bcc720a5e61 (commit)
      from  086afe3375ee1fe3cda34c1b2302bdd6c95ebc0b (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
commit 80863f75dfa06b3a256760da86850bcc720a5e61
Author: Nikos Mavrogiannopoulos <address@hidden>
Date:   Sun Sep 23 12:55:40 2012 +0200

    improve performance by 50% by using hashes to compare strings.

-----------------------------------------------------------------------

Summary of changes:
 NEWS             |    1 +
 lib/Makefile.am  |    3 +-
 lib/gstr.c       |   11 ++-
 lib/gstr.h       |    2 +-
 lib/hash.c       |  235 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 lib/int.h        |    2 +
 lib/parser_aux.c |   33 ++++++--
 7 files changed, 273 insertions(+), 14 deletions(-)
 create mode 100644 lib/hash.c

diff --git a/NEWS b/NEWS
index e93c44b..aa30c23 100644
--- a/NEWS
+++ b/NEWS
@@ -3,6 +3,7 @@ GNU Libtasn1 NEWS                                     -*- 
outline -*-
 * Noteworthy changes in release 3.0 (unreleased) [stable]
 - Added asn1_read_node_value() to obtain a node's value.
 - Optimizations in internal tree allocation.
+- Optimizations in tree search.
 - libtasn1.h no longer exports internal structures.
 
 * Noteworthy changes in release 2.13 (2012-05-31) [stable]
diff --git a/lib/Makefile.am b/lib/Makefile.am
index 044f72d..03b49f5 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -44,7 +44,8 @@ libtasn1_la_SOURCES = \
        parser_aux.h    \
        structure.c     \
        structure.h     \
-       version.c
+       version.c       \
+       hash.c
 
 libtasn1_la_LIBADD = gllib/libgnu.la
 
diff --git a/lib/gstr.c b/lib/gstr.c
index 4785073..0558c77 100644
--- a/lib/gstr.c
+++ b/lib/gstr.c
@@ -48,7 +48,8 @@ _asn1_str_cat (char *dest, size_t dest_tot_size, const char 
*src)
     }
 }
 
-void
+/* Returns the bytes copied (not including the null terminator) */
+unsigned int
 _asn1_str_cpy (char *dest, size_t dest_tot_size, const char *src)
 {
   size_t str_size = strlen (src);
@@ -56,13 +57,17 @@ _asn1_str_cpy (char *dest, size_t dest_tot_size, const char 
*src)
   if (dest_tot_size > str_size)
     {
       strcpy (dest, src);
+      return str_size;
     }
   else
     {
       if (dest_tot_size > 0)
        {
-         memcpy (dest, src, dest_tot_size - 1);
-         dest[dest_tot_size - 1] = 0;
+         str_size = dest_tot_size - 1;
+         memcpy (dest, src, str_size);
+         dest[str_size] = 0;
+         return str_size;
        }
+      else return 0;
     }
 }
diff --git a/lib/gstr.h b/lib/gstr.h
index baaa6a0..672d59e 100644
--- a/lib/gstr.h
+++ b/lib/gstr.h
@@ -19,7 +19,7 @@
  * 02110-1301, USA
  */
 
-void _asn1_str_cpy (char *dest, size_t dest_tot_size, const char *src);
+unsigned int _asn1_str_cpy (char *dest, size_t dest_tot_size, const char *src);
 void _asn1_str_cat (char *dest, size_t dest_tot_size, const char *src);
 
 #define Estrcpy(x,y) _asn1_str_cpy(x,ASN1_MAX_ERROR_DESCRIPTION_SIZE,y)
diff --git a/lib/hash.c b/lib/hash.c
new file mode 100644
index 0000000..50da70c
--- /dev/null
+++ b/lib/hash.c
@@ -0,0 +1,235 @@
+/*
+ * Copyright (C) 2011-2012 Free Software Foundation, Inc.
+ *
+ * Author: Nikos Mavrogiannopoulos
+ *
+ * This file is part of GnuTLS.
+ *
+ * The GnuTLS is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; either version 3 of
+ * the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>
+ *
+ */
+
+#include <int.h>
+
+#define INIT_VAL 0x33a1
+/*
+-------------------------------------------------------------------------------
+lookup3.c, by Bob Jenkins, May 2006, Public Domain.
+
+These are functions for producing 32-bit hashes for hash table lookup.
+hashword(), hashlittle(), hashlittle2(), hashbig(), mix(), and final() 
+are externally useful functions.  Routines to test the hash are included 
+if SELF_TEST is defined.  You can use this free for any purpose.  It's in
+the public domain.  It has no warranty.
+
+You probably want to use hashlittle().  hashlittle() and hashbig()
+hash byte arrays.  hashlittle() is faster than hashbig() on
+little-endian machines.  Intel and AMD are little-endian machines.
+On second thought, you probably want hashlittle2(), which is identical to
+hashlittle() except it returns two 32-bit hashes for the price of one.  
+You could implement hashbig2() if you wanted but I haven't bothered here.
+
+If you want to find a hash of, say, exactly 7 integers, do
+  a = i1;  b = i2;  c = i3;
+  mix(a,b,c);
+  a += i4; b += i5; c += i6;
+  mix(a,b,c);
+  a += i7;
+  final(a,b,c);
+then use c as the hash value.  If you have a variable length array of
+4-byte integers to hash, use hashword().  If you have a byte array (like
+a character string), use hashlittle().  If you have several byte arrays, or
+a mix of things, see the comments above hashlittle().  
+
+Why is this so big?  I read 12 bytes at a time into 3 4-byte integers, 
+then mix those integers.  This is fast (you can do a lot more thorough
+mixing with 12*3 instructions on 3 integers than you can with 3 instructions
+on 1 byte), but shoehorning those bytes into integers efficiently is messy.
+-------------------------------------------------------------------------------
+*/
+#define rot(x,k) (((x)<<(k)) | ((x)>>(32-(k))))
+
+/*
+-------------------------------------------------------------------------------
+mix -- mix 3 32-bit values reversibly.
+
+This is reversible, so any information in (a,b,c) before mix() is
+still in (a,b,c) after mix().
+
+If four pairs of (a,b,c) inputs are run through mix(), or through
+mix() in reverse, there are at least 32 bits of the output that
+are sometimes the same for one pair and different for another pair.
+This was tested for:
+* pairs that differed by one bit, by two bits, in any combination
+  of top bits of (a,b,c), or in any combination of bottom bits of
+  (a,b,c).
+* "differ" is defined as +, -, ^, or ~^.  For + and -, I transformed
+  the output delta to a Gray code (a^(a>>1)) so a string of 1's (as
+  is commonly produced by subtraction) look like a single 1-bit
+  difference.
+* the base values were pseudorandom, all zero but one bit set, or 
+  all zero plus a counter that starts at zero.
+
+Some k values for my "a-=c; a^=rot(c,k); c+=b;" arrangement that
+satisfy this are
+    4  6  8 16 19  4
+    9 15  3 18 27 15
+   14  9  3  7 17  3
+Well, "9 15 3 18 27 15" didn't quite get 32 bits diffing
+for "differ" defined as + with a one-bit base and a two-bit delta.  I
+used http://burtleburtle.net/bob/hash/avalanche.html to choose 
+the operations, constants, and arrangements of the variables.
+
+This does not achieve avalanche.  There are input bits of (a,b,c)
+that fail to affect some output bits of (a,b,c), especially of a.  The
+most thoroughly mixed value is c, but it doesn't really even achieve
+avalanche in c.
+
+This allows some parallelism.  Read-after-writes are good at doubling
+the number of bits affected, so the goal of mixing pulls in the opposite
+direction as the goal of parallelism.  I did what I could.  Rotates
+seem to cost as much as shifts on every machine I could lay my hands
+on, and rotates are much kinder to the top and bottom bits, so I used
+rotates.
+-------------------------------------------------------------------------------
+*/
+#define mix(a,b,c) \
+{ \
+  a -= c;  a ^= rot(c, 4);  c += b; \
+  b -= a;  b ^= rot(a, 6);  a += c; \
+  c -= b;  c ^= rot(b, 8);  b += a; \
+  a -= c;  a ^= rot(c,16);  c += b; \
+  b -= a;  b ^= rot(a,19);  a += c; \
+  c -= b;  c ^= rot(b, 4);  b += a; \
+}
+
+/*
+-------------------------------------------------------------------------------
+final -- final mixing of 3 32-bit values (a,b,c) into c
+
+Pairs of (a,b,c) values differing in only a few bits will usually
+produce values of c that look totally different.  This was tested for
+* pairs that differed by one bit, by two bits, in any combination
+  of top bits of (a,b,c), or in any combination of bottom bits of
+  (a,b,c).
+* "differ" is defined as +, -, ^, or ~^.  For + and -, I transformed
+  the output delta to a Gray code (a^(a>>1)) so a string of 1's (as
+  is commonly produced by subtraction) look like a single 1-bit
+  difference.
+* the base values were pseudorandom, all zero but one bit set, or 
+  all zero plus a counter that starts at zero.
+
+These constants passed:
+ 14 11 25 16 4 14 24
+ 12 14 25 16 4 14 24
+and these came close:
+  4  8 15 26 3 22 24
+ 10  8 15 26 3 22 24
+ 11  8 15 26 3 22 24
+-------------------------------------------------------------------------------
+*/
+#define final(a,b,c) \
+{ \
+  c ^= b; c -= rot(b,14); \
+  a ^= c; a -= rot(c,11); \
+  b ^= a; b -= rot(a,25); \
+  c ^= b; c -= rot(b,16); \
+  a ^= c; a -= rot(c,4);  \
+  b ^= a; b -= rot(a,14); \
+  c ^= b; c -= rot(b,24); \
+}
+
+
+/*
+-------------------------------------------------------------------------------
+hashlittle() -- hash a variable-length key into a 32-bit value
+  k       : the key (the unaligned variable-length array of bytes)
+  length  : the length of the key, counting by bytes
+  initval : can be any 4-byte value
+Returns a 32-bit value.  Every bit of the key affects every bit of
+the return value.  Two keys differing by one or two bits will have
+totally different hash values.
+
+The best hash table sizes are powers of 2.  There is no need to do
+mod a prime (mod is sooo slow!).  If you need less than 32 bits,
+use a bitmask.  For example, if you need only 10 bits, do
+  h = (h & hashmask(10));
+In which case, the hash table should have hashsize(10) elements.
+
+If you are hashing n strings (uint8_t **)k, do it like this:
+  for (i=0, h=0; i<n; ++i) h = hashlittle( k[i], len[i], h);
+
+By Bob Jenkins, 2006.  address@hidden  You may use this
+code any way you wish, private, educational, or commercial.  It's free.
+
+Use for hash table lookup, or anything where one collision in 2^^32 is
+acceptable.  Do NOT use for cryptographic purposes.
+-------------------------------------------------------------------------------
+*/
+
+uint32_t _asn1_bhash( const void *key, size_t length)
+{
+  uint32_t a,b,c;                                          /* internal state */
+  const uint8_t *k;
+
+  /* Set up the internal state */
+  a = b = c = 0xdeadbeef + ((uint32_t)length) + INIT_VAL;
+
+  k = (const uint8_t *)key;
+
+  /*--------------- all but the last block: affect some 32 bits of (a,b,c) */
+  while (length > 12)
+  {
+    a += k[0];
+    a += ((uint32_t)k[1])<<8;
+    a += ((uint32_t)k[2])<<16;
+    a += ((uint32_t)k[3])<<24;
+    b += k[4];
+    b += ((uint32_t)k[5])<<8;
+    b += ((uint32_t)k[6])<<16;
+    b += ((uint32_t)k[7])<<24;
+    c += k[8];
+    c += ((uint32_t)k[9])<<8;
+    c += ((uint32_t)k[10])<<16;
+    c += ((uint32_t)k[11])<<24;
+    mix(a,b,c);
+    length -= 12;
+    k += 12;
+  }
+
+  /*-------------------------------- last block: affect all 32 bits of (c) */
+  switch(length)                   /* all the case statements fall through */
+  {
+    case 12: c+=((uint32_t)k[11])<<24;
+    case 11: c+=((uint32_t)k[10])<<16;
+    case 10: c+=((uint32_t)k[9])<<8;
+    case 9 : c+=k[8];
+    case 8 : b+=((uint32_t)k[7])<<24;
+    case 7 : b+=((uint32_t)k[6])<<16;
+    case 6 : b+=((uint32_t)k[5])<<8;
+    case 5 : b+=k[4];
+    case 4 : a+=((uint32_t)k[3])<<24;
+    case 3 : a+=((uint32_t)k[2])<<16;
+    case 2 : a+=((uint32_t)k[1])<<8;
+    case 1 : a+=k[0];
+             break;
+    case 0 : return c;
+  }
+
+  final(a,b,c);
+  return c;
+}
+
+
+
diff --git a/lib/int.h b/lib/int.h
index fad4dbe..9e6bc89 100644
--- a/lib/int.h
+++ b/lib/int.h
@@ -47,6 +47,7 @@ struct node_asn_struct
 {
   /* public fields: */
   char name[ASN1_MAX_NAME_SIZE+1];                     /* Node name */
+  unsigned int name_hash;
   unsigned int type;           /* Node type */
   unsigned char *value;                /* Node value */
   int value_len;
@@ -63,6 +64,7 @@ struct node_asn_struct
 #define _asn1_strcmp(a,b) strcmp((const char *)a, (const char *)b)
 #define _asn1_strcpy(a,b) strcpy((char *)a, (const char *)b)
 #define _asn1_strcat(a,b) strcat((char *)a, (const char *)b)
+uint32_t _asn1_bhash( const void *key, size_t length);
 
 #define MAX_LOG_SIZE 1024      /* maximum number of characters of a log 
message */
 
diff --git a/lib/parser_aux.c b/lib/parser_aux.c
index 7b89355..323a5ea 100644
--- a/lib/parser_aux.c
+++ b/lib/parser_aux.c
@@ -95,6 +95,8 @@ asn1_find_node (ASN1_TYPE pointer, const char *name)
   ASN1_TYPE p;
   char *n_end, n[ASN1_MAX_NAME_SIZE + 1];
   const char *n_start;
+  unsigned int nsize;
+  unsigned int nhash;
 
   if (pointer == NULL)
     return NULL;
@@ -110,20 +112,25 @@ asn1_find_node (ASN1_TYPE pointer, const char *name)
       n_end = strchr (n_start, '.');   /* search the first dot */
       if (n_end)
        {
-         memcpy (n, n_start, n_end - n_start);
-         n[n_end - n_start] = 0;
+         nsize = n_end - n_start;
+         memcpy (n, n_start, nsize);
+         n[nsize] = 0;
          n_start = n_end;
          n_start++;
+
+          nhash = _asn1_bhash(n, nsize);
        }
       else
        {
-         _asn1_str_cpy (n, sizeof (n), n_start);
+         nsize = _asn1_str_cpy (n, sizeof (n), n_start);
+          nhash = _asn1_bhash(n, nsize);
+
          n_start = NULL;
        }
 
       while (p)
        {
-         if ((p->name) && (!strcmp (p->name, n)))
+         if ((p->name) && nhash == p->name_hash && (!strcmp (p->name, n)))
            break;
          else
            p = p->right;
@@ -143,14 +150,18 @@ asn1_find_node (ASN1_TYPE pointer, const char *name)
       n_end = strchr (n_start, '.');   /* search the next dot */
       if (n_end)
        {
-         memcpy (n, n_start, n_end - n_start);
-         n[n_end - n_start] = 0;
+         nsize = n_end - n_start;
+         memcpy (n, n_start, nsize);
+         n[nsize] = 0;
          n_start = n_end;
          n_start++;
+
+          nhash = _asn1_bhash(n, nsize);
        }
       else
        {
-         _asn1_str_cpy (n, sizeof (n), n_start);
+         nsize = _asn1_str_cpy (n, sizeof (n), n_start);
+          nhash = _asn1_bhash(n, nsize);
          n_start = NULL;
        }
 
@@ -172,7 +183,7 @@ asn1_find_node (ASN1_TYPE pointer, const char *name)
        {                       /* no "?LAST" */
          while (p)
            {
-             if (!strcmp (p->name, n))
+             if (p->name_hash == nhash && !strcmp (p->name, n))
                break;
              else
                p = p->right;
@@ -346,16 +357,20 @@ _asn1_append_value (ASN1_TYPE node, const void *value, 
unsigned int len)
 ASN1_TYPE
 _asn1_set_name (ASN1_TYPE node, const char *name)
 {
+unsigned int nsize;
+
   if (node == NULL)
     return node;
 
   if (name == NULL)
     {
       node->name[0] = 0;
+      node->name_hash = _asn1_bhash(node->name, 0);
       return node;
     }
 
-  _asn1_str_cpy (node->name, sizeof (node->name), name);
+  nsize = _asn1_str_cpy (node->name, sizeof (node->name), name);
+  node->name_hash = _asn1_bhash(node->name, nsize);
 
   return node;
 }


hooks/post-receive
-- 
GNU libtasn1



reply via email to

[Prev in Thread] Current Thread [Next in Thread]