What are the boundaries of the eval branch quota?

I’m trying to figure out the best practices with the branch quota. I’ve been unable to determine when the quota starts fresh, and when it’s inherited from the caller. Is it per-scope? Per-function? Per comptime context? Per file? Per compilation unit? I don’t understand where the boundary is. And if I have something that I know will exceed the 32-bit limit, is there any way to get around that?

I saw this thread already, but it really didn’t answer my question. Perhaps it’s my ADHD, but I couldn’t see anything that explained what I wanted to know there.

I’m really sorry for causing confusion. What I meant is that the evaluation semantics of branch quota already have accepted proposals for modification, so discussing its ‘best practices’ now is a bit premature. In a sense, since branch quota is introduced to solve the halting problem, trying to calculate the optimal branch quota itself is also a halting problem.

According to #21324 and #22410, there isn’t a satisfactory decision on the design yet.

I’m just trying to figure out where the boundaries are. For example, if I call @setEvalBranchQuota inside of a comptime function, does it set the quota fresh for that function? Does it start at 0 within that function? Is the quota for the scope that setEvalBranchQuota is called in? When/where does the quota reset/start fresh?

These two issues discussed them, which means the design here hasn’t actually been finalized.

I’m not concerned about that. I’m concerned about how it currently works right now.

1 Like

Pay attention to branch_quota, branch_count, and quota_request in Sema.zig:

branch_quota refers to the actual limit in the current analysis process. quota_request is used to cache the actual limit of a compile-time function.

In the current implementation, whenever it encounters a @setEvalBranchQuota and the set value is larger than the current branch_quota, the branch_quota will be expanded to the bigger quota.

https://codeberg.org/ziglang/zig/src/commit/13ee140439e8a175dc80983b0257793fa9c2cb0e/src/Sema.zig#L4916-L4922

The results of compile-time functions will be cached, so whether this compile-time execution generates a bigger quota and how much quota is used will also be cached.

https://codeberg.org/ziglang/zig/src/commit/13ee140439e8a175dc80983b0257793fa9c2cb0e/src/Sema.zig#L7240-L7242

quota_request is used to record this cache.

https://codeberg.org/ziglang/zig/src/commit/13ee140439e8a175dc80983b0257793fa9c2cb0e/src/Sema.zig#L7370-L7372

https://codeberg.org/ziglang/zig/src/commit/13ee140439e8a175dc80983b0257793fa9c2cb0e/src/Sema.zig#L7401-L7412

But where is the boundary? When does a quota begin counting from 0?

Each Sema’s quota starts at 0.

A new Sema is generated for each global scope declaration when there is an analysis need. The container’s memory layout is resolved lazily, so a new Sema is also generated for that.

Thank you. I think that answers my question. I’m just going to set the quota to the maximum when I’m not sure what to set it to but know that it won’t hit the limit.

It looks to me that the scope of the quota is the innermost runtime function call.