-
Notifications
You must be signed in to change notification settings - Fork 7.7k
Handle PSObject argument specially in method invocation logging #18060
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
Conversation
src/System.Management.Automation/engine/runtime/Operations/MiscOps.cs
Outdated
Show resolved
Hide resolved
case string: | ||
case BigInteger: | ||
case var _ when baseType.IsEnum || baseType.IsPrimitive: | ||
return baseObj.ToString(); |
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.
Please review the cases that are covered here, and let me know if any other cases should be added.
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.
We could take something from our TryFastTrackPrimitiveTypes() method.
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.
Uri value might be worth logging.
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.
Added Guid
, Uri
, Version
, and SemanticVersion
to the list.
@iSazonov I cannot find the TryFastTrackPrimitiveTypes
method.
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.
@iSazonov I cannot find the
TryFastTrackPrimitiveTypes
method.
Ye it's a nested method
PowerShell/src/System.Management.Automation/engine/MshObject.cs
Lines 1260 to 1294 in 397bd87
bool TryFastTrackPrimitiveTypes(object value, out string str) | |
{ | |
switch (Convert.GetTypeCode(value)) | |
{ | |
case TypeCode.String: | |
str = (string)value; | |
break; | |
case TypeCode.Byte: | |
case TypeCode.SByte: | |
case TypeCode.Int16: | |
case TypeCode.UInt16: | |
case TypeCode.Int32: | |
case TypeCode.UInt32: | |
case TypeCode.Int64: | |
case TypeCode.UInt64: | |
case TypeCode.DateTime: | |
case TypeCode.Decimal: | |
var formattable = (IFormattable)value; | |
str = formattable.ToString(format, formatProvider); | |
break; | |
case TypeCode.Double: | |
var dbl = (double)value; | |
str = dbl.ToString(format ?? LanguagePrimitives.DoublePrecision, formatProvider); | |
break; | |
case TypeCode.Single: | |
var sgl = (float)value; | |
str = sgl.ToString(format ?? LanguagePrimitives.SinglePrecision, formatProvider); | |
break; | |
default: | |
str = null; | |
return false; | |
} | |
return true; | |
} |
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.
Thanks! Added decimal
to the list.
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.
Also re-arrange the order of the case
clauses, based on the likelihood of those types being used as arguments.
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.
Use if
statement instead of switch
because the switch
statement generates IL to check "is the instance of this type" for every case
branch that matches a type, which is unnecessary. Also, it's easier to arrange the comparison order with if
statements.
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.
LGTM
This PR has Quantification details
Why proper sizing of changes matters
Optimal pull request sizes drive a better predictable PR flow as they strike a
What can I do to optimize my changes
How to interpret the change counts in git diff output
Was this comment helpful? 👍 :ok_hand: :thumbsdown: (Email) |
/backport to release/v7.3.0-rc.1 |
🎉 Handy links: |
PR Summary
Fix #18033
The
ToString
implementation ofPSObject
does extra work on the base object, for example, unrolling the base object if it's enumerable, which is very expensive.For method invocation logging, we should avoid calling
ToString
on aPSObject
argument. Instead,PSObject
, we represent it as"PSObject{}"
.PSObject
, or is aPSObject
wrapping a base object, we represent it asbaseObj.ToString()
.Here are some log messages with this change:
PR Checklist
.h
,.cpp
,.cs
,.ps1
and.psm1
files have the correct copyright headerWIP:
or[ WIP ]
to the beginning of the title (theWIP
bot will keep its status check atPending
while the prefix is present) and remove the prefix when the PR is ready.