From ef8969d975a371aa0ebabf5a56915bd4dc6e13bd Mon Sep 17 00:00:00 2001 From: "zik.saleeba" Date: Mon, 5 Jan 2009 04:52:33 +0000 Subject: [PATCH] Added lexer token caching for improved performance git-svn-id: http://picoc.googlecode.com/svn/trunk@29 21eae674-98b7-11dd-bd71-f92a316d2d60 --- lex.c | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/lex.c b/lex.c index cff7111..39b8803 100644 --- a/lex.c +++ b/lex.c @@ -159,7 +159,7 @@ enum LexToken LexGetComment(struct LexState *Lexer, char NextChar, union AnyValu return LexGetToken(Lexer, Value); } -enum LexToken LexGetToken(struct LexState *Lexer, union AnyValue *Value) +enum LexToken LexGetTokenUncached(struct LexState *Lexer, union AnyValue *Value) { char ThisChar; char NextChar; @@ -215,6 +215,28 @@ enum LexToken LexGetToken(struct LexState *Lexer, union AnyValue *Value) return TokenEOF; } +enum LexToken LexGetToken(struct LexState *Lexer, union AnyValue *Value) +{ + static const char *CachedPos = NULL; + static union AnyValue CachedValue; + static struct LexState CachedLexer; + static enum LexToken CachedToken; + + if (Lexer->Pos == CachedPos) + { + *Value = CachedValue; + *Lexer = CachedLexer; + } + else + { + CachedToken = LexGetTokenUncached(Lexer, Value); + CachedLexer = *Lexer; + CachedValue = *Value; + } + + return CachedToken; +} + enum LexToken LexGetPlainToken(struct LexState *Lexer) { union AnyValue Value;