-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Introduce string builder factory template. #4611
Open
paulomorgado
wants to merge
1
commit into
RicoSuter:master
Choose a base branch
from
paulomorgado:performance/string-builder
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+178
−81
Open
Changes from all commits
Commits
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
3 changes: 3 additions & 0 deletions
3
src/NSwag.CodeGeneration.CSharp/Templates/Client.Class.StringBuilder.liquid
This file contains 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,3 @@ | ||
private static global::System.Text.StringBuilder GetStringBuilder() => new global::System.Text.StringBuilder(8000); | ||
|
||
private partial static void ReturnStringBuilder(global::System.Text.StringBuilder sb); |
This file contains 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 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.
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.
How common is this scenario? I think if you find a GC allocated StringBuilder to effect your performance, you might not want to use a generated REST client in the first place...
If this is approved though, I think the avarage url length is a lot shorter than 8000 characters.
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.
If you run this code:
you'll see that the
StringBuilder
, by default, is a linked list ofStringBuilder
instances with 16 characters.This is especially painful if you need to traverse the chain. It is especially painful if you are inserting, removing or replacing characters.
PR #4585 removed replace operations and generates append only operations.
By moving this to its own template, it allows users to use any source compatible version they want. Including special implementations (.NET is full of private
ValueStringBuilder
implementations) and pooling.The 8000-character initial size is something I anticipate no one reaches and is easily collected by the GC.
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 know how StringBuilder works internally, I'm just saying I highly doubt theres a need out there to want to pool the StringBuilder.
I'm not against using a default capacity to the StringBuilder object though, but I still think 8000 is too large.
The reason is because the IIS maximum url length defaults to 4096, and at this point the HttpClient likely already has a BaseAddress attached to it, so it won't be part of the appending here. So even though 8000 is too low for LOH its still an unnecessarily big allocation.
By the way, how are you supposed to override the implementation of GetStringBuilder() from this class? And
private partial static void ReturnStringBuilder
is an illegal method declaration, you need to make itstatic partial void ReturnStringBuilder
.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.
Replacing the template for generation.