From 0f3d210f05194fed979e83c3a134fccb9f3505cd Mon Sep 17 00:00:00 2001 From: "Ryan C. Gordon" Date: Wed, 17 Apr 2013 23:30:10 -0400 Subject: [PATCH] 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. --- mojoshader_preprocessor.c | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/mojoshader_preprocessor.c b/mojoshader_preprocessor.c index 5137bd4a..916243ae 100644 --- a/mojoshader_preprocessor.c +++ b/mojoshader_preprocessor.c @@ -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; @@ -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; @@ -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