From patchwork Fri Oct 22 18:17:44 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicola Pero X-Patchwork-Id: 68895 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) by ozlabs.org (Postfix) with SMTP id DC93CB70A9 for ; Sat, 23 Oct 2010 05:18:14 +1100 (EST) Received: (qmail 32079 invoked by alias); 22 Oct 2010 18:18:11 -0000 Received: (qmail 32061 invoked by uid 22791); 22 Oct 2010 18:18:09 -0000 X-SWARE-Spam-Status: No, hits=-1.3 required=5.0 tests=AWL, BAYES_00, T_RP_MATCHES_RCVD X-Spam-Check-By: sourceware.org Received: from fencepost.gnu.org (HELO fencepost.gnu.org) (140.186.70.10) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Fri, 22 Oct 2010 18:18:03 +0000 Received: from eggs.gnu.org ([140.186.70.92]:39478) by fencepost.gnu.org with esmtps (TLS-1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.69) (envelope-from ) id 1P9MBl-0000sQ-SZ for gcc-patches@gnu.org; Fri, 22 Oct 2010 14:18:01 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1P9MBX-00047Y-NC for gcc-patches@gnu.org; Fri, 22 Oct 2010 14:18:01 -0400 Received: from smtp181.iad.emailsrvr.com ([207.97.245.181]:51512) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1P9MBX-00046S-Kf for gcc-patches@gnu.org; Fri, 22 Oct 2010 14:17:47 -0400 Received: from localhost (localhost.localdomain [127.0.0.1]) by smtp48.relay.iad1a.emailsrvr.com (SMTP Server) with ESMTP id 0B769168596 for ; Fri, 22 Oct 2010 14:17:45 -0400 (EDT) Received: from dynamic1.wm-web.iad.mlsrvr.com (dynamic1.wm-web.iad1a.rsapps.net [192.168.2.150]) by smtp48.relay.iad1a.emailsrvr.com (SMTP Server) with ESMTP id E842516857D for ; Fri, 22 Oct 2010 14:17:44 -0400 (EDT) Received: from meta-innovation.com (localhost [127.0.0.1]) by dynamic1.wm-web.iad.mlsrvr.com (Postfix) with ESMTP id D8A4DC98072 for ; Fri, 22 Oct 2010 14:17:44 -0400 (EDT) Received: by www2.webmail.us (Authenticated sender: nicola.pero@meta-innovation.com, from: nicola.pero@meta-innovation.com) with HTTP; Fri, 22 Oct 2010 20:17:44 +0200 (CEST) Date: Fri, 22 Oct 2010 20:17:44 +0200 (CEST) Subject: libcpp - tiny speedup From: "Nicola Pero" To: "gcc-patches@gnu.org" MIME-Version: 1.0 X-Type: plain Message-ID: <1287771464.882432386@192.168.2.230> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 3) X-IsSubscribed: yes Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org 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 + + * include/symtab.h (ht_lookup): Added as static inline. + * symtab.c (calc_hash): Removed. + (ht_lookup): Removed. + 2010-10-19 Basile Starynkevitch * line-map.h (source_location): Remove obsolete comment mentioning location_s.