Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GDScript: Optimize match #78742

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

dalexeev
Copy link
Member

@dalexeev dalexeev commented Jun 27, 2023

Optimize match with jump tables whenever possible. I want to use two kinds of tables:

  1. OPCODE_JUMP_TABLE_RANGE. Can be used for continuous (or with a little sparsity) ranges of integers (such as enums). For example, from 0 to 10, or from 2 to 12. Instead of doing N comparisons (in the worst case), the jump address is calculated using the value, offset, and size.
  2. OPCODE_JUMP_TABLE_BSEARCH. Can be used to binary search in a constant array sorted at compilation. Suitable when the value is not an integer or the range is very sparse.

When using variable patterns, destructuring (and match guards in the future), the old bytecode will be preserved (or the branches will be divided into subgroups in which optimizations can be performed).

TODO

Parser
  • (Bonus) Allow pass in match.
  • Add warning REDUNDANT_PATTERN (multiple patterns with a wildcard pattern).
  • Add the ability to ignore warnings in match.
Analyzer
Codegen, VM, disassembler
  • Add opcodes OPCODE_JUMP_TABLE_RANGE and OPCODE_JUMP_TABLE_BSEARCH.
Compiler
  • Do not perform unnecessary operations if the type is known.
  • Optimize match with jump tables.
  • Remove unreachable branches?
Miscellaneous
  • Add tests.
  • Make benchmarks: PR vs master vs if-elif-else.

@MewPurPur
Copy link
Contributor

MewPurPur commented Mar 24, 2024

My project uses a lot of match in performance-critical sections where GDScript is a bottleneck, so it would be nice to know if I can expect a free speedup in them anytime soon. Is this PR abandoned?

@dalexeev
Copy link
Member Author

Is this PR abandoned?

As far as I remember, I stopped because of #58878. I implemented a workaround with SafeVariantSort, but this did not seem elegant enough to me, since I had to create a separate table variant_vector_constants. Also, the compiler part is unfinished, but I think it is doable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants