square/anvil

Question about runtime performance difference between @MergeSubcomponent and @ContributesSubcomponent

HylkeB opened this issue · 2 comments

Hi, I am in the process of migrating to anvil, and in my current implementation I have defined every feature as a @ContributesSubcomponent.

However when looking closer to the documentation it states the following:

Similar to MergeSubcomponent, but with the main difference that the subcomponent is generated and modules, bindings and component interfaces are merged whenever the component with parentScope is processed.

So I was wondering, it feels like my current startup time of my app is slower than it was before anvil, but I havent been able to reliably measure the differences. So could this bit of documentation indicate that indeed using @ContributesSubcomponent increases app startup time as opposed to @MergeSubcomponent, because it will have to generate all the feature subcomponents immediately at app startup?

In my case I have around 30 feature subcomponents that don't necessarily need to be created at app startup, but only just as you go and you encounter a feature they would need to be created.

Could this explain my feeling that the startup time of my app has increased? Or does the documentation only relate to compile time generation of the components? And are there other considerations to make when deciding between @ContributesSubcomponent and @MergeSubcomponent?

What I like about @ContributesSubcomponent as opposed to @MergeSubcomponent is that you don't need to wait for a build which generates the dagger classes.

There is no runtime difference, but there could be a difference at compile time. It could be that the size of your subcomponent grows because it may get generated in a different module and picks up more contributions, but this depends on your codebase and dependency graph.

What I like about @ContributesSubcomponent as opposed to @MergeSubcomponent is that you don't need to wait for a build which generates the dagger classes.

I don't understand this part. Anvil and Dagger will still generate all code, but maybe in a different module and different compilation step.

Thanks for your reply, glad to know it doesn't make a runtime performance difference.

And I can understand your comment, looking further into the samples it appears my comment doesn't make sense. I thought that just like @MergeComponent generates a DaggerMyComponent class, the same would be true for @MergeSubcomponent, but of course thats not the case. My bad.