-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Reduce allocation in TextWriter.NewLine #121508
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -2,6 +2,7 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // The .NET Foundation licenses this file to you under the MIT license. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| using System.Buffers; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| using System.Diagnostics; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| using System.Diagnostics.CodeAnalysis; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| using System.Globalization; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| using System.Runtime.CompilerServices; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -25,6 +26,9 @@ public abstract partial class TextWriter : MarshalByRefObject, IDisposable, IAsy | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // We don't want to allocate on every TextWriter creation, so cache the char array. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private static readonly char[] s_coreNewLine = Environment.NewLineConst.ToCharArray(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // s_coreNewLine will be ['\r', '\n'] on Windows and ['\n'] on Unix. This is the opposite, lazily created/cached. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private static char[]? s_otherCoreNewLine; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// <summary> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// This is the 'NewLine' property expressed as a char[]. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// It is exposed to subclasses as a protected field for read-only | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -112,8 +116,17 @@ public virtual string NewLine | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| value ??= Environment.NewLineConst; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| CoreNewLineStr = value; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| CoreNewLine = value.ToCharArray(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (CoreNewLineStr != value) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| CoreNewLineStr = value; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| CoreNewLine = | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| value == Environment.NewLineConst ? s_coreNewLine : // current OS default | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Environment.NewLineConst == "\r\n" && value == "\n" ? (s_otherCoreNewLine ??= ['\n']) : // other OS default | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Environment.NewLineConst == "\n" && value == "\r\n" ? (s_otherCoreNewLine ??= ['\r', '\n']) : // other OS default | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| value.ToCharArray(); // unknown | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+123
to
+126
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe a switch expression to improve readability?
Suggested change
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The way I wrote it, the C# compiler will detect that some of the branches are dead and dead-code eliminate them, e.g. on Linux, the line that starts with
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, I see. With tuples Roslyn doesn't see that the branche is dead code. With the following it does Sharplab / DiffChecker.
Suggested change
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yup, though I personally don't think that improves readability.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
File an issue? |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Debug.Assert(CoreNewLineStr == new string(CoreNewLine)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.