Skip to content

System.Text.Json-powered IContentSerializer - #836

Merged
clairernovotny merged 37 commits into
reactiveui:masterfrom
Sergio0694:feature/system-text-json
Mar 23, 2020
Merged

clairernovotny merged 37 commits into
reactiveui:masterfrom
Sergio0694:feature/system-text-json

Conversation

@Sergio0694

@Sergio0694 Sergio0694 commented Feb 8, 2020 •

Copy link
Copy Markdown
Contributor

What kind of change does this PR introduce?

This PR introduces a few changes discussed in #832.

  • Remove the net461 target
  • Add a System.Text.Json-powered IContentSerializer type

What is the current behavior?

refit currently only includes a single IContentSerializer type, which is powered by Newtonsoft.Json.

What is the new behavior?

This PR introduces a new performance-oriented serializer, using the System.Text.Json APIs.

What might this PR break?
The new IContentSerializer is not 100% at feature parity with the one using Newtonsoft.Json (see here for the comparison), but since it's completely optional and up to individual users to use it, there are no breaking changes caused by that.

The other main changes is the removed net461 target.

Please check if the PR fulfills these requirements

  • Tests for the changes have been added (for bug fixes / features)
  • Docs have been added / updated (for bug fixes / features)

Other information:

This is the current tests situation after trying to simply drop-in replace the Newtonsoft.Json-powered IContentSerializer with the new one, without doing any other changes:

image

As mentioned before, some differences are to be expected (eg. System.Text.Json doesn't support JSON properties/values using the single quote ' as a delimiter). Still though, just 16 failing tests out of 240 with a first attempt look promising 🚀

cc. @onovotny - let me know how you think we should proceed from here, whether we want to add new tests for the new serializer, and if so how to properly structure them, etc. 😊

@clairernovotny

Copy link
Copy Markdown
Member

Please don't remove the net461 target. I don't think there's any reason to and that will let .NET Framework users keep using the library.

System.Text.Json supports net461 as well.

@clairernovotny

Copy link
Copy Markdown
Member

We should have tests for the new IContentSerializer, but I think they should be more around ensuring that the correct implementation is used where expected, not on proving serialization/deserialization itself works.

@Sergio0694

Copy link
Copy Markdown
Contributor Author

@onovotny Ah, my bad, when you said you'd have no problem bumping the library to .NET Standard 2.0 I thought you also meant to drop net461 by that 😅
I'll revert that commit - if System.Text.Json supports both there's no problem anyway!

As for tests, what do you mean by that exactly? Also, we plan to leave Newtonsoft.Json as default, right?
I'm currently doing some benchmarks to see if I can come up with some more performance improvements for the new serializer, then I'll be able to help with the new tests if you give me some guidance on how you'd like to structure them.

Thanks again for your time! 😊

@clairernovotny

Copy link
Copy Markdown
Member

I mean we should have tests that ensure that if the new IContentSerializer is specified in the config, that it's used later on. This might partially mean relying on subtle behavioral differences in the expected JSON so that we can run the test twice. Once with the defaults to get the result we expect with Newtonsoft.Json and again with the new IContentSeralizer to get the result we expect from System.Text.Json.

Comment thread Refit/IO/PooledMemoryStream.cs Resolved
@clairernovotny

Copy link
Copy Markdown
Member

Are there other areas in the code base that can benefit from using the PooledMemoryStream as well?

@Sergio0694

Copy link
Copy Markdown
Contributor Author

Not sure, will have to take a look. Also note that one of the reasons why the PR is still draft is that I'm not yet fully committed to that PooledMemoryStream type, I want to run some more benchmarks with a memory profiler to see if there are some changes I can make there. Looking at the System.Text.Json sources in CoreCLR it looks like the serialization overload working on a Stream allocates some non-pooled arrays internall, and if possible I'd like to avoid that. I'm going to write another type implementing IMemoryBuffer<T> instead and compare that against the current implementation.

Once I have the final set of APIs laid down and test we can figure out if there are some other places in the lib where we could reuse those, absolutely 😊

@Sergio0694

Sergio0694 commented Feb 9, 2020 •

Copy link
Copy Markdown
Contributor Author

@onovotny Alright, I've finished running some benchmarks and came up with a more optimized version of the System.Text.Json-based IContentSerializer. Here's a run with a sample JSON response:

|          Method |     Mean |     Error |    StdDev | Ratio |   Gen 0 |   Gen 1 |   Gen 2 | Allocated |
|---------------- |---------:|----------:|----------:|------:|--------:|--------:|--------:|----------:|
|  NewtonsoftJson | 1.568 ms | 0.0010 ms | 0.0009 ms |  1.00 | 91.7969 | 29.2969 | 29.2969 | 403.22 KB |
|  SystemTextJson | 1.073 ms | 0.0017 ms | 0.0015 ms |  0.68 | 13.6719 |  1.9531 |       - |  62.09 KB |

