Вы сравниваете не два способа обработки одной и той же работы. Прогон с try выходит на первой же битой строке. Несколько сотен µs на 2M строк означает, что цикл едва успел начаться, а имя ошибки, которое вы видите, относится к этому раннему возврату. Если в генераторе i % 5000 == 0, то строка 0 уже битая, так что он падает сразу.
Прогон с catch unreachable — отдельная история. В ReleaseFast это undefined behaviour на первой же битой строке, так что оптимизатор может делать что угодно. Поэтому и сумма не сходится. Debug и ReleaseSafe паникуют, потому что реально проверяют. Десяткам мс тоже нельзя доверять, ведь компилятор мог свернуть цикл во что-то странное.
Я бы сначала прогнал бенч с нулём битых строк, а потом с битыми, обработанными через catch continue или счётчик. И сравнил бы их. Выводите вместе с суммой, сколько строк реально распарсено, чтобы знать, что цикл дошёл до конца.
Честно говоря, мне это похоже на какую-то причуду кодгена, так что проверьте ещё версию Zig. На какой сборке вы сидите?
Переведено с английского·Показать оригиналYou're not comparing two ways of handling the same work. The try run is bailing out at the first bad line. A few hundred µs for 2M lines means it barely got started, and the error name you see is the one from that early return. If your generator does i % 5000 == 0, line 0 is already corrupt, so it dies straight away.
The catch unreachable run is a different problem. In ReleaseFast that's undefined behaviour on the first bad line, so the optimiser can do whatever it likes. That's why the total is off. Debug and ReleaseSafe panic because they actually check. The tens of ms isn't a number you can trust either, since the compiler may have folded the loop into something odd.
I'd bench it with zero bad lines first, then bad lines handled with catch continue or a counter. Compare those. Print how many lines were actually parsed alongside the total, so you know the loop ran to the end.
Honestly it looks like a codegen quirk to me, so I'd check your Zig version too. Which build are you on?