Skip to content
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

Fix Route merge order to fix Issue #3066 #3160

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 3 additions & 9 deletions zio-http/shared/src/main/scala/zio/http/Routes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -251,19 +251,13 @@ final case class Routes[-Env, +Err](routes: Chunk[zio.http.Route[Env, Err]]) { s
val chunk = tree.get(req.method, req.path)
chunk.length match {
case 0 => Handler.notFound
case 1 => chunk(0)
case n => // TODO: Support precomputed fallback among all chunk elements
var acc = chunk(0)
var i = 1
while (i < n) {
val h = chunk(i)
acc = acc.catchAll { response =>
case _ => // TODO: Support precomputed fallback among all chunk elements
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the idea here but this is too slow. It's in a hot path and needs to be optimized. You should definitely not delete the special cases (1, n), and if you need to do reverse iterator and reduction, it needs to be done somewhere else (not here, in the hot path), as a pre-computation.

chunk.reverseIterator.reduceLeft { (acc, h) =>
acc.catchAll { response =>
if (response.status == Status.NotFound) h
else Handler.fail(response)
}
i += 1
}
acc
}
}
.merge
Expand Down
Loading