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.
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
Add fault telemetry for (hopefully) all VS APIs #3775
Add fault telemetry for (hopefully) all VS APIs #3775
Changes from all commits
01e5d9c
7577062
dc4cfa8
f3b8320
b3f8d3b
a6fc680
59157cc
4d6777c
6d68f8d
File filter
Filter by extension
Conversations
Jump to
There are no files selected for viewing
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.
Can you make this one same of line 36, just to keep same coding format.
fault.Properties[$"{VSTelemetrySession.VSPropertyNamePrefix}kvp.Key"] = kvp.Value;
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 don't know if the C# compiler is better now, but initially at least interpolated strings (
$"... {something}"
was just syntactic sugar forstring.Format
, and the book "Writing High Performance .NET Code" discusses howstring.Format
harms performance since the runtime has to parse the format string, and build up the final string. Their recommendation was to use string concatenation instead. Hence I changed line 36 to do that instead.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.
Didn't realize string.Format had that perf hit. I think StringBuilder is the most perf and memory efficient, right?
I don't really prefer reading the concatenation, especially inline an indexer like that.
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.
c#10 (or maybe later) might have const interpolated strings, so in the future this may no longer be necessary. But
string.Concat
can produce const strings determined at compile time, rather than runtime. And at runtime,string.Concat
(which the+
operator is syntactic sugar for) remains to be by far the fastest execution at runtime as well.string.Concat
only takes strings as input, anything that is not a string is implicitly run though.ToString()
before being passed tostring.Concat
, meaning thatstring.Concat
can useargs.Sum(a => a.Length)
to determine the exact length of the final string, do a single memory allocation of the required length, then copy the input strings one by one.Using a
StringBuilder
is the next best. It has a small allocation for theStringBuilder
object iteself, and it allocates at least one buffer. As you callAppend
, it copies your string into the buffer, and if the buffer gets full, it needs to allocate more buffers. Finally, when you callStringBuilder.ToString()
, it can do the same work asstring.Concat
, which as previously stated is an additional allocation of the required length. Hence, we can see thatStringBuilder
cannot possibly be more efficient thanstring.Concat
, since it does everything thatstring.Concat
does, plus more.string.Format
(which is what interpolated strings are, and also what StringBuilder'sAppendFormat
method is) is the least efficient. Internally, it creates a string builder (except in the StringBuilder.AppendFormat case), since it can't figure out what the final string length is going to be from the inputs alone. Therefore it can't possibly be faster thanStringBuilder
, since it uses aStringBuilder
. As previously mentioned, Format also needs to parse the input string to find the{x}
placeholders, copy the static parts, then parse the placeholder to see if there are formatting options, then callToString
on the relevant object with the discovered formatting options.string.Format
andStringBuilder.AppendFormat
are therefore by far the slowest at runtime. My memory is that the Writing High Performance .NET Code book claimed that usingStringBuilder
with a series ofAppend
calls is faster thanstring.Format
.So in summary,
constStr1 + constStr2
will be evaluated at compile time, so runtime does nothing more than load a const string. This should become available for interpolated const strings in the future, but is not yet available. If the strings are not const at compile time, string concatenation remains the fastest, so if you want to hyper-optimize allocations, this is the best bet. It's always best if all strings you need to concat are in a single call, but even if you usestring.Concat
a small number of times to generate a single string, it can still be faster than using aStringBuilder
. Using (non-const) interpolated strings, orstring.Format
basically means you don't care about performance. Having said that, while we don't want our tools to be slow, client tools don't have the same concerns or cost savings that web services like Bing or Office web applications have.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.
Strings that are already allocated. I thought the whole memory consumption (not perf) problem with concat was in
string1 + string2
, you allocate 2 strings, then you allocate a result string and garbage collect 1 and 2. StringBuilder's buffering avoided that.Anyway, thanks for the writeup and book recommendation!
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.
this time of delegation makes me cringe :D
The problem is that the only other
alternative
I can suggest is to have a wrapper that uses reflection.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.
Another alternative, which I implemented, is to call
.ToList()
at the end of each. This of course has multiple performance impacts. If the caller doesWhatever().First()
, then we'll now be evaluating every item in the result set, not just the first. Also, we'll always be doing extra allocations, but it's hard to imagine that these APIs are being used in such critical codepaths where that perf is important.