-
Notifications
You must be signed in to change notification settings - Fork 926
Avoid memory allocation and improve performance, part 1 #522
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
Closed
DaniilSokolyuk
wants to merge
2
commits into
reactjs:master
from
DaniilSokolyuk:avoid-memory-allocation
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* Copyright (c) 2014-Present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
|
||
using System; | ||
using System.IO; | ||
|
||
#if LEGACYASPNET | ||
using System.Text; | ||
using System.Web; | ||
#else | ||
using System.Text.Encodings.Web; | ||
using IHtmlString = Microsoft.AspNetCore.Html.IHtmlContent; | ||
#endif | ||
|
||
#if LEGACYASPNET | ||
namespace React.Web.Mvc | ||
#else | ||
namespace React.AspNet | ||
#endif | ||
{ | ||
/// <summary> | ||
/// IHtmlString or IHtmlString action wrapper implementation | ||
/// </summary> | ||
public class ActionHtmlString : IHtmlString | ||
{ | ||
private readonly Action<TextWriter> _textWriter; | ||
|
||
/// <summary> | ||
/// Constructor IHtmlString or IHtmlString action wrapper implementation | ||
/// </summary> | ||
/// <param name="textWriter"></param> | ||
public ActionHtmlString(Action<TextWriter> textWriter) | ||
{ | ||
_textWriter = textWriter; | ||
} | ||
|
||
#if LEGACYASPNET | ||
[ThreadStatic] | ||
private static StringWriter _sharedStringWriter; | ||
|
||
/// <summary>Returns an HTML-encoded string.</summary> | ||
/// <returns>An HTML-encoded string.</returns> | ||
public string ToHtmlString() | ||
{ | ||
var stringWriter = _sharedStringWriter; | ||
if (stringWriter != null) | ||
{ | ||
stringWriter.GetStringBuilder().Clear(); | ||
} | ||
else | ||
{ | ||
_sharedStringWriter = stringWriter = new StringWriter(new StringBuilder(512)); | ||
} | ||
|
||
_textWriter(stringWriter); | ||
return stringWriter.ToString(); | ||
} | ||
#else | ||
/// <summary> | ||
/// Writes the content by encoding it with the specified <paramref name="encoder" /> | ||
/// to the specified <paramref name="writer" />. | ||
/// </summary> | ||
/// <param name="writer">The <see cref="T:System.IO.TextWriter" /> to which the content is written.</param> | ||
/// <param name="encoder">The <see cref="T:System.Text.Encodings.Web.HtmlEncoder" /> which encodes the content to be written.</param> | ||
public void WriteTo(TextWriter writer, HtmlEncoder encoder) | ||
{ | ||
_textWriter(writer); | ||
} | ||
#endif | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see that we're manually constructing HTML tags in existing code.. as long as we're not passing user-generated data to these methods, this should be OK (although it would be nice to sanitize the data going into these attributes just in case)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yea, original tag builder has a similar code https://github.com/aspnet/Mvc/blob/feed0fea2c5280264ac000947b3fee542d7d9e6a/src/Microsoft.AspNetCore.Mvc.ViewFeatures/Rendering/TagBuilder.cs#L336