Skip to content

Conversation

@strawgate
Copy link
Contributor

@strawgate strawgate commented Jun 29, 2021

PR Summary

Removes a previously-needed check from the convertfrom-json cmdlet. It appears a pre-parsing step was added for json whose root element was an array -- because of some issues with how Newtonsoft handled issues with Arrays in deserializeObject JamesNK/Newtonsoft.Json#1930

Upon further testing this bug is no longer present in Newtonsoft.Json and this preparsing can be removed. This was previously attempted and then rolled back because newtonsoft.json failed when deserializing a [. I have no idea why we needed to double parse this to check if the contents were [ but the tests are passing and the cases previously listed as causing issues are now working.

PR Context

It makes ConvertFrom-Json and Invoke-RestMethod ~50% faster. It does not reduce memory usage because the pre-parsing was immediately tossed out after it occurred.

Benchmark for parsing a 100MB JSON blob whose root element is an array.

Master:

Run Built-in Cmdlet: Hashtable

  • Memory Usage: 1079.3671875 MB
  • Runtime: 7256.4268 ms

Run Built-in Cmdlet: PSCustomObject

  • Memory Usage: 1338.74609375 MB
  • Runtime: 10430.8258 ms

ThisPR:

Run Built-in Cmdlet: Hashtable

  • Memory Usage: 1077.58203125 MB
  • Runtime: 3885.0489 ms

Run Built-in Cmdlet: PSCustomObject

  • Memory Usage: 1374.69921875 MB
  • Runtime: 6976.1836 ms

PR Checklist

@iSazonov
Copy link
Collaborator

Have we direct test for the scenario?

@iSazonov iSazonov added the CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log label Jun 29, 2021
@strawgate
Copy link
Contributor Author

strawgate commented Jun 29, 2021

I manually tested this but it looks like a test like what was added earlier and then removed would work?

    It 'Throws an ArgumentException with an incomplete array with AsHashtable switch set to <AsHashtable>' -TestCase $testCasesWithAndWithoutAsHashtableSwitch {
        Param($AsHashtable)
        { ConvertFrom-Json '[' -AsHashtable:$AsHashtable } |
            Should -Throw -ErrorId "System.ArgumentException,Microsoft.PowerShell.Commands.ConvertFromJsonCommand"
        { ConvertFrom-Json '["1",' -AsHashtable:$AsHashtable } |
            Should -Throw -ErrorId "System.ArgumentException,Microsoft.PowerShell.Commands.ConvertFromJsonCommand"
    }

Thoughts?

@strawgate
Copy link
Contributor Author

Have we direct test for the scenario?

I added a test for this scenario

@ghost ghost added the Review - Needed The PR is being reviewed label Jul 7, 2021
@ghost
Copy link

ghost commented Jul 7, 2021

This pull request has been automatically marked as Review Needed because it has been there has not been any activity for 7 days.
Maintainer, please provide feedback and/or mark it as Waiting on Author

@strawgate
Copy link
Contributor Author

I believe that this is ready to merge.

@strawgate
Copy link
Contributor Author

I think this is ready if any of the pending reviewers have any questions

@anmenaga anmenaga closed this Nov 30, 2021
@ghost ghost removed the Review - Needed The PR is being reviewed label Nov 30, 2021
@anmenaga anmenaga reopened this Nov 30, 2021
@pull-request-quantifier-deprecated

This PR has 6 quantified lines of changes. In general, a change size of upto 200 lines is ideal for the best PR experience!


Quantification details

Label      : Extra Small
Size       : +4 -2
Percentile : 2.4%

Total files changed: 2

Change summary by file extension:
.cs : +0 -2
.ps1 : +4 -0

Change counts above are quantified counts, based on the PullRequestQuantifier customizations.

Why proper sizing of changes matters

Optimal pull request sizes drive a better predictable PR flow as they strike a
balance between between PR complexity and PR review overhead. PRs within the
optimal size (typical small, or medium sized PRs) mean:

  • Fast and predictable releases to production:
    • Optimal size changes are more likely to be reviewed faster with fewer
      iterations.
    • Similarity in low PR complexity drives similar review times.
  • Review quality is likely higher as complexity is lower:
    • Bugs are more likely to be detected.
    • Code inconsistencies are more likely to be detetcted.
  • Knowledge sharing is improved within the participants:
    • Small portions can be assimilated better.
  • Better engineering practices are exercised:
    • Solving big problems by dividing them in well contained, smaller problems.
    • Exercising separation of concerns within the code changes.

What can I do to optimize my changes

  • Use the PullRequestQuantifier to quantify your PR accurately
    • Create a context profile for your repo using the context generator
    • Exclude files that are not necessary to be reviewed or do not increase the review complexity. Example: Autogenerated code, docs, project IDE setting files, binaries, etc. Check out the Excluded section from your prquantifier.yaml context profile.
    • Understand your typical change complexity, drive towards the desired complexity by adjusting the label mapping in your prquantifier.yaml context profile.
    • Only use the labels that matter to you, see context specification to customize your prquantifier.yaml context profile.
  • Change your engineering behaviors
    • For PRs that fall outside of the desired spectrum, review the details and check if:
      • Your PR could be split in smaller, self-contained PRs instead
      • Your PR only solves one particular issue. (For example, don't refactor and code new features in the same PR).

How to interpret the change counts in git diff output

  • One line was added: +1 -0
  • One line was deleted: +0 -1
  • One line was modified: +1 -1 (git diff doesn't know about modified, it will
    interpret that line like one addition plus one deletion)
  • Change percentiles: Change characteristics (addition, deletion, modification)
    of this PR in relation to all other PRs within the repository.


Was this comment helpful? 👍  :ok_hand:  :thumbsdown: (Email)
Customize PullRequestQuantifier for this repository.

@anmenaga
Copy link

Reopened PR to reset hanging PR checks.

@anmenaga
Copy link

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 5 pipeline(s).

@ghost ghost added the Review - Needed The PR is being reviewed label Dec 8, 2021
@ghost
Copy link

ghost commented Dec 8, 2021

This pull request has been automatically marked as Review Needed because it has been there has not been any activity for 7 days.
Maintainer, please provide feedback and/or mark it as Waiting on Author

@daxian-dbw
Copy link
Member

/rebase

@ghost ghost removed the Review - Needed The PR is being reviewed label Jul 25, 2022
@github-actions
Copy link
Contributor

github-actions bot commented Jul 25, 2022

Started rebase: https://github.com/PowerShell/PowerShell/actions/runs/2734744349

GitHub
PowerShell for every system! Contribute to PowerShell/PowerShell development by creating an account on GitHub.

@daxian-dbw daxian-dbw closed this Jul 25, 2022
@daxian-dbw daxian-dbw reopened this Jul 25, 2022
@daxian-dbw
Copy link
Member

The pre-parsing was previously removed by #8346, but then a regression was found because a feature-level test failed: #8346 (comment), and hence the change was reverted (#8375).

I have verified that the previously failing feature-level test passes with changes in this PR. So, it looks to me we are safe to merge this PR.

It "ConvertFrom-Json deserializes an array of PSObjects (in multiple lines) as a single string." {
# Create an array of PSCustomObjects, and serialize it
$array = [pscustomobject]@{ objectName = "object1Name"; objectValue = "object1Value" },
[pscustomobject]@{ objectName = "object2Name"; objectValue = "object2Value" }
# Serialize the array to a text file
$filePath = Join-Path $TESTDRIVE test.json
$array | ConvertTo-Json | Out-File $filePath -Encoding utf8
# Read the object as an array of PSObjects and deserialize it.
$result = Get-Content $filePath | ConvertFrom-Json
$result.Count | Should -Be 2
}

image

@daxian-dbw daxian-dbw merged commit ec0dba4 into PowerShell:master Jul 25, 2022
@daxian-dbw daxian-dbw assigned daxian-dbw and unassigned anmenaga Jul 25, 2022
@strawgate
Copy link
Contributor Author

Great! Thank you!

@daxian-dbw
Copy link
Member

Thank you, @strawgate, for the contribution!

@ghost
Copy link

ghost commented Aug 12, 2022

🎉v7.3.0-preview.7 has been released which incorporates this pull request.:tada:

Handy links:

@TravisEz13 TravisEz13 mentioned this pull request Sep 30, 2022
22 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log Extra Small

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants