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

"Should be a compile time constant" warning when using nameof #9

Closed
lennartb- opened this issue Aug 3, 2020 · 6 comments
Closed

"Should be a compile time constant" warning when using nameof #9

lennartb- opened this issue Aug 3, 2020 · 6 comments

Comments

@lennartb-
Copy link

Example: Log.Information($"Hello {nameof(MyClass)}"); causes a "Message template should be a compile time constant" warning, but nameof expressions are evaluated at compile time.

@olsh olsh added help wanted Extra attention is needed bug Something isn't working labels Aug 5, 2020
@olsh
Copy link
Owner

olsh commented Nov 24, 2020

Well, after some research, I think that it's not a mistake.
Yes, the nameof expression is evaluated at compile-time, but the interpolation is not.

The compilation of this code fails

const string a = $"Hello {nameof(String)}";

But this code is valid

const string a = "Hello " + nameof(String);

You may say that this irrelevant in the context of Serilog templates. Because both interpolation and concatenation produce an immutable string, but that's not completely true either.

Let's look at IL for the code

Console.WriteLine($"Hello {nameof(String)}");

On .NET Core 3.1+ it creates a constant

IL_0000:  ldstr       "Hello String"
IL_0005:  call        System.Console.WriteLine
IL_000A:  ret       

But on old full .NET Framework string.Format() is called internally

IL_0000:  ldstr       "Hello {0}"
IL_0005:  ldstr       "String"
IL_000A:  call        System.String.Format
IL_000F:  call        System.Console.WriteLine
IL_0014:  ret         

And this is a performance issue
https://nblumhardt.com/2014/09/how-not-to-parameterize-serilog-events/

Don’t do this. While never particularly good logging practise (the string concatenation occurs regardless of whether logging is enabled or not, wasting RAM and CPU cycles), with Serilog, this is strongly considered an anti-pattern.

So I'm not sure if we want to treat the interpolation as a constant.

Please let me know what you think.

P.S.
There is a proposal for constant interpolated strings.
dotnet/csharplang#2951

@olsh olsh added discussion and removed bug Something isn't working help wanted Extra attention is needed labels Nov 24, 2020
@lennartb-
Copy link
Author

lennartb- commented Nov 26, 2020

Interesting, didn't consider what is happening under the hood. Ultimately, I don't think it's a big deal - if this is a potential performance issue then the warning is warranted. The only improvement I can think of would be to split off the warning into a different analyzer (something like "nameof() incurs a performance penalty"), maybe even detecting if it's running on Framework or .NET Core 3.1+ if that's possible, so it only gets displayed if it doesn't get compiled into an actual constant.

@olsh
Copy link
Owner

olsh commented Nov 26, 2020

Hi @lennartb-,

The only improvement I can think of would be to split off the warning into a different analyzer (something like "nameof() incurs a performance penalty")

nameof doesn't incur a performance penalty per se. The problem here is an interpolation.

const string a = "String";
Console.WriteLine($"Hello {a}");
IL_0000:  ldstr       "Hello {0}"
IL_0005:  ldstr       "String"
IL_000A:  call        System.String.Format
IL_000F:  call        System.Console.WriteLine

As you can see using a constant as a placeholder produces the same result.

maybe even detecting if it's running on Framework or .NET Core 3.1+ if that's possible, so it only gets displayed if it doesn't get compiled into an actual constant.

I believe this is possible. But relying on pretty low-level compiler optimization is dangerous.
In my opinion, the safest approach which will work for all .NET versions is to use old-plain concatenation

Log.Information("Hello " + nameof(String));

or Serilog way

Log.Information("Hello {ClassName}", nameof(String));

You'll get a ClassName property here as a bonus.

@olsh
Copy link
Owner

olsh commented Dec 24, 2020

Closed due to inactivity, feel free to re-open the issue.

@markusschaber
Copy link

@olsh Looks like C# 10 has constant interpolated strings, giving this issue some fresh wind:
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-10.0/constant_interpolated_strings

@olsh
Copy link
Owner

olsh commented Feb 17, 2022

Well, it works if you target C# 10
C# 10
image

C# 9
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants