Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Optimizations to preprocessor's find_define().
- don't check for __FILE__ and __LINE__ until after we've checked our hash,
  since this is the less likely case.
- Check against the hard-coded hash value of the strings "__FILE__" and
  "__LINE__" to avoid unnecessary strcmp() calls.

This dropped the profile of this function from 20% of our total CPU time to
 10%. Of that 10%, 70% is now the hashing function.
  • Loading branch information
icculus committed Apr 18, 2013
1 parent 3471e38 commit 0f3d210
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions mojoshader_preprocessor.c
Expand Up @@ -437,7 +437,22 @@ static int remove_define(Context *ctx, const char *sym)

static const Define *find_define(Context *ctx, const char *sym)
{
if ( (ctx->file_macro) && (strcmp(sym, "__FILE__") == 0) )
const uint8 hash = hash_define(sym);
Define *bucket = ctx->define_hashtable[hash];
while (bucket)
{
if (strcmp(bucket->identifier, sym) == 0)
return bucket;
bucket = bucket->next;
} // while

const uint8 filestrhash = 67;
assert(hash_define("__FILE__") == filestrhash);

const uint8 linestrhash = 75;
assert(hash_define("__LINE__") == linestrhash);

if ( (hash == filestrhash) && (ctx->file_macro) && (strcmp(sym, "__FILE__") == 0) )
{
Free(ctx, (char *) ctx->file_macro->definition);
const IncludeState *state = ctx->include_stack;
Expand All @@ -453,7 +468,7 @@ static const Define *find_define(Context *ctx, const char *sym)
return ctx->file_macro;
} // if

else if ( (ctx->line_macro) && (strcmp(sym, "__LINE__") == 0) )
else if ( (hash == linestrhash) && (ctx->line_macro) && (strcmp(sym, "__LINE__") == 0) )
{
Free(ctx, (char *) ctx->line_macro->definition);
const IncludeState *state = ctx->include_stack;
Expand All @@ -468,14 +483,6 @@ static const Define *find_define(Context *ctx, const char *sym)
return ctx->line_macro;
} // else

const uint8 hash = hash_define(sym);
Define *bucket = ctx->define_hashtable[hash];
while (bucket)
{
if (strcmp(bucket->identifier, sym) == 0)
return bucket;
bucket = bucket->next;
} // while
return NULL;
} // find_define

Expand Down

0 comments on commit 0f3d210

Please sign in to comment.