Performance tip: avoid "current action"

This might be something that everybody already knows, but I was quite surprised to find out just how many cycles that can be saved by getting rid of even a single reference to the “current action”. Something to try if your large Inform 7 project has performance problems.

I guess this is due to the usual slow memory management of Inform 7, but there seems to be something particularly demanding about the BlkValueCreateOnStack(0, STORED_ACTION_TY) that this translates to.

Often it can be easily avoided as well. I had used a lot of things like “when the action name part of current action is the removing it from action” where “when removing something from something” would work just as well.

After getting rid of all unnecessary “current actions” in the Counterfeit Monkey code, I saw a speed increase of almost 18 percent.

This is one of those (common) situations where if you do it once in a turn, the cost is trivial and nobody will notice.

If it’s part of a routine which is checked many times per turn, you start to worry.

But you are correct. “The action name part of the current action” goes through a lot of work to assemble the I6 variables “action”, “noun”, and “second” into a memory block, then pulls “action” back out. You can just check “if fooing” and it will test “action” directly.

(The I6 “noun” and “second” variables translate directly to I7 “noun” and “second noun”. The “action” variable has no equivalent, but you could easily make one if you felt like it.)

An i7 action variable is a great idea. Thanks!

The slowest current action-related rule in CM turned out to be “After deciding the scope of the player when the action name part of current action is the starting a conversation with it about action”. There was a huge reduction in cycles when I changed this to “After deciding the scope of the player when starting a conversation with an object about an object”.

As usual when I talk about performance gains, these are improvements that will probably not be noticeable during normal play, as responses generally feel nearly instant by now. But when I test it I use several scripts in a row that play through the entire game, and then it can mean four minutes of total test time rather than five, which is pretty nice.

2 Likes