Use null-coalescing assignment where applicable#294
Use null-coalescing assignment where applicable#294slozier merged 3 commits intoIronLanguages:mainfrom
Conversation
| _alias = "$lambda" + ++_lambdaId; | ||
| } | ||
| } | ||
| _alias ??= lambda.Name; |
There was a problem hiding this comment.
I guess this is adding an extra null check so not quite equivalent. Was this intentional?
There was a problem hiding this comment.
Thanks for paying attention 😄 (there are a lot of changes), but yes, it was intentional, for readability.
I also thought it would be better performance-wise, but after closer examination of the generated IL code it turns out that the performance is better in Debug (unoptimized) configuration but worse in Release (optimized).
So I will fix it in the next commit.
Here is some background info. Considering the code path when _alias is already not null.
if (_alias == null) {
_alias = lambda.Name;
_alias ??= "$lambda" + ++_lambdaId;
}generates normally
IL_0001 ldarg.0
IL_0002 ldnull
IL_0003 ceq
IL_0005 stloc.0
IL_0006 ldloc.0
IL_0007 brfalse.s IL_0039
...
IL_0039 ret
But when optimized:
IL_0000 ldarg.0
IL_0001 brtrue.s IL_0031
...
IL_0031 ret
Code:
_alias ??= lambda.Name;
_alias ??= "$lambda" + ++_lambdaId;generates in both release and debug:
IL_0001 ldarg.0
IL_0002 brtrue.s IL_000C
...
IL_000C ldarg.0
IL_000D brtrue.s IL_0032
...
IL_0032 ret
There was a problem hiding this comment.
Another option that wins in both Debug and Release:
_alias ??= lambda.Name ?? "$lambda" + ++_lambdaId;
This is for both performance and readability improvements.