Using code_interface makes resource slow to compile

I have some pretty big resources in my code base that takes more than 10 seconds to compile, after profiling it a bit, I noticed that the compile time would go down a lot if I remove the code_interface block from it.

For example, one of my resources have this code_interface:

    code_interface do
      define :create
      define :create_with_lead
      define :create_live_transfer
      define :create_fake
      define :create_with_photo_upload

      define :update
      define :photo_requred_now
      define :send_photo_upload_request
      define :request_photo_upload_from_seller
      define :modify_appointment

      define :cancel
      define :cancel_fake

      define :reschedule

      define :get
      define :get_admin
      define :get_own
      define :get_by_lead
      define :get_by_property_registry
      define :get_by_attom_id

      define :list_own
      define :list_between_dates
      define :list_admin
      define :list_by_address

      define :priors
      define :sign_appointment
    end

When compiling the resource with the code_interface, it would take around 11 seconds:

`[profile] 11260ms compiling + 0ms waiting while compiling lib/core/marketplace/ap/appointment.ex`

If I remove the code_interface, then it goes down to 2 seconds:

`[profile] 2270ms compiling + 0ms waiting while compiling lib/core/marketplace/ap/appointment.ex`

I also tried to move the code interface to the domain:

      resource Ap.Appointment do
        define :create_appointment, action: :create
        ...
        define :sign_appointment, action: :sign_appointment
      end

With this in the domain, the domain took 6 seconds to finish:

[profile] 6397ms compiling + 0ms waiting while compiling lib/core/marketplace/ap.ex

And without it, it would take only 285ms:

[profile] 285ms compiling + 0ms waiting while compiling lib/core/marketplace/ap.ex

So, here is the question, is this expected? Can this be optimized in some way? It took me by surprise how slower the compilation is when having a code interface (either in the resource or in the domain).

I’m literally considering remove all code interfaces from my code base to make it compile faster, but I would like to know if this is just a optimization “bug” that can be fixed or it is expected.

Wow, that is pretty damn hefty!

Honestly I’d be willing to bet that we can optimize this. Could you perhaps put together a sample project showing these slow compile times? I could go in and hunt down problems and clean things up. One such example is that the code interface logic is not really the greatest macro code anyone ever wrote, I wrote it pretty early on and I know a lot more than I did back then :laughing:

Here it is @zachdaniel GitHub - sezaru/test_slow_code_interface · GitHub

To test it, you need to compile with profile set, basically run mix compile –profile time

Then, in the lib/test/my_domain.ex you can change the if to enable or disable the code interface definitions.

You can also do the same with the resource in lib/test/my_domain/my_resource.ex

Here are my numbers:

MyResource with code_interface:

[profile] 4148ms compiling + 0ms waiting while compiling lib/test/my_domain/my_resource.ex

MyResource without code_interface:

[profile] 533ms compiling + 0ms waiting while compiling lib/test/my_domain/my_resource.ex

MyDomain with code_interface:

[profile] 3719ms compiling + 0ms waiting while compiling lib/test/my_domain.ex

MyDomain without code_interface:

[profile] 368ms compiling + 0ms waiting while compiling lib/test/my_domain.ex

hey @zachdaniel any insights regarding this? Were you able to reproduce the issue with the project I created in the post above?

I’m hoping to look into this this weekend.

@sezaru should be some pretty significant improvements in main of ash. Please give it a shot and let me know the results :smiley:

For us trying out main was a massive improvement, from ~32s to ~9s, thanks @zachdaniel

Working great for the as-well! From 18 seconds in a resource to around 6s. Thanks again :slight_smile: