# What are the boundaries of the eval branch quota?

**URL:** <https://ziggit.dev/t/what-are-the-boundaries-of-the-eval-branch-quota/17976>\
**Category:** Explain\
**Tags:** language\
**Created:** [October 15, 2026, 2:46pm UTC](https://ziggit.dev/t/what-are-the-boundaries-of-the-eval-branch-quota/17976 "2026-10-15T14:46:50Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![nullsurplus](https://ziggit.dev/user_avatar/ziggit.dev/nullsurplus/32/10353_2.png) [@nullsurplus](https://ziggit.dev/u/nullsurplus)\
**Post date:** [October 15, 2026, 2:46pm UTC](https://ziggit.dev/t/what-are-the-boundaries-of-the-eval-branch-quota/17976/1 "2026-10-15T14:46:50Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![npc1054657282](https://ziggit.dev/user_avatar/ziggit.dev/npc1054657282/32/5790_2.png) [@npc1054657282](https://ziggit.dev/u/npc1054657282)\
**Post date:** [October 15, 2026, 3:30pm UTC](https://ziggit.dev/t/what-are-the-boundaries-of-the-eval-branch-quota/17976/2 "2026-10-15T15:30:26Z")

</div>

> <https://github.com/ziglang/zig/issues/16983>
>
> Brought up a couple weeks back by @InKryption on the Discord - I couldn't find a… direct response, nor an issue/proposal form of this.
> 
> Short version: If the eval branch quota has only been (designed and) implemented to guard against infinite comptime execution, then there is no reason for known-finite operations (\`for\` loops and SIMD) to consume it.
> (Currently \`for\` loops consume it, but SIMD builtins don't, which seems inconsistent.)
> 
> \<details\>\<summary\>original message:\</summary\>
> \<p\>
> 
> \> An alternative perspective on this is that both for loops and simd operations operate on a bounded range of memory (as in, confined to the address space), meaning that any "backwards branches" they would produce should be guaranteed to be finite in number (unbounded for loops are a compiler error after all).
> \> Therefore, unlike while loops or function calls which could theoretically loop indefinitely if not constrained by the eval branch quota, maybe it would be safe to define both for loops and simd operations as not consuming eval branch quota?
> Correct me if I'm making any invalid assumptions here
> 
> \</p\>
> \</details\>
> 
> (Created now because it would offer a direct solution to https://github.com/ziglang/zig/issues/16980 .)

---

<div class="post-metadata">

**Author:** ![nullsurplus](https://ziggit.dev/user_avatar/ziggit.dev/nullsurplus/32/10353_2.png) [@nullsurplus](https://ziggit.dev/u/nullsurplus)\
**Post date:** [October 15, 2026, 3:40pm UTC](https://ziggit.dev/t/what-are-the-boundaries-of-the-eval-branch-quota/17976/3 "2026-10-15T15:40:30Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![npc1054657282](https://ziggit.dev/user_avatar/ziggit.dev/npc1054657282/32/5790_2.png) [@npc1054657282](https://ziggit.dev/u/npc1054657282)\
**Post date:** [October 15, 2026, 4:45pm UTC](https://ziggit.dev/t/what-are-the-boundaries-of-the-eval-branch-quota/17976/4 "2026-10-15T16:45:29Z")

</div>

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.

> [@nullsurplus](#):
>
> when it’s inherited from the caller

According to [#21324](https://github.com/ziglang/zig/issues/21324) and [#22410](https://github.com/ziglang/zig/issues/22410), there isn’t a satisfactory decision on the design yet.

---

<div class="post-metadata">

**Author:** ![nullsurplus](https://ziggit.dev/user_avatar/ziggit.dev/nullsurplus/32/10353_2.png) [@nullsurplus](https://ziggit.dev/u/nullsurplus)\
**Post date:** [October 15, 2026, 4:49pm UTC](https://ziggit.dev/t/what-are-the-boundaries-of-the-eval-branch-quota/17976/5 "2026-10-15T16:49:59Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![npc1054657282](https://ziggit.dev/user_avatar/ziggit.dev/npc1054657282/32/5790_2.png) [@npc1054657282](https://ziggit.dev/u/npc1054657282)\
**Post date:** [October 15, 2026, 5:00pm UTC](https://ziggit.dev/t/what-are-the-boundaries-of-the-eval-branch-quota/17976/6 "2026-10-15T17:00:15Z")

</div>

> [@nullsurplus](#):
>
> 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?

> [@npc1054657282](#):
>
> According to [#21324](https://github.com/ziglang/zig/issues/21324) and [#22410](https://github.com/ziglang/zig/issues/22410), there isn’t a satisfactory decision on the design yet.

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

---

<div class="post-metadata">

**Author:** ![nullsurplus](https://ziggit.dev/user_avatar/ziggit.dev/nullsurplus/32/10353_2.png) [@nullsurplus](https://ziggit.dev/u/nullsurplus)\
**Post date:** [October 15, 2026, 5:24pm UTC](https://ziggit.dev/t/what-are-the-boundaries-of-the-eval-branch-quota/17976/7 "2026-10-15T17:24:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![npc1054657282](https://ziggit.dev/user_avatar/ziggit.dev/npc1054657282/32/5790_2.png) [@npc1054657282](https://ziggit.dev/u/npc1054657282)\
**Post date:** [October 15, 2026, 6:43pm UTC](https://ziggit.dev/t/what-are-the-boundaries-of-the-eval-branch-quota/17976/8 "2026-10-15T18:43:53Z")

</div>

Pay attention to [`branch_quota`, `branch_count`](https://codeberg.org/ziglang/zig/src/commit/3f4f2c640b187b5f97cf67a3399f4c8c0005e8ff/src/Sema.zig#L76-L77), and [`quota_request`](https://codeberg.org/ziglang/zig/src/commit/3f4f2c640b187b5f97cf67a3399f4c8c0005e8ff/src/Sema.zig#L135-L137) 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](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](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#L7370-L7372)

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

---

<div class="post-metadata">

**Author:** ![nullsurplus](https://ziggit.dev/user_avatar/ziggit.dev/nullsurplus/32/10353_2.png) [@nullsurplus](https://ziggit.dev/u/nullsurplus)\
**Post date:** [October 15, 2026, 7:05pm UTC](https://ziggit.dev/t/what-are-the-boundaries-of-the-eval-branch-quota/17976/9 "2026-10-15T19:05:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![npc1054657282](https://ziggit.dev/user_avatar/ziggit.dev/npc1054657282/32/5790_2.png) [@npc1054657282](https://ziggit.dev/u/npc1054657282)\
**Post date:** [October 15, 2026, 7:37pm UTC](https://ziggit.dev/t/what-are-the-boundaries-of-the-eval-branch-quota/17976/10 "2026-10-15T19:37:59Z")

</div>

> [@nullsurplus](#):
>
> 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.

---

<div class="post-metadata">

**Author:** ![nullsurplus](https://ziggit.dev/user_avatar/ziggit.dev/nullsurplus/32/10353_2.png) [@nullsurplus](https://ziggit.dev/u/nullsurplus)\
**Post date:** [October 15, 2026, 8:29pm UTC](https://ziggit.dev/t/what-are-the-boundaries-of-the-eval-branch-quota/17976/11 "2026-10-15T20:29:13Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![zigo](https://ziggit.dev/user_avatar/ziggit.dev/zigo/32/2414_2.png) [@zigo](https://ziggit.dev/u/zigo)\
**Post date:** [October 16, 2026, 1:31am UTC](https://ziggit.dev/t/what-are-the-boundaries-of-the-eval-branch-quota/17976/12 "2026-10-16T01:31:05Z")

</div>

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