Update argument validation to favour JIT in-lining #903
Description
In most operators, we have guard statements that are executed immediately, while the bulk of the operator is implemented in a separate method (see #837 for discussion on why this pattern is used). Currently, the guard statements cannot be inlined to the calling method. This is because throw
statements are used directly, and throw
statements prevent the JIT from inlining any method.
We should move guard statements to separate methods, which would allow them to be inlined in release builds. In some cases, this would be that due to available context information to the JIT, they can be eliminated entirely and the final JIT code only has a direct call to the internal operator code.
Personal recommendation: add a reference to CommunityToolkit.Diagnostics
, and update all guard statements to these. This would allow us to rely on a shared implementation of this boilerplate code and if others do as well, then that is mutual code shared.
Alternative recommendation: Add our own Guard
and ThrowHelper
methods and update all guard statements to these. This would duplicate code that could be shared, but not add a dependency on another library.