Skip to content

Conversation

@jgonz120
Copy link
Contributor

@jgonz120 jgonz120 commented Feb 7, 2024

Bug

Fixes: NuGet/Home#12715

Regression? Last working version:

Description

Currently we use JObject to parse the assets file. This library reads the full file into memory causing performance issues with the GC. This PR switches it to use System.Text.Json, allowing up to read the file in pieces and parse it without having to load the full file into memory. This work was done in a feature branch and this is the PR to merge that feature branch into dev

PR Checklist

jgonz120 and others added 13 commits January 26, 2024 15:18
* Moved files over and addressed some PR comments

* added comment

* switched to true and false strings

* Added ctr to specify buffer for testing purposes.

* remove commented code

* switch to use Utf8 preamble for BOM

* Create method for checking complete

* combined code for ReadStringArray

* Updated buffer size to match STJ's default buffer size

* Switch Utf8JsonStreamReader to be disposable.

* Switch to read the value for numbers into a string directly

* revert back to using private var for utf8Bom

* Remove ReadStringArrayAsList

* Avoid referencing buffer after  returning

* Actually avoid referencing _buffer after returning

* Update how buffers are fed into Utf8JsonReader to avoid feeding extra empty data.

* remove extra line

* Reverted back to using try get int for ReadTokenAsString

* Update src/NuGet.Core/NuGet.ProjectModel/Utf8JsonStreamReader.cs

Co-authored-by: Andy Zivkovic <[email protected]>

* Remove ValueTextEquals taking in string

* Switched to Skip instead of TrySkip

* Update src/NuGet.Core/NuGet.ProjectModel/Utf8JsonStreamReader.cs

Co-authored-by: Andy Zivkovic <[email protected]>

* Added some unit tests

* fix Bom

* Switched to using Moq

* Update src/NuGet.Core/NuGet.ProjectModel/Utf8JsonStreamReader.cs

Co-authored-by: Andy Zivkovic <[email protected]>

* loop through stream when reading to ensure reading full bytes or to the end

* update signature comment

* Switched stream back to field and supress warning

---------

Co-authored-by: Andy Zivkovic <[email protected]>
…ng STJ (#5541)

Updated the logic for parsing the package spec to use STJ. Using a registry setting to determine if the code should use NJ or STJ for parsing.
…Json file using STJ. (#5558)

Update the LockFileFormat class to use STJ for parsing the assets file.
* Moved files over and addressed some PR comments

* added comment

* switched to true and false strings

* Added ctr to specify buffer for testing purposes.

* remove commented code

* switch to use Utf8 preamble for BOM

* Create method for checking complete

* combined code for ReadStringArray

* Updated buffer size to match STJ's default buffer size

* Switch Utf8JsonStreamReader to be disposable.

* Switch to read the value for numbers into a string directly

* revert back to using private var for utf8Bom

* Remove ReadStringArrayAsList

* Avoid referencing buffer after  returning

* Actually avoid referencing _buffer after returning

* Update how buffers are fed into Utf8JsonReader to avoid feeding extra empty data.

* remove extra line

* Reverted back to using try get int for ReadTokenAsString

* Update src/NuGet.Core/NuGet.ProjectModel/Utf8JsonStreamReader.cs

Co-authored-by: Andy Zivkovic <[email protected]>

* Remove ValueTextEquals taking in string

* Switched to Skip instead of TrySkip

* Update src/NuGet.Core/NuGet.ProjectModel/Utf8JsonStreamReader.cs

Co-authored-by: Andy Zivkovic <[email protected]>

* Added some unit tests

* fix Bom

* Switched to using Moq

* Update src/NuGet.Core/NuGet.ProjectModel/Utf8JsonStreamReader.cs

Co-authored-by: Andy Zivkovic <[email protected]>

* loop through stream when reading to ensure reading full bytes or to the end

* update signature comment

* Switched stream back to field and supress warning

---------

Co-authored-by: Andy Zivkovic <[email protected]>
…ng STJ (#5541)

Updated the logic for parsing the package spec to use STJ. Using a registry setting to determine if the code should use NJ or STJ for parsing.
…Json file using STJ. (#5558)

Update the LockFileFormat class to use STJ for parsing the assets file.
@jgonz120 jgonz120 requested a review from a team as a code owner February 7, 2024 18:24
@jgonz120 jgonz120 requested review from nkolev92 and zivkan February 7, 2024 22:59
Copy link
Member

@nkolev92 nkolev92 left a comment

Choose a reason for hiding this comment

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

Using STJ is the default correct?

If yes, then this looks great!

@zivkan
Copy link
Member

zivkan commented Feb 12, 2024

@jgonz120 the PR template has a section about docs. This PR introduces an environment variable, so an issue to add this new environment to the docs should be added: https://learn.microsoft.com/en-us/nuget/reference/cli-reference/cli-ref-environment-variables

@jgonz120 jgonz120 merged commit ef5bce3 into dev Feb 12, 2024
@jgonz120 jgonz120 deleted the dev-feature-jgonz120-parseLockfileWithStj branch February 12, 2024 19:04
@nkolev92
Copy link
Member

I linked the docs issue in the PR body.

@jgonz120 fyi, if you link the docs issue, the validate-release command will "catch" all the filled, but not completed docs issue, ensuring we've documented the features we're shipping.

@akoeplinger
Copy link
Contributor

@jgonz120 this seems to be causing two test failures in dotnet/sdk#38799

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.

Stop using JObject in assets file reading to reduce allocations

6 participants