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

Switch to Panko serializers #22378

Open
dogelover911 opened this issue Dec 16, 2022 · 4 comments
Open

Switch to Panko serializers #22378

dogelover911 opened this issue Dec 16, 2022 · 4 comments
Labels
suggestion Feature suggestion

Comments

@dogelover911
Copy link
Contributor

Pitch

Mastodon poor performance is basically a meme. You would expect the bottleneck to be the database. Well, in my tests the culprit is actually the ActiveModel Serializer, which is not producing json fast enough AFTER retrieving from the database. That is funny and stupid.

The solution is to switch to Panko for serialization. This cuts response times up to 3x. Fortunately the API is quite similar to ActiveModel, and you can use both in a project while incrementally adopting it.

Motivation

Mastodon is faster for users, the world no longer suffers the carbon emissions of ActiveModel Serializer.

@dogelover911 dogelover911 added the suggestion Feature suggestion label Dec 16, 2022
@jwusch
Copy link

jwusch commented Dec 17, 2022

Can you post your detailed stats to backup your claim? Core infra changes are hard, but if you can give concrete numbers it helps with your suggestion

@bneutra
Copy link

bneutra commented Dec 17, 2022

not that it's my business, but I found https://panko.dev/docs/performance; I know those are just benchmarks, implementing and testing might be a fair bit of work (do you have a way to benchmark commonly used methods?)

@darronschall
Copy link

I poked around with this in main...darronschall:mastodon:panko-benchmark-tag-serializer

I focused on the TagSerializer without a current_user (thus omitting the "following" flag). Running rspec ./spec/serializers/tag_serializer_spec.rb with my branch checked out shows the AMS and Panko version produce the same output, with the Panko version being slightly faster.

             user     system      total        real
ams:     0.012578   0.004628   0.017206 (  0.036755)
panko:   0.012644   0.003973   0.016617 (  0.036245)

I suspect the results will be more dramatic when more work is done. The real test will be converting REST::StatusSerializer, but that's a larger effort than I had time for this morning.

@darronschall
Copy link

darronschall commented Dec 18, 2022

Quick addendum. I added another benchmark to use Panko's serialize_to_json which uses Oj::StringWriter internally. This version is the fastest using my branch (repeating the tag serializing 25 times in the benchmark):

                  user     system      total        real
ams:          0.013254   0.002893   0.016147 (  0.031433)
panko:        0.013079   0.002764   0.015843 (  0.030981)
panko (oj):   0.011171   0.002312   0.013483 (  0.028908)

Your results may vary. I have an M1 MacBook Pro.

All that said, I'm leaning towards pursuing this a little bit further and publishing my TagSerializer conversion as a PR for further discussion. I couldn't find any specs that validate the actual API responses though? I see spec/controllers/api/v1/tags_controller_spec.rb but that doesn't validate the API's JSON response. AFAICT, there are no requests spec... I expected to find them in spec/requests/api/v1, so I'll have to write some first to ensure I don't break the API in the process.

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

No branches or pull requests

4 participants