Skip to content

Commit

Permalink
TOK_HASH_ optimization for gcc
Browse files Browse the repository at this point in the history
    tccboot kernel compilation with tcc before and after
      38402 idents, 6737645 lines, 196142735 bytes, 7.285 s, 924836 lines/s, 26.9 MB/s
      38402 idents, 6737645 lines, 196142736 bytes, 7.145 s, 942966 lines/s, 27.5 MB/s
  • Loading branch information
seyko2 committed Apr 23, 2016
1 parent 6ef9915 commit b89f0d6
Showing 1 changed file with 22 additions and 12 deletions.
34 changes: 22 additions & 12 deletions tccpp.c
Expand Up @@ -409,9 +409,15 @@ static TokenSym *tok_alloc_new(TokenSym **pts, const char *str, int len)
return ts;
}

#define TOK_HASH_INIT 1
#define TOK_HASH_FUNC(h, c) ((h) + ((h) << 5) + ((h) >> 27) + (c))

#if __TINYC__
#define TOK_HASH_INIT(h) h = 1
#define TOK_HASH_FUNC(h, c) h += (h << 5) + (h >> 27) + c
#define TOK_HASH_FINISH(h)
#else
#define TOK_HASH_INIT(h) { unsigned int _h = 1
#define TOK_HASH_FUNC(h, c) _h += (_h << 5) + (_h >> 27) + c
#define TOK_HASH_FINISH(h) h = _h; }
#endif

/* find a token and add it if not found */
ST_FUNC TokenSym *tok_alloc(const char *str, int len)
Expand All @@ -420,9 +426,10 @@ ST_FUNC TokenSym *tok_alloc(const char *str, int len)
int i;
unsigned int h;

h = TOK_HASH_INIT;
TOK_HASH_INIT(h);
for(i=0;i<len;i++)
h = TOK_HASH_FUNC(h, ((unsigned char *)str)[i]);
TOK_HASH_FUNC(h,((unsigned char *)str)[i]);
TOK_HASH_FINISH(h);
h &= (TOK_HASH_SIZE - 1);

pts = &hash_ident[h];
Expand All @@ -448,9 +455,10 @@ ST_FUNC TokenSym** symtab_tok_find(const char *str, int len)
int i;
unsigned int h;

h = TOK_HASH_INIT;
TOK_HASH_INIT(h);
for(i=0;i<len;i++)
h = TOK_HASH_FUNC(h, ((unsigned char *)str)[i]);
TOK_HASH_FUNC(h,((unsigned char *)str)[i]);
TOK_HASH_FINISH(h);
h &= (TOK_HASH_SIZE - 1);

pts = &hash_ident[h];
Expand Down Expand Up @@ -1682,12 +1690,13 @@ static inline int hash_cached_include(const char *filename)
const unsigned char *s;
unsigned int h;

h = TOK_HASH_INIT;
TOK_HASH_INIT(h);
s = (unsigned char *) filename;
while (*s) {
h = TOK_HASH_FUNC(h, *s);
TOK_HASH_FUNC(h,*s);
s++;
}
TOK_HASH_FINISH(h);
h &= (CACHED_INCLUDES_HASH_SIZE - 1);
return h;
}
Expand Down Expand Up @@ -2810,10 +2819,11 @@ static inline void next_nomacro1(void)
#endif
parse_ident_fast:
p1 = p;
h = TOK_HASH_INIT;
h = TOK_HASH_FUNC(h, c);
TOK_HASH_INIT(h);
TOK_HASH_FUNC(h,c);
while (c = *++p, isidnum_table[c - CH_EOF] & (IS_ID|IS_NUM))
h = TOK_HASH_FUNC(h, c);
TOK_HASH_FUNC(h,c);
TOK_HASH_FINISH(h);
len = p - p1;
if (c != '\\') {
TokenSym **pts;
Expand Down

0 comments on commit b89f0d6

Please sign in to comment.