Assorted token golf in rope logic #25

Merged
pyrex merged 7 commits from kistaro/chameleonic:rope_golf into main 2023-01-03 01:06:57 +00:00
Contributor

These should be semantically equivalent.

In converting level:tug_crate to level_tug_crate (which allows for a substantially more efficient and modestly shorter foreach to replace a for loop elsewhere in the code), I chose to define local self=level to save four cycles at the cost of three tokens; using the global level in all cases incurs an extra two global lookups (at two cycles each). Since this is called in a loop in code generally sus from a performance perspective I think that's the appropriate trade-off but you're welcome to overrule me on this.

Most other changes are "pure wins", improving both performance and tokens, primarily by finding terser ways to express conditions.

I tested this by playing a few levels but please scrutinize this closely to make sure my permuations are as equivalent as I think they are.

As is required in Pico-8 Golf, I'm sacrificing some readability in favor of terseness.

These should be semantically equivalent. In converting level:tug_crate to level_tug_crate (which allows for a substantially more efficient and modestly shorter foreach to replace a for loop elsewhere in the code), I chose to define `local self=level` to save four cycles at the cost of three tokens; using the global `level` in all cases incurs an extra two global lookups (at two cycles each). Since this is called in a loop in code generally sus from a performance perspective I think that's the appropriate trade-off but you're welcome to overrule me on this. Most other changes are "pure wins", improving both performance and tokens, primarily by finding terser ways to express conditions. I tested this by playing a few levels but please scrutinize this closely to make sure my permuations are as equivalent as I think they are. As is required in Pico-8 Golf, I'm sacrificing some readability in favor of terseness.
kistaro added 7 commits 2023-01-03 00:31:20 +00:00
The tug_crate conversion is for performance. `foreach(tbl, predefined_func)` is substantially faster than a standard `for` loop using the `all` iterator. However, if the function inside the foreach is defined inline, it's much slower due to closure-construction overhead (even though nothing is being closed over). Converting `tug_crate` to take a table as an argument allows foreach to feed right into it, and it also naturally suggests a rewrite a few lines down to get rid of duplicative listing of `mx0,my0,dmx,dmy`, saving several tokens.

I'm going to take a look at can_move to see if it's worth making iits mx0,my0,dmx,dmy arguments into a table as well.
this is approximately token-neutral but performance-saving. each function parameter makes its call cost worse. When can_move is called inside a loop, we already have a table and we unpack to call can_move; moving the unpack into can_move saves us marshalling cost. It requires us to construct a table in a different spot (where we were not previously doing so) but that spot is not in a loop.
Reorganizing conditionals saves tokens here.
This also saves a few tokens and cycles by turning level:tug_crate into a free function. It's not _pretty_ but it's the least bad option.
Removed unnecessary variable declarations and conditional cases by using an "assume, alternate, verify assumption" pattern and reusing ax0/ay0 when they would never be referenced again.
pyrex merged commit 4562128fa6 into main 2023-01-03 01:06:57 +00:00
Sign in to join this conversation.
No reviewers
No Label
No Milestone
No project
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: pyrex/chameleonic#25
No description provided.