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

empty string vs. null string #74126

Open
kasperk81 opened this issue Jun 24, 2024 · 5 comments
Open

empty string vs. null string #74126

kasperk81 opened this issue Jun 24, 2024 · 5 comments
Labels
Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead
Milestone

Comments

@kasperk81
Copy link

s += null;

gets compiled to s ?? ""

s += string.Empty;

gets compiled to a method call s = string.Concat(s, string.Empty). can it be s ?? "" as well?

@dotnet-issue-labeler dotnet-issue-labeler bot added Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead labels Jun 24, 2024
@jaredpar
Copy link
Member

can it be s ?? "" as well?

Possibly but is there a specific benefit of doing that? Does it shrink IL, have better throughput, etc ....

@kasperk81
Copy link
Author

yes there is a codegen impact.
@EgorBo assuming runtime can't optimize this pattern?

@jaredpar
Copy link
Member

Okay but how common is this pattern? Hard to see this being a big item we should be prioritizing.

@jaredpar jaredpar added this to the Backlog milestone Jun 27, 2024
@EgorBo
Copy link
Member

EgorBo commented Jun 28, 2024

yes there is a codegen impact. @EgorBo assuming runtime can't optimize this pattern?

I think we can tweak the JIT's inliner a bit to optimize this case, here it's not optimized because String.Concat is not inlined

@kasperk81
Copy link
Author

if it can be handled in runtime, i will close this. something i noticed in a pr review somewhere that preference was to use string.Empty in new dotnet versions because "there is no difference in perf", then i found this call which broke that assumption.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead
Projects
None yet
Development

No branches or pull requests

3 participants