Scribunto currently has the function mw.incrementExpensiveFunctionCount, which I gather is supposed to be called by a module function that is considered expensive. (I haven't seen it used that way yet.)
On English Wiktionary, this function is sometimes used to detect whether the expensive function limit has been reached, and thus (ostensibly) prevent the "too many expensive function calls" error message from being triggered:
if pcall(mw.incrementExpensiveFunctionCount) then -- code block in which an expensive function is called end
This is clumsy and, I just realized, doesn't really work: when the expensive function count is one less than the limit (499 on English Wiktionary), calling mw.incrementExpensiveFunctionCount brings it to the limit (500), and then using another expensive function in the if-block increments it past the limit (to 501) and triggers the error message. So there is probably going to be at least one error on a page that goes over the expensive function limit solely through the code block shown above.
And of course because the code includes two parser functions, the limit will be reached twice as fast.
As a replacement, it would be useful to have a function be added that returns the current expensive function count, perhaps named mw.getExpensiveFunctionCount. The code above could then be replaced with the following, which would actually reliably work.
if mw.getExpensiveFunctionCount() <= 500 - n then -- code block in which as many as n expensive functions might end up being be called end
Without such a function, the way to prevent the module errors is to create a list of the pages that are triggering the expensive function error and exclude them manually from running the portions of code that contain the expensive parser functions. If the pages later on end up running below the limit for whatever reason, there isn't a simple way to detect this and update the list.
I'm curious if this would be possible or desirable, or if there are reasons why a function for incrementing the expensive function count has been created but one for returning the count has not.