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

Support custom deserialization #658

Open
vicero opened this issue Sep 19, 2019 · 7 comments
Open

Support custom deserialization #658

vicero opened this issue Sep 19, 2019 · 7 comments

Comments

@vicero
Copy link

vicero commented Sep 19, 2019

I'd like to use my own optimized JSON deserializer against Algolia, avoiding the performance hit from reflection and garbage collection in Newtonsoft.

One possible solution to this would be to allow injection of HttpTransport into the SearchClient constructors. HttpTransport would need to go from internal to public as a consequence.

@Ant-hem
Copy link
Member

Ant-hem commented Sep 20, 2019

Hi @vicero

Thanks for the feedback. This is definitely something I wanted to address in the next version of the library since .NET Core 3 will be shipped with a new (and performant) std serializer. (related to #652)

Regarding the implementation, I was more thinking of only injecting the serializer and not the whole transport layer because the latter is responsible for the retry strategy (which is one of the most important part of the library). I will design a simple interface for the serializer. I'll keep you updated on this issue when we come up with something.

However, in the mean time you can still improve a bit the performance of Newtonsoft with Manual Serialization - Using a reader or writer directly skips any of the overhead from a serializer, such as reflection. You could use it on your POCOs.

Hope this helps.

@vicero
Copy link
Author

vicero commented Sep 20, 2019

However, in the mean time you can still improve a bit the performance of Newtonsoft with Manual Serialization - Using a reader or writer directly skips any of the overhead from a serializer, such as reflection

Indeed, I tried this (but with a JsonConverter attribute) but there is still overhead according to the profiler.

image

I cannot set the contract resolver in the Algolia C# client because the JsonConfig is internal.

@Ant-hem
Copy link
Member

Ant-hem commented Sep 20, 2019

What kind of performance issues are you facing? Memory/speed or both?
Are you facing this issue at Indexing time or at Search time?

@vicero
Copy link
Author

vicero commented Sep 20, 2019

Speed, you can see in the profiler image above that even with the JsonConverter attribute, newtonsoft is spending a lot of time doing reflection and garbage collection

This is happening on search, just deserializing the result of the http response.

@Ant-hem
Copy link
Member

Ant-hem commented Sep 20, 2019

Alright. I'll work on the injection of the serializer in the coming days.

In the meantime, another work-around could be to reduce the size of the response with the responseFields and attributesToRetrieve parameters.

If the tip above didn't work. Would it be possible to have the full dump of the profiler? (If it does not contain any sensitive information indeed). You could share it through our support page mentioning this thread. If you could share a reproducible code snippets + application and index name it would be perfect as well!

@vicero
Copy link
Author

vicero commented Jan 21, 2020

Sorry for the late response, but I noticed some movement on deserialization!

In the meantime, another work-around could be to reduce the size of the response with the responseFields and attributesToRetrieve parameters.

I initially had the same idea, however, unfortunately I'm not able to minimize either of those.

I came across this recent article from October 2019 about serializer benchmarks in dotnet 3.0 to hopefully share why I am interested in injecting my own serializer into the algolia client:

https://michaelscodingspot.com/the-battle-of-c-to-json-serializers-in-net-core-3/

Even the latest standard serializer from dotnet is not the most performant, in some cases it's even worse than Newtonsoft.

Hopefully injection of the serializer is still on the horizon, we continue to see spikes in our response times grow under load and tailoring deserialization is the only solution to this.

@morganleroi
Copy link
Contributor

Hello !

We just released a new version of the client (Alpha) on Nuget.

This version now use System.Text.Json as Serializer. If needed, we could also allow customer to inject their own logic if removing Newtonsoft did not solve the issue.

If you have time, we need customer feedback about this new version. It's still not GA so you may not want to use it in production.

There is no more deps on Newtonsoft, all Algolia APIs are now available (Insights, Recommend, Query Perso, ...) and all models and routes are synced.

To help you migrate, there is a Migration guide.

Happy to help in your migration process !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants