This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
libcpp - tiny speedup
- From: "Nicola Pero" <nicola dot pero at meta-innovation dot com>
- To: "gcc-patches at gnu dot org" <gcc-patches at gnu dot org>
- Date: Fri, 22 Oct 2010 20:17:44 +0200 (CEST)
- Subject: libcpp - tiny speedup
This patch inlines a libcpp hash function for speed. The speedup is to tiny (I had to
run 100m get_identifier() calls in a loop to measure an 8% reduction in time) that I'm
not sure it's worth the extra complication of having a static inline function in the headers.
Anyway since I've already done the work, here it is. If the libcpp maintainers
like it I can commit it. If not, I won't be offended - I was going to trash it anyway. ;-)
Thanks
PS: calc_hash was already automatically inlined by the compiler even if not declared
inline. I inlined the other call, which the compiler doesn't do automatically probably
because the function to inline is too big.
Index: symtab.c
===================================================================
--- symtab.c (revision 165822)
+++ symtab.c (working copy)
@@ -30,27 +30,12 @@
intrinsically how to calculate a hash value, and how to compare an
existing entry with a potential new one. */
-static unsigned int calc_hash (const unsigned char *, size_t);
static void ht_expand (hash_table *);
static double approx_sqrt (double);
/* A deleted entry. */
#define DELETED ((hashnode) -1)
-/* Calculate the hash of the string STR of length LEN. */
-
-static unsigned int
-calc_hash (const unsigned char *str, size_t len)
-{
- size_t n = len;
- unsigned int r = 0;
-
- while (n--)
- r = HT_HASHSTEP (r, *str++);
-
- return HT_HASHFINISH (r, len);
-}
-
/* Initialize an identifier hashtable. */
hash_table *
@@ -85,20 +70,7 @@
free (table);
}
-/* Returns the hash entry for the a STR of length LEN. If that string
- already exists in the table, returns the existing entry. If the
- identifier hasn't been seen before, and INSERT is CPP_NO_INSERT,
- returns NULL. Otherwise insert and returns a new entry. A new
- string is allocated. */
hashnode
-ht_lookup (hash_table *table, const unsigned char *str, size_t len,
- enum ht_lookup_option insert)
-{
- return ht_lookup_with_hash (table, str, len, calc_hash (str, len),
- insert);
-}
-
-hashnode
ht_lookup_with_hash (hash_table *table, const unsigned char *str,
size_t len, unsigned int hash,
enum ht_lookup_option insert)
Index: include/symtab.h
===================================================================
--- include/symtab.h (revision 165822)
+++ include/symtab.h (working copy)
@@ -76,14 +76,34 @@
/* Frees all memory associated with a hash table. */
extern void ht_destroy (hash_table *);
-extern hashnode ht_lookup (hash_table *, const unsigned char *,
- size_t, enum ht_lookup_option);
extern hashnode ht_lookup_with_hash (hash_table *, const unsigned char *,
size_t, unsigned int,
enum ht_lookup_option);
+
#define HT_HASHSTEP(r, c) ((r) * 67 + ((c) - 113));
#define HT_HASHFINISH(r, len) ((r) + (len))
+/* Returns the hash entry for the a STR of length LEN. If that string
+ already exists in the table, returns the existing entry. If the
+ identifier hasn't been seen before, and INSERT is CPP_NO_INSERT,
+ returns NULL. Otherwise insert and returns a new entry. A new
+ string is allocated. */
+static inline hashnode
+ht_lookup (hash_table *table, const unsigned char *str, size_t len,
+ enum ht_lookup_option insert)
+{
+ /* Calculate the hash of the string STR of length LEN. */
+ size_t n = len;
+ unsigned int r = 0;
+ const unsigned char *s = str;
+
+ while (n--)
+ r = HT_HASHSTEP (r, *s++);
+
+ return ht_lookup_with_hash (table, str, len, HT_HASHFINISH (r, len),
+ insert);
+}
+
/* For all nodes in TABLE, make a callback. The callback takes
TABLE->PFILE, the node, and a PTR, and the callback sequence stops
if the callback returns zero. */
Index: ChangeLog
===================================================================
--- ChangeLog (revision 165822)
+++ ChangeLog (working copy)
@@ -1,3 +1,9 @@
+2010-10-22 Nicola Pero <nicola.pero@meta-innovation.com>
+
+ * include/symtab.h (ht_lookup): Added as static inline.
+ * symtab.c (calc_hash): Removed.
+ (ht_lookup): Removed.
+
2010-10-19 Basile Starynkevitch <basile@starynkevitch.net>
* line-map.h (source_location): Remove obsolete comment
mentioning location_s.