X Tutup
The Wayback Machine - https://web.archive.org/web/20220814024719/https://github.com/PowerShell/PowerShell/pull/15684
Skip to content
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

Remove pre-parse for array-based JSON #15684

Merged
merged 2 commits into from Jul 25, 2022

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

iSazonov commented Jun 29, 2021

Have we direct test for the scenario?

@iSazonov iSazonov added the CL-General 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

strawgate commented Jun 29, 2021

Have we direct test for the scenario?

I added a test for this scenario

@iSazonov iSazonov requested review from TravisEz13 and SteveL-MSFT Jun 30, 2021
@msftbot msftbot bot added the Review - Needed label Jul 7, 2021
@msftbot
Copy link

msftbot bot 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

strawgate commented Jul 13, 2021

I believe that this is ready to merge.

@strawgate
Copy link
Contributor Author

strawgate commented Aug 10, 2021

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

@anmenaga anmenaga closed this Nov 30, 2021
@msftbot msftbot bot removed the Review - Needed label Nov 30, 2021
@anmenaga anmenaga reopened this Nov 30, 2021
@pull-request-quantifier
Copy link

pull-request-quantifier bot commented Nov 30, 2021

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? 👍  👌  👎 (Email)
Customize PullRequestQuantifier for this repository.

@anmenaga
Copy link
Contributor

anmenaga commented Nov 30, 2021

Reopened PR to reset hanging PR checks.

@anmenaga
Copy link
Contributor

anmenaga commented Nov 30, 2021

/azp run

@azure-pipelines
Copy link

azure-pipelines bot commented Nov 30, 2021

Azure Pipelines successfully started running 5 pipeline(s).

@msftbot msftbot bot added the Review - Needed label Dec 8, 2021
@msftbot
Copy link

msftbot bot 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

daxian-dbw commented Jul 25, 2022

/rebase

@msftbot msftbot bot removed the Review - Needed 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

daxian-dbw commented Jul 25, 2022

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
62 of 64 checks passed
@daxian-dbw daxian-dbw assigned daxian-dbw and unassigned anmenaga Jul 25, 2022
@strawgate
Copy link
Contributor Author

strawgate commented Jul 26, 2022

Great! Thank you!

@daxian-dbw
Copy link
Member

daxian-dbw commented Jul 26, 2022

Thank you, @strawgate, for the contribution!

@msftbot
Copy link

msftbot bot commented Aug 12, 2022

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

Handy links:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CL-General Extra Small
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants
X Tutup