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

Failure to derive typeclass for large sum types on Scala 3 #565

Open
ghostdogpr opened this issue Jan 27, 2025 · 3 comments
Open

Failure to derive typeclass for large sum types on Scala 3 #565

ghostdogpr opened this issue Jan 27, 2025 · 3 comments

Comments

@ghostdogpr
Copy link

If you have a sealed trait with lots of subtypes (~1000 in my case, but the issue happens with much less), Magnolia runs into max-inlines issues or even triggers a compiler stack overflow.

This comes from the fact that the subtypesFromMirror method is not tail-recursive. It folds over the list of subtypes and increases the number of nested inline function calls and the stack depth proportionally to the number of subtypes.

See this example in Caliban on how it can be made tail-recursive.

On top of that, subtypesFromMirror calls .distinctBy(_.typeInfo).sortBy(_.typeInfo.full) on every iteration, which is very inefficient with large list of subtypes. This should be moved at the end of the loop.

@adamw
Copy link
Member

adamw commented Jan 27, 2025

Indeed :) Would you be able maybe to work on a fix?

@ghostdogpr
Copy link
Author

Indeed :) Would you be able maybe to work on a fix?

Not right now, I'm in the middle of a large Scala 3 migration and I ended up not using Magnolia since I could do it more simply. But I thought I'd share my findings anyway, hopefully it's relatively simple!

@adamw
Copy link
Member

adamw commented Jan 27, 2025

Sure thing, thanks :)

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

No branches or pull requests

2 participants