Repository navigation
Conversation
Run the displacement regex in parse_asm only on lines that contain a '+' (the pattern needs one to match), and build the X/Y/Z register map once per call instead of once per operand. Output is unchanged. Parse time for the C test-suite listings: 58.1 ms -> 39.1 ms (1.49x). Co-Authored-By: Aiden <aiden@weco.ai>
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
TL;DR
parse_asminsrc/tiny8/assembler.pyfaster. Everyassemble()call and everycompile_c->assembletest uses it.+. It also builds the X/Y/Z register map once per call.pytest testspasses (342 passed).ruff checkandruff format --checkare clean.What & Why
Before,
parse_asmcalledre.subon every instruction line. Most lines have no+, so the regex can never match there. The code also built the{"X": 26, "Y": 28, "Z": 30}dict again for each X/Y/Z operand.Now the code checks
"+" in linefirst and uses the compiled pattern only when it is true. It builds the register map once, before the loop. The pattern needs a literal+, so skipping lines without one gives the same result.p.upper() in reg_mapis true for exactly the same operands asp.upper() in ("X", "Y", "Z"). The program, labels,pc_to_lineand errors stay identical.Results
Rows 1-3: best of 15 timing repeats, median of 3 alternating rounds. Row 4: median of 9 paired before/after timings. Same machine and Python 3.12:
compile_coutput, 154 lines)examples/*.asmprogramsY + n/Z+ndisplacement (400 lines)Why this is safe and easy to maintain
The change is 9 lines in one function and uses plain Python. The regex is still the same, and lines without a
+skip it because it cannot match them.tests/test_assembler.py,tests/test_compiler.py,tests/test_control_flow.pyandtests/test_cpu_edge_cases.pycover labels, comments, displacement operands and bad operands.Validation
pytest tests: 342 passed on this branch (342 passed onmain).ruff check src/tiny8/assembler.pyandruff format --check src/tiny8/assembler.py: clean.Before/after timing: the suite listings above took 58.1 ms -> 39.1 ms (32.7% faster, 1.49x) in the median of 3 separate measurements. The 3 measurements gave 0.679, 0.671 and 0.673 of the
maintime. An automated output check compared programs, labels,pc_to_line,source_linesand error messages withmain, and they were identical.Quick reproduction:
AI assistance
Code candidates were generated by Codex (gpt-6.1-sol) and tracked with Weco Observe. This PR uses the smallest readable candidate. The tests, timings and an automated output-equality check above were run on it.
Weco share link: https://dashboard.weco.ai/share/gLEKWXkUReYHkIjz2BXzPLKDXaEHdYK2