From 70be18427b069cae7ffcfb84811ae268fa18afce Mon Sep 17 00:00:00 2001 From: Bryan Bishop Date: Sat, 31 Aug 2013 11:04:27 -0500 Subject: [PATCH 1/5] don't call load_pokecrystal_macros 2000 times Also, don't call make_macro_table 2000 times by only calling it once and passing the result. --- preprocessor.py | 7 ++++--- prequeue.py | 10 ++++++++-- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/preprocessor.py b/preprocessor.py index f5dd205b2..aef1ff220 100644 --- a/preprocessor.py +++ b/preprocessor.py @@ -642,9 +642,8 @@ def read_line(l, macro_table): if comment: sys.stdout.write(comment) -def preprocess(macros, lines=None): +def preprocess(macro_table, lines=None): """Main entry point for the preprocessor.""" - macro_table = make_macro_table(macros) if not lines: # read each line from stdin @@ -658,4 +657,6 @@ def preprocess(macros, lines=None): # only run against stdin when not included as a module if __name__ == "__main__": - preprocess(load_pokecrystal_macros()) + macros = load_pokecrystal_macros() + macro_table = make_macro_table(macros) + preprocess(macro_table) diff --git a/prequeue.py b/prequeue.py index 2c8f4cf5a..58790f702 100644 --- a/prequeue.py +++ b/prequeue.py @@ -9,9 +9,15 @@ import os import sys import preprocessor -if __name__ == '__main__': +def main(): + macros = preprocessor.load_pokecrystal_macros() + macro_table = preprocessor.make_macro_table(macros) + for source in sys.argv[1:]: dest = os.path.splitext(source)[0] + '.tx' sys.stdin = open(source, 'r') sys.stdout = open(dest, 'w') - preprocessor.preprocess(preprocessor.load_pokecrystal_macros()) + preprocessor.preprocess(macro_table) + +if __name__ == '__main__': + main() From 7eaf5bf72656fa84d2aa94a0ea6e534120f40131 Mon Sep 17 00:00:00 2001 From: Bryan Bishop Date: Sat, 31 Aug 2013 11:07:49 -0500 Subject: [PATCH 2/5] reset stdout in prequeue.py near end Other output shouldn't be dumped into items/item_attributes.tx by default. --- prequeue.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/prequeue.py b/prequeue.py index 58790f702..6efc519d1 100644 --- a/prequeue.py +++ b/prequeue.py @@ -13,11 +13,16 @@ def main(): macros = preprocessor.load_pokecrystal_macros() macro_table = preprocessor.make_macro_table(macros) + stdout = sys.stdout + for source in sys.argv[1:]: dest = os.path.splitext(source)[0] + '.tx' sys.stdin = open(source, 'r') sys.stdout = open(dest, 'w') preprocessor.preprocess(macro_table) + # reset stdout + sys.stdout = stdout + if __name__ == '__main__': main() From 473bd192d9eb5767d5cda2ae21a0aa52552fabf6 Mon Sep 17 00:00:00 2001 From: Bryan Bishop Date: Sat, 31 Aug 2013 11:57:01 -0500 Subject: [PATCH 3/5] better read_line performance in preprocessor Jump out of read_line early if the line is an empty string or a newline. --- preprocessor.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/preprocessor.py b/preprocessor.py index aef1ff220..79fb9224f 100644 --- a/preprocessor.py +++ b/preprocessor.py @@ -427,12 +427,10 @@ def macro_test(asm, macro_table): token = extract_token(asm) # skip db and dw since rgbasm handles those and they aren't macros - if token not in ["db", "dw"]: - # check against all names - if token in macro_table: - return (macro_table[token], token) - - return (None, None) + if token is not None and token not in ["db", "dw"] and token in macro_table: + return (macro_table[token], token) + else: + return (None, None) def is_based_on(something, base): """ @@ -611,6 +609,10 @@ def macro_translator(macro, token, line, show_original_lines=False, do_macro_san def read_line(l, macro_table): """Preprocesses a given line of asm.""" + if l in ["\n", ""]: + sys.stdout.write(l) + return # jump out early + # strip comments from asm asm, comment = separate_comment(l) @@ -624,7 +626,7 @@ def read_line(l, macro_table): sys.stdout.write(asm) # ascii string macro preserves the bytes as ascii (skip the translator) - elif len(asm) > 6 and "ascii " == asm[:6] or "\tascii " == asm[:7]: + elif len(asm) > 6 and ("ascii " == asm[:6] or "\tascii " == asm[:7]): asm = asm.replace("ascii", "db", 1) sys.stdout.write(asm) @@ -640,7 +642,8 @@ def read_line(l, macro_table): else: sys.stdout.write(asm) - if comment: sys.stdout.write(comment) + if comment: + sys.stdout.write(comment) def preprocess(macro_table, lines=None): """Main entry point for the preprocessor.""" From a74462bc52ee3050e13680cd35156589e7f45555 Mon Sep 17 00:00:00 2001 From: Bryan Bishop Date: Sat, 31 Aug 2013 12:03:48 -0500 Subject: [PATCH 4/5] even better performance for read_line --- preprocessor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/preprocessor.py b/preprocessor.py index 79fb9224f..10e407380 100644 --- a/preprocessor.py +++ b/preprocessor.py @@ -609,7 +609,7 @@ def macro_translator(macro, token, line, show_original_lines=False, do_macro_san def read_line(l, macro_table): """Preprocesses a given line of asm.""" - if l in ["\n", ""]: + if l in ["\n", ""] or l[0] == ";": sys.stdout.write(l) return # jump out early From 6191559c539af5b4e6f05254d10c6f52993d0321 Mon Sep 17 00:00:00 2001 From: Bryan Bishop Date: Sat, 31 Aug 2013 12:12:09 -0500 Subject: [PATCH 5/5] give preprocessor.py a main() --- preprocessor.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/preprocessor.py b/preprocessor.py index 10e407380..a8be0ee76 100644 --- a/preprocessor.py +++ b/preprocessor.py @@ -658,8 +658,11 @@ def preprocess(macro_table, lines=None): for l in lines: read_line(l, macro_table) -# only run against stdin when not included as a module -if __name__ == "__main__": +def main(): macros = load_pokecrystal_macros() macro_table = make_macro_table(macros) preprocess(macro_table) + +# only run against stdin when not included as a module +if __name__ == "__main__": + main()