We have a ~30% speedup, and a 6.5x memory usage reduction 🚀

Can I remove this property and the other strong Newtonsoft.Json references from the RefitSettings class now that more options are available?
That property has already been marked as Obsolete for a while anyway 🤔

public JsonSerializerSettings JsonSerializerSettings
{
get => jsonSerializerSettings;
set
{
jsonSerializerSettings = value;
ContentSerializer = new JsonContentSerializer(value);
}
}

@Sergio0694

Copy link
Copy Markdown
Contributor Author

Oof, I just realized I had tagged the wrong person!
I meant to tag @clairernovotny in my previous comment, my bad 😅

I just have that remaining open question from that comment, then this PR is ready for review 🚀

@clairernovotny

Copy link
Copy Markdown
Member

I'd prefer we don't remove it yet as that'd necessitate a major version bump. We can do it for next time. Please bump the minor version up to 5.1 in the version.json.

@clairernovotny

Copy link
Copy Markdown
Member

Can you please add tests for the new serializer and the integration points to ensure that the correct serliazer is used?

@richbryant

Copy link
Copy Markdown

many of us working the various forms of mono-wasm implementation have discovered that System.Text.Json relies quite heavily on Emit functionality where Json.NET does not, meaning System.Text.Json either throws odd errors or does not function at all under mono-wasm.

This is not to say "don't do this". Just "be aware that...".

@Sergio0694

Copy link
Copy Markdown
Contributor Author

@clairernovotny Sure thing, will do! Thanks for the feedbacks 😊

@richbryant Are you sure about that? I'm not seeing any reference to the *.Emit namespace in the source code (see here), plus I know that the new System.Text.Json APIs also work on UWP, which in Release mode uses .NET Native which is 100% AOT, and doesn't support any dynamic code generation feature as far as I know. Maybe there's something else going on there, or it's using some other execution mode that relies on code generation? 🤔

@richbryant

Copy link
Copy Markdown

@Sergio0694 that could be it. But something's up with it. I don't think Refit is huge in the Webassembly world right now, largely because of some godawful changes the Blazor team pushed with 3.1 that make the httpclient settings essentially hardcoded into the framework - you can get around this but you have to know what you're doing and jump through a lot of hoops - but as a C# based system which relies on REST API calls, I expect it will definitely become so once this is fixed.

@Dreamescaper

Copy link
Copy Markdown
Contributor

I don't mean to remove classes themselves.
Take a look at Obsolete message that I shared above - it suggest using JsonContentSerializer, which becomes obsolete itself.

@Sergio0694

Copy link
Copy Markdown
Contributor Author

Oh, no, I wasn't talking about removing the classes either, just that property there.
I see what you were saying now, and yeah it makes sense 👍

Done in 8cd2984.

@clairernovotny

Copy link
Copy Markdown
Member

Once we're good on this PR, I can get 5.1 pushed out.

@Sergio0694

Copy link
Copy Markdown
Contributor Author

@clairernovotny Awesome! 😄
I'd say it looks good on my end, I just have that open question about the overrides for the Stream methods working on Span<byte> etc., but as I mentioned I think we'd need to multitarget to .NET Standard 2.1 in order to do that, and I'm not sure that's allowed for a minor update 🤔

If that's out of the picture for this PR, unless there are some other feedbacks or suggested changes (which would be more than welcome), the current code should be alright 👍

@clairernovotny

Copy link
Copy Markdown
Member

We can add a netstandard2.1 target, just not remove.

@Sergio0694

Copy link
Copy Markdown
Contributor Author

I've added the .NET Standard 2.1 target and the overrides that @benaadams mentioned in ab24dff. I think we should now be covered for whatever method will the System.Text.Json APIs call on this Stream behind the scenes. If I missed anything, please let me know! 😊

@clairernovotny

Copy link
Copy Markdown
Member

Okay, final thing -- can you please document this in the readme.md so people will know that it exists and how to choose which JSON serializer to use?

@Sergio0694

Copy link
Copy Markdown
Contributor Author

Done in 6f69cb8, let me know if that's fine or if you'd like me to make any other changes 😊

@clairernovotny
clairernovotny merged commit c2e2c92 into reactiveui:master Mar 23, 2020
@Sergio0694
Sergio0694 deleted the feature/system-text-json branch March 23, 2020 11:05
@lock lock Bot locked and limited conversation to collaborators Jun 24, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feature: Enable choice between Newtonsoft.Json and System.Text.Json

5 participants