From 8c35a8ffabea1c989d9f449bff053081fdc987e1 Mon Sep 17 00:00:00 2001 From: Scott Wolchok Date: Thu, 7 Jan 2016 15:02:38 -0800 Subject: [PATCH] Cache scalar regexes This improves performance on the test.yaml attached to #158 by about 35% on my machine (0.39s -> 0.25s), as measured by `time build/util/parse < test.yaml > /dev/null`. --- src/exp.h | 9 +++++++++ src/scantoken.cpp | 4 ++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/exp.h b/src/exp.h index f248802..ba82874 100644 --- a/src/exp.h +++ b/src/exp.h @@ -165,6 +165,15 @@ inline const RegEx& EndScalarInFlow() { return e; } +inline const RegEx& ScanScalarEndInFlow() { + static const RegEx e = (EndScalarInFlow() || (BlankOrBreak() + Comment())); + return e; +} + +inline const RegEx& ScanScalarEnd() { + static const RegEx e = EndScalar() || (BlankOrBreak() + Comment()); + return e; +} inline const RegEx& EscSingleQuote() { static const RegEx e = RegEx("\'\'"); return e; diff --git a/src/scantoken.cpp b/src/scantoken.cpp index 180ad00..79e7682 100644 --- a/src/scantoken.cpp +++ b/src/scantoken.cpp @@ -297,8 +297,8 @@ void Scanner::ScanPlainScalar() { // set up the scanning parameters ScanScalarParams params; - params.end = (InFlowContext() ? Exp::EndScalarInFlow() : Exp::EndScalar()) || - (Exp::BlankOrBreak() + Exp::Comment()); + params.end = + (InFlowContext() ? Exp::ScanScalarEndInFlow() : Exp::ScanScalarEnd()); params.eatEnd = false; params.indent = (InFlowContext() ? 0 : GetTopIndent() + 1); params.fold = FOLD_FLOW;