Avoid interface method dispatches for iterables in Validator
#2499
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.
While looking at some profiles, I noticed a lot of
itable stub
s in Caliban'sValidator
. Most of those came from methods such asnonEmpty
andtoList
on classes that don't override them. In addition, in Scala 3 there was a fair bit of overhead when usingforeach
because it is not inlined (unlike Scala 2).In this PR, we:
nonEmpty
with eitherv ne Nil
for Lists and!v.isEmpty
for other iterables, and in some cases avoid method calls altogether when we know the iterable is empty.foreachOne
method as a substitute offoreach
for lists (which we heavily use inValidator
). In Scala 3 this is an inlined while loop, whereas in Scala 2 we simply proxy toforeach
(which the compiler inlines)unapply
methods wherever possible especially in cases that we are not using all of the arguments of a case classThis results in a rather noticeably performance improvement (for Scala 3, haven't tested for Scala 2) especially when validating small queries (up to 50% improvement)
series/2.x:
PR: