-
Couldn't load subscription status.
- Fork 1.4k
Optimize error path in FiberRuntime
#9975
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
kyri-petrou
merged 3 commits into
zio:series/2.x
from
kyri-petrou:optimize-error-path-fiber-runtime
Jun 19, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -538,21 +538,29 @@ final class FiberRuntime[E, A](fiberId: FiberId.Runtime, fiberRefs0: FiberRefs, | |
| val stack = _stack | ||
| val size = _stackSize // racy | ||
|
|
||
| builder += _lastTrace | ||
| var last = _lastTrace | ||
| builder += last | ||
|
|
||
| try { | ||
| if (stack ne null) { | ||
| var i = (if (stack.length < size) stack.length else size) - 1 | ||
|
|
||
| while (i >= 0) { | ||
| val k = stack(i) | ||
| if (k ne null) { // racy | ||
| builder += k.trace | ||
| val trace = k.trace | ||
| if (trace ne last) { | ||
| last = trace | ||
| builder += trace | ||
| } | ||
| i -= 1 | ||
| } | ||
| } | ||
| } | ||
|
|
||
| builder += id.location // TODO: Allow parent traces? | ||
| val loc = id.location | ||
| if (loc ne last) | ||
| builder += loc // TODO: Allow parent traces? | ||
|
|
||
| StackTrace(self.fiberId, builder.result()) | ||
| } finally { | ||
|
|
@@ -844,19 +852,27 @@ final class FiberRuntime[E, A](fiberId: FiberId.Runtime, fiberRefs0: FiberRefs, | |
| * | ||
| * '''NOTE''': This method must be invoked by the fiber itself. | ||
| */ | ||
| private def patchRuntimeFlags[R, E, A]( | ||
| private def patchRuntimeFlags[E0, A0]( | ||
| patch: RuntimeFlags.Patch, | ||
| cause: Cause[E0], | ||
| continueEffect: Exit[E0, A0] | ||
| ): Exit[E0, A0] = | ||
| patchRuntimeFlagsCause(patch, cause) match { | ||
| case null => continueEffect | ||
| case c => Exit.Failure(c) | ||
| } | ||
|
|
||
| private def patchRuntimeFlagsCause[E0]( | ||
| patch: RuntimeFlags.Patch, | ||
| cause: Cause[E], | ||
| continueEffect: ZIO[R, E, A] | ||
| ): ZIO[R, E, A] = { | ||
| cause: Cause[E0] | ||
| ): Cause[E0] = { | ||
| val changed = patchRuntimeFlagsOnly(patch) | ||
| val interruptEnabled = RuntimeFlags.Patch.isEnabled(patch, RuntimeFlag.Interruption.mask) | ||
|
|
||
| if (changed && interruptEnabled && shouldInterrupt()) { | ||
| if (cause ne null) Exit.Failure(cause ++ getInterruptedCause()) | ||
| else Exit.Failure(getInterruptedCause()) | ||
| } else if (cause ne null) Exit.Failure(cause) | ||
| else continueEffect | ||
| if (cause ne null) cause ++ getInterruptedCause() | ||
| else getInterruptedCause() | ||
| } else cause | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -1241,12 +1257,15 @@ final class FiberRuntime[E, A](fiberId: FiberId.Runtime, fiberRefs0: FiberRefs, | |
| } | ||
|
|
||
| case updateFlags: ZIO.UpdateRuntimeFlags => | ||
| cur = patchRuntimeFlags(updateFlags.update, cause, null) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This would have previously:
|
||
| cause = patchRuntimeFlagsCause(updateFlags.update, cause) | ||
| } | ||
| } | ||
|
|
||
| if (cur eq null) { | ||
| return failure | ||
| val f = | ||
| if (cause eq failure.cause) failure | ||
| else Exit.Failure(cause) | ||
| return f | ||
| } | ||
|
|
||
| case updateRuntimeFlags: UpdateRuntimeFlags => | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Much more performant to keep the
lastvariable in the stack memory instead of checking it inStackTraceBuilderwhich kept it in the heap