Proposal: Per-scope comptime branch quota #7407
Labels
proposal
This issue suggests modifications. If it also has the "accepted" label then it is planned.
Milestone
Originally part of #5895, but was effectively a separate concept even before #7396 was accepted. Now splitting.
Currently, there is a single global branch quota per context that cannot be read. This has two problems.
Firstly, an irresponsible author of a long-running calculation may, instead of reasoning about its behaviour, choose to simply increment the quota in a loop, bypassing the purpose of a quota altogether:
Secondly, a responsible author who wishes to increase the quota must take a blind guess as to how many branches have already been taken:
Allowing the quota to be read solves the second problem but worsens the first; restricting
@setEvalBranchQuota
to global scope solves the first but worsens the second.I propose that we abolish
@setEvalBranchQuota
entirely, in favour of a new builtin:@addBranchQuota(comptime q: int) void
. This may only be called within linear scope. It imbues the scope with a branch quota of its own initialised toq
, counted separately from the global quota, and only valid within that scope. A branch first draws from its own scope's quota, then when that is exhausted (or if it does not exist) it draws from the enclosing linear scope's quota if applicable, then from the calling function's quota, and so on up the call stack until it reaches the context quota.The following actions incur a branch debt:
while
loopThese are the only actions which could potentially loop infinitely -- note especially that
for
loops are not included, as they iterate up to a fixed number of times. A loop repeat is debted to the scope containing the loop, and a function or inline expansion recursion to the scope of the topmost instance -- otherwise a badly behaving function could increment its own quota to infinity by some means. If the compiler decides to pre-compute values not explicitly marked ascomptime
, or inlines non-inline
functions or loops, the mechanism for bounding these actions is internal to the compiler and separate from the local quota -- it would be unfair and unpredictable to do otherwise.Increasing the context quota cannot be done from within the program -- it requires a command line option, say
-Dbranch-quota=2000
or something similar; the lack of distinction between top-level and lower level nonlinear scope makes it possible to indirectly loop-increment the surrounding quota otherwise.The text was updated successfully, but these errors were encountered: