Skip to content

Commit

Permalink
Precompile regular expressions
Browse files Browse the repository at this point in the history
This change boost performance at scale by pre-compiling regular
expressions in the global space, then reusing them many times within
functions.

This (while not the desired intent of the author) will boost
performance when parsing input.

Signed-off-by: Enji Cooper <[email protected]>
  • Loading branch information
ngie-eign committed Mar 27, 2020
1 parent 1b545a4 commit 23874cf
Showing 1 changed file with 34 additions and 24 deletions.
58 changes: 34 additions & 24 deletions event_rpcgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

CPPCOMMENT_RE = re.compile(r"\/\/.*$")
NONIDENT_RE = re.compile(r"\W")
PREPROCESSOR_DEF_RE = re.compile(r"^#define")
STRUCT_REF_RE = re.compile(r"^struct\[(?P<name>[a-zA-Z_][a-zA-Z0-9_]*)\]$")
STRUCT_DEF_RE = re.compile(r"^struct +[a-zA-Z_][a-zA-Z0-9_]* *{$")
WHITESPACE_RE = re.compile(r"\s+")
Expand Down Expand Up @@ -1309,8 +1310,8 @@ def NormalizeLine(line):
return line


NAME_RE = re.compile(r"(?P<name>[^\[\]]+)(\[(?P<fixed_length>.*)\])?")
TAG_NUMBER_RE = re.compile(r"(0x)?\d+", re.I)
ENTRY_NAME_RE = re.compile(r"(?P<name>[^\[\]]+)(\[(?P<fixed_length>.*)\])?")
ENTRY_TAG_NUMBER_RE = re.compile(r"(0x)?\d+", re.I)


def ProcessOneEntry(factory, newstruct, entry):
Expand Down Expand Up @@ -1338,15 +1339,13 @@ def ProcessOneEntry(factory, newstruct, entry):
continue

if not name:
res = re.match(r'^([^\[\]]+)(\[.*\])?$', token)
res = ENTRY_NAME_RE.match(token)
if not res:
raise RpcGenError(
'Cannot parse name: \"%s\" '
'around line %d' % (entry, LINE_COUNT))
name = res.group(1)
fixed_length = res.group(2)
if fixed_length:
fixed_length = fixed_length[1:-1]
name = res.group("name")
fixed_length = res.group("fixed_length")
continue

if not separator:
Expand All @@ -1358,7 +1357,7 @@ def ProcessOneEntry(factory, newstruct, entry):

if not tag_set:
tag_set = 1
if not re.match(r'^(0x)?[0-9]+$', token):
if not ENTRY_TAG_NUMBER_RE.match(token):
raise RpcGenError('Expected tag number: \"%s\"' % entry)
tag = int(token, 0)
continue
Expand Down Expand Up @@ -1438,6 +1437,23 @@ def ProcessStruct(factory, data):
structs.append(newstruct)
return structs


C_COMMENT_START = "/*"
C_COMMENT_END = "*/"

C_COMMENT_START_RE = re.compile(re.escape(C_COMMENT_START))
C_COMMENT_END_RE = re.compile(re.escape(C_COMMENT_END))

C_COMMENT_START_SUB_RE = re.compile(r"%s.*$" % (re.escape(C_COMMENT_START)))
C_COMMENT_END_SUB_RE = re.compile(r"%s.*$" % (re.escape(C_COMMENT_END)))

C_MULTILINE_COMMENT_SUB_RE = re.compile(
r"%s.*?%s" % (re.escape(C_COMMENT_START), re.escape(C_COMMENT_END))
)
CPP_CONDITIONAL_BLOCK_RE = re.compile(r"#(if( |def)|endif)")
INCLUDE_RE = re.compile(r'#include (".+"|<.+>)')


def GetNextStruct(filep):
global CPP_DIRECT
global LINE_COUNT
Expand All @@ -1455,38 +1471,32 @@ def GetNextStruct(filep):
LINE_COUNT += 1
line = line[:-1]

if not have_c_comment and re.search(r'/\*', line):
if re.search(r'/\*.*?\*/', line):
line = re.sub(r'/\*.*?\*/', '', line)
if not have_c_comment and C_COMMENT_START_RE.search(line):
if C_MULTILINE_COMMENT_SUB_RE.search(line):
line = C_MULTILINE_COMMENT_SUB_RE.sub("", line)
else:
line = re.sub(r'/\*.*$', '', line)
line = C_COMMENT_START_SUB_RE.sub("", line)
have_c_comment = True

if have_c_comment:
if not re.search(r'\*/', line):
if not C_COMMENT_END_RE.search(line):
continue
have_c_comment = False
line = re.sub(r'^.*\*/', '', line)
line = C_COMMENT_END_SUB_RE.sub("", line)

line = NormalizeLine(line)

if not line:
continue

if not got_struct:
if re.match(r'#include ["<].*[>"]', line):
if INCLUDE_RE.match(line):
CPP_DIRECT.append(line)
continue

if re.match(r'^#(if( |def)|endif)', line):
elif CPP_CONDITIONAL_BLOCK_RE.match(line):
CPP_DIRECT.append(line)
continue

if re.match(r'^#define', line):
elif PREPROCESSOR_DEF_RE.match(line):
HEADER_DIRECT.append(line)
continue

if not STRUCT_DEF_RE.match(line):
elif not STRUCT_DEF_RE.match(line):
raise RpcGenError('Missing struct on line %d: %s'
% (LINE_COUNT, line))
else:
Expand Down

0 comments on commit 23874cf

Please sign in to comment.