Skip to content

Json generator should hoist property values in serialization fast-path when ignore condition on write is set #80703

Closed

Description

@Sergio0694

Description

Just noticed a minor missed optimization opportunity in the source generated serialization fast-path while going over some code in the Store. Consider this example where I have a model that has a property that allocates something on every read:

[JsonSerializable(typeof(MyModel))]
[JsonSourceGenerationOptions(DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)]
public partial class MyJsonContext : JsonSerializerContext
{
}

public class MyModel
{
    public string SomeAllocatingProperty => $"The current time is: {DateTime.Now}";
}

The serialization fast-path is currently this one:

private void MyModelSerializeHandler(global::System.Text.Json.Utf8JsonWriter writer, global::MyModel? value)
{
    if (value == null)
    {
        writer.WriteNullValue();
        return;
    }

    writer.WriteStartObject();
    if (value.SomeAllocatingProperty != null)
    {
        writer.WriteString(PropName_SomeAllocatingProperty, value.SomeAllocatingProperty);
    }

    writer.WriteEndObject();
}

Note the repeated read of value.SomeAllocatingProperty. The generator could improve this if:

  • Ignore condition is to ignore null/default values on write
  • The property is of a nullable or reference type

And change that branch to instead be:

PropertyType __value_SomeAllocatingProperty = value.SomeAllocatingProperty;
if ((object)__value_SomeAllocatingProperty != null)
{
    writer.WriteString(PropName_SomeAllocatingProperty, __value_SomeAllocatingProperty);
}

Using legacy style syntax here for backwards compat, but of course you could also use value.Prop is { } propValue and use that local.

Configuration

  • System.Text.Json 7.0.1

Regression?

No.

Activity

  1. added
    tenet-performance
    on Jan 16, 2023
  2. ghost added
    untriaged
    on Jan 16, 2023
  3. eiriktsarpalis commented on Jan 17, 2023

    @eiriktsarpalis
    Member

    Playing devil's advocate, this would impact readability to account for the rare case where you have a DTO that isn't using auto properties.

    The generator could improve this if:

    • Ignore condition is to ignore null/default values on write
    • The property is of a nullable or reference type

    I'm pretty sure that the source generator specializes generated code depending on this configuration. If we've missed something, we'd definitely accept a PR that adds it.

    if ((object)__value_SomeAllocatingProperty != null)

    Why is the cast to object necessary here? I'm pretty certain that the compiler emits optimal code without it.

  4. removed
    untriaged
    on Jan 17, 2023
  5. added this to the Future milestone on Jan 17, 2023
  6. added
    help wanted
    on Jan 17, 2023
  7. Sergio0694 commented on Jun 17, 2023

    @Sergio0694
    ContributorAuthor

    "Why is the cast to object necessary here? I'm pretty certain that the compiler emits optimal code without it."

    Whoops, missed this message. The cast is needed to make sure to do a reference comparison with null even in the case where the type of that property has a custom equality operator implemented. If we can use new C# syntax, of course, we can also just do is not null (or is { }), which also always ignore any custom equality operators, even if present 🙂

    Edit: I suppose since the existing behavior is (by design?) not doing this, we might leave it as is, or address it separately.

  8. eiriktsarpalis commented on Jun 19, 2023

    @eiriktsarpalis
    Member

    I think is not would have been the best way forward, but there's already a discussion about what the minimal supported language version should be (IIRC is not patterns are newer than C# 7.3)

    I suppose since the existing behavior is (by design?)

    Probably not, but then again I wouldn't worry too much about operators where != null has different meaning than expected.

  9. eiriktsarpalis commented on Jun 19, 2023

    @eiriktsarpalis
    Member

    Fixed by #87725.

  10. modified the milestones: Future, 8.0.0 on Jun 19, 2023
  11. ghost locked as resolved and limited conversation to collaborators on Jul 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.Text.Jsonhelp wantedtenet-performance

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions