-
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
Changes from 1 commit
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 |
---|---|---|
@@ -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 was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,7 +37,7 @@ | |
<PackageReference Include="JavaScriptEngineSwitcher.Msie" Version="2.4.9" /> | ||
<PackageReference Include="JSPool" Version="3.0.1" /> | ||
<PackageReference Include="MsieJavaScriptEngine" Version="2.2.2" /> | ||
<PackageReference Include="Newtonsoft.Json" Version="9.0.1" /> | ||
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" /> | ||
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. Is bumping 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. No, split PR fail, bumping JSON.NET need in part 2 for use ArrayPool, fixed |
||
</ItemGroup> | ||
|
||
<ItemGroup Condition=" '$(TargetFramework)' == 'net40' "> | ||
|
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