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

[API Proposal]: Add StringBuilder overload for WebUtility.HtmlEncode #109591

Open
Lanayx opened this issue Nov 6, 2024 · 4 comments
Open

[API Proposal]: Add StringBuilder overload for WebUtility.HtmlEncode #109591

Lanayx opened this issue Nov 6, 2024 · 4 comments
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Net untriaged New issue has not been triaged by the area owner

Comments

@Lanayx
Copy link

Lanayx commented Nov 6, 2024

Background and motivation

I suggest adding StringBuilder overloads in WebUtility similar to TextWriter one. Actually one very similar already exists for private implementation.

Motivation: in Oxpecker framework (which is currently one of the top frameworks at Techempower benchmarks) I'm using StringBuilder for ViewEngine rather than TextWriter because it gives about 10% performance increase for rendering HTML views to string. One downside is that HtmlEncode method doesn't allow for StringBuilder, so I'm allocating strings each time encoding is required.
If overload is added, it can behave faster than TextWriter since no intermediate ValueStringBuilder instances are requried.

API Proposal

public static class WebUtility
{
    public static void HtmlEncode(string? value, StringBuilder output)
    public static void HtmlDecode(string? value, StringBuilder output)
}

API Usage

var sb = new StringBuilder();
WebUtility.HtmlEncode("myString", sb);

Alternative Designs

No response

Risks

No response

@Lanayx Lanayx added the api-suggestion Early API idea and discussion, it is NOT ready for implementation label Nov 6, 2024
@dotnet-issue-labeler dotnet-issue-labeler bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Nov 6, 2024
@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Nov 6, 2024
@Lanayx
Copy link
Author

Lanayx commented Nov 6, 2024

@timcassel Yes, I'm aware of this type, but as I said - using it gives 10% overhead in my benchmarks compared to straight StringBuilder usage. With implemented, overload with StringBuilder will give performance difference even larger (due to aforementioned possibility of getting rid of intermediate string builder in implementation)

@julealgon
Copy link

@Lanayx please add a proper title to the issue so people are not forced to open the link to see what it is about.

@Lanayx Lanayx changed the title [API Proposal]: [API Proposal]: Add StringBuilder overload for WebUtility.HtmlEncode Nov 6, 2024
@Lanayx
Copy link
Author

Lanayx commented Nov 6, 2024

Thank you for noticing, done

@MihaZupan MihaZupan added area-System.Net and removed needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners labels Nov 6, 2024
Copy link
Contributor

Tagging subscribers to this area: @dotnet/ncl
See info in area-owners.md if you want to be subscribed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Net untriaged New issue has not been triaged by the area owner
Projects
None yet
Development

No branches or pull requests

3 participants