Suggestion: Migrating from ActiveModelSerializers to Alba

AI-generated summary

In a discussion titled “Suggestion: Migrating from ActiveModelSerializers to Alba,” okuramasafumi proposes replacing Discourse’s outdated ActiveModelSerializers (AMS) with Alba, a faster, AMS-like JSON serializer they maintain. They note Alba is ~10x faster than AMS, though not fully compatible. Falco and sam agree to consider the switch but emphasize the need for benchmarks and minimal plugin disruption. Sam recommends testing on topic/topic list serializers and using discourse bench for performance metrics, noting ecosystem compatibility is critical. okuramasafumi acknowledges plugin compatibility concerns, citing a plugin that uses AMS’s include_votes? method — a “magic” method not found in the codebase. pfaffman suggests add_to_serializer as a potential workaround, though later admits it may not fully apply if new serializers are needed pfaffman. okuramasafumi confirms they’ll attempt to make Alba compatible with existing plugin patterns before proceeding.

Hi, I just noticed that Discourse uses older version of ActiveModelSerializer.
It was released five years ago and I don’t think it’s good.

It seems that it’s planned to fork AMS but there’s a better solution.
My suggestion here to migrate from AMS to Alba, a JSON serializer I created and maintain. Although Alba is not 100% compatible with AMS, the API it offers is similar to AMS and about 10 times faster than latest AMS.

If the maintainers agree on migration, I’ll send a PR.

Could you change 2 or 3 endpoints over to it in PR and run some benchmarks? That may help us to evaluate it :smiley:

Yes, as Falco mentioned we need to worry about 2 areas here:

  1. Plugin compatibility - many plugins extend serializers, we would like to minimize breaking changes

  2. Performance - changing serializers needs to be backed with hard data.

I recommend looking at some of the serializers involved in topic or topic list pages and using them as testing grounds:

Then use “before” and “after” figures of discoruse bench. It will give you a very accurate feel of the progress made.

The amount of ecosystem disruption we are open to accepting heavily depends on the performance improvement here.

Sorry for the late response.

I believe it makes performance improvements so I’ll run benchmark script.

I didn’t know about serializer plugins. I found one example of plugins that customize serializer.

Here, include_votes? method seems a magic method from AMS since I cannot find where it’s used in the codebase. I think we need some migration in such cases since Alba doesn’t support this behavior.

Keeping compatibility must be important, so if I cannot make Alba compatible with existing plugins, I prefer not moving on.

The magic you’re looking for is add_to_serializer. You don’t need to make a new serializer.

Maybe this will help: Add to serializer only for staff and current user - #7 by paresy

Ah, thanks, it looks like it just defines some helper methods that returns whether the plugin is enabled or not and the plugin body. I think I can make it compatible with Alba, let me try.

Sorry. I didn’t look closely enough at the original plugin. I think that my hint might not be as helpful as I thought.

If what you’re doing is adding something to existing serializers (like adding a field to the post serializer), then I’ve helped. If you need to add a new route that returns its own information, then you’ll need to do something else.