Skip to content

Unexpected code size increase when using JSON Source Generator #56995

Closed

Description

@mrange

Hi.

I tried the JSON Source Generator as asked too. I was surprised to find that my assembly grew from 7 KiB to 125 KiB after adding source generators even though I only generated code for Person and List<Person>.

  public partial record Person
  {
    public int                  Id                             { get; set; }
    public string               FirstName                      { get; set; }
    public string               LastName                       { get; set; }
  }

  [JsonSerializable(typeof(Person)      , GenerationMode = default)]
  [JsonSerializable(typeof(List<Person>), GenerationMode = default)]
  partial class MyJsonContext : JsonSerializerContext
  {
  }

When looking at the generated code I found a bunch of generated methods that I didn't use and that should be possible to have in a shared library such AssemblySerialize.

It's not really a bug but I felt 120 KiB was a bit too much, especially in a world where we try to squeeze down the size for Blazor WASM Clients.

I wrote a bit about my experience with using JSON source generator here: https://github.com/mrange/T4JsonSerializer

Regards,
Mårten

Activity

  1. ghost added
    untriaged
    on Aug 6, 2021
  2. layomia commented on Aug 6, 2021

    @layomia
    Contributor

    Thanks for the feedback & analysis of the generator's performance. I believe the size issue is resolved following #56354. The generator was including a bunch of auto-generated members on record types, but those should now be excluded following this PR. You'll be able to try the fix in a couple hours when we have a new build of System.Text.Json - https://dev.azure.com/dnceng/public/_packaging?_a=package&feed=dotnet6&view=versions&package=System.Text.Json&version=6.0.0-rc.1.21406.4&protocolType=NuGet. Pls let us know if the issue is resolved.

  3. removed
    untriaged
    on Aug 6, 2021
  4. self-assigned this
    on Aug 6, 2021
  5. added this to the 6.0.0 milestone on Aug 6, 2021
  6. mrange commented on Aug 7, 2021

    @mrange
    Author

    @layomia Hi. Tried it but after updating the reference my code no longer compiles. It almost seems like the source generator hasn't generated any code as it complains that: error CS0534: 'MyJsonContext' does not implement inherited abstract member 'JsonSerializerContext.GetTypeInfo(Type)'

    I tried faking the method but then I got other errors.

    It could be because you made some changes to how to use the source generator but I wasn't aware of those.

    If you are interested see my PR: https://github.com/mrange/T4JsonSerializer/pull/1/files#diff-7f0adf85cf5c2c4802b4e4849b49eb3fd63de1852d53df61da98fadf46604e6fR10

    (It also seems the new version has updated transitive dependencies but I downloaded them as well so I hope that's good)

  7. mrange commented on Aug 7, 2021

    @mrange
    Author

    I also tried the version you linked but with the same results.

  8. mrange commented on Aug 9, 2021

    @mrange
    Author

    Tried system.text.json.6.0.0-rc.1.21409.2 as well and still seems to me that the source generator isn't executed. Perhaps I am doing something wrong?

  9. am11 commented on Aug 9, 2021

    @am11
    Member

    @mrange, could you try with the daily build of whole sdk / shared framework (https://github.com/dotnet/installer#installers-and-binaries first column in the table)?

    On my Ubuntu box, I did:

    mkdir ~/.dotnet6
    curl -sSL https://aka.ms/dotnet/6.0/daily/dotnet-sdk-linux-x64.tar.gz | tar xzf - -C ~/.dotnet6
    ~/.dotnet6/dotnet --version
    # 6.0.100-rc.1.21409.2
    
    cd T4JsonSerializer/src
    cat > nuget.config <<EOF
    <configuration>
      <packageSources>
        <add key="dotnet6" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet6/nuget/v3/index.json" />
      </packageSources>
    </configuration>
    EOF
    
    ~/.dotnet6/dotnet publish -c Release -r linux-x64 --self-contained

    it cuts down the size from (128K and 8.0K from last week's build) to:

    16K	CsSourceGeneratorsJson.dll
    8.0K	CsNoSourceGeneratorsJson.dll
    
  10. mrange commented on Aug 9, 2021

    @mrange
    Author

    Thanks for the tips, I managed to build it on Ubuntu in WSL and when inspecting the binaries it looks much better. I no longer see AssemblyWrite and other unrelated metadata. There is metadata generated for Int32 and String which I guess could be in a common assembly but it doesn't take much space.

  11. danmoseley commented on Aug 9, 2021

    @danmoseley
    Contributor

    @mrange now you have it working, we'd value any other feedback you may have (if bugs/suggestions then probably as different issues)

    Would it make sense to close this one now?

  12. mrange commented on Aug 9, 2021

    @mrange
    Author

    Feel free to close this one.

  13. ghost locked as resolved and limited conversation to collaborators on Sep 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions