Skip to content

WIP - Fix performance and memory allocation issues in ReadPdhMultiString. - #5845

Closed
jcarino wants to merge 1 commit into
PowerShell:masterfrom
jcarino:master
Closed

WIP - Fix performance and memory allocation issues in ReadPdhMultiString.#5845
jcarino wants to merge 1 commit into
PowerShell:masterfrom
jcarino:master

Conversation

@jcarino

@jcarino jcarino commented Jan 10, 2018

Copy link
Copy Markdown

PR Summary

String concats in ReadPdhMultiString have been replaced with a StringBuilder to significantly reduce heap allocation and GC.

ReadPdhMultiString reads characters one by one from a pointer location into a string, then splits that string into a string collection (on the null character '\0'). However, it does this through a looped string concat, allocating a new string on the heap for each character read in. This has significant GC implications for large strings. The string concats have been replaced with a string builder, and the strings are split and added to the StringCollection on the fly.

PR Checklist

Note: Please mark anything not applicable to this PR NA.

@msftclas

Microsoft Contribution License Agreements (msftclas) commented Jan 10, 2018

Copy link
Copy Markdown

CLA assistant check
All CLA requirements met.

@iSazonov

Copy link
Copy Markdown
Collaborator

jcarino We can not merge the contribution until you sign CLA.

@iSazonov

Copy link
Copy Markdown
Collaborator

Ah, PdhHelper.cs isn't used currently and excluded from compiling.

@jcarino

jcarino commented Jan 11, 2018

Copy link
Copy Markdown
Author

CLA has now been signed.
I know this module is excluded due to issue #4303 but this is an issue in production, and if/when related modules are added back this might be useful.

char nextChar = (char)next4;
if (nextChar == '\0')
{
strColl.Add(sb.ToString());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding to strColl should occur outside the while loop. The updated logic causes the contents of sb to be dropped on the floor by the break statement when next4 == 0.

Essentially, replace the original allSubstringsWithNulls += (char)next4 statement with sb.Append(nextChar) and move the strColl.Add statement to the original location after the while loop.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, but adding to strColl only outside the while loop has some memory implications (has to alloc at least one extra string before Split). Can we just add another strColl.Add(sb.ToString()); outside the while to catch whatever is left in sb?

@iSazonov

Copy link
Copy Markdown
Collaborator

Dan Travison (@dantraMSFT) Please clarify - this file is excluded from compilation - is it acceptable to change it without tests? And will we use it later?

@daxian-dbw

Copy link
Copy Markdown
Member

this file is excluded from compilation

Ilya (@iSazonov) Thanks for bringing it up :) You are right that we don't have a way to officially validate the changes. Travis Plunk (@TravisEz13), any thoughts?

For background: the perf counter cmdlets are disabled due to unsupported APIs. But the files are left in place so that

  1. They can be brought back easily in future once the issue is resolved.
  2. Users can enable building those cmdlets themselves and use the cmdlets privately.

@iSazonov

Ilya (iSazonov) commented Jan 19, 2018

Copy link
Copy Markdown
Collaborator

We should not make changes that are not tested.

I think we can postpone this change until the API is ported in the future. I've seen that CoreFX team still doesn't have a consensus how to do it.

Also if we decide to use Windows Compatibility Pack and it will support this API, then we could also include this in the compilation.

@TravisEz13

Copy link
Copy Markdown
Member

Not only is it not tested. The file is not being compiled. I don't think we can take any changes to this file except possibly adding a comment to this effect and pointing to the issue to add the CmdLets back using supported APIs. #4306

@TravisEz13 Travis Plunk (TravisEz13) changed the title Fix performance and memory allocation issues in ReadPdhMultiString. WIP - Fix performance and memory allocation issues in ReadPdhMultiString. Jan 20, 2018
@iSazonov

Copy link
Copy Markdown
Collaborator

I think we should close the PR until we compile this code again.

@TravisEz13

Copy link
Copy Markdown
Member

jcarino The consensus is that we cannot take changes to this code at this time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants