Skip to content

Defer the inclusion until after ActiveRecord has been fully loaded, i…#8782

Open
alexsmartens wants to merge 1 commit intoactiveadmin:masterfrom
alexsmartens:master_lazy-load-activerecordbase-includes
Open

Defer the inclusion until after ActiveRecord has been fully loaded, i…#8782
alexsmartens wants to merge 1 commit intoactiveadmin:masterfrom
alexsmartens:master_lazy-load-activerecordbase-includes

Conversation

@alexsmartens
Copy link

Issue Description

The current implementation references ActiveRecord on gem load which triggers premature loading of Active Record. This can significantly slow down application boot time, especially in development and test environments.

Solution

Use Rails recommended ActiveSupport.on_load(:active_record) hook to defer module inclusion until after ActiveRecord is fully loaded. This improves boot performance and avoids potential load order issues in edge cases.

…mproving booting performance and following Rails best practices
@tagliala tagliala requested a review from Copilot August 9, 2025 12:45
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR defers the loading of Active Admin components until after ActiveRecord is fully loaded to improve application boot performance. The change uses Rails' ActiveSupport.on_load(:active_record) hook to avoid premature ActiveRecord loading during gem initialization.

Key Changes

  • Wraps router initialization in ActiveSupport.on_load(:active_record) callback
  • Defers load! and router application until ActiveRecord is ready
  • Uses explicit reference to ActiveAdmin.application within the callback

@codecov
Copy link

codecov bot commented Aug 9, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.08%. Comparing base (01f1430) to head (8b797de).
⚠️ Report is 16 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #8782   +/-   ##
=======================================
  Coverage   99.08%   99.08%           
=======================================
  Files         139      139           
  Lines        4043     4045    +2     
=======================================
+ Hits         4006     4008    +2     
  Misses         37       37           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@tagliala
Copy link
Contributor

tagliala commented Aug 9, 2025

Thank you for this PR!

I'm not sure if Copilot's suggestions are applicable here, and I don't have enough insight to provide a thorough review.

I'll leave to others with more expertise to review and make the merging decision

@alexsmartens
Copy link
Author

@tagliala I've just commented on Copilot's suggestions. They don't make sense.

Can you tag people who could potentially review the PR?

@tagliala
Copy link
Contributor

They don't make sense.

Strict but fair. I did also downvote the response and added the explanation, hoping that this would help

Can you tag people who could potentially review the PR?

Done

@javierjulio
Copy link
Member

@alexsmartens thank you. I’d like to see what the problem is first that is caused by not having this. It sounds serious so I wonder why we haven’t seen it reported before. We’ve done something similar so it’s not surprising to add something like this but it would be helpful to know and see what the issue is that it’s addressing. We generate dev and test apps locally in this repo but my guess it’s better to see this in an actual app so you may want to demonstrate the issue with our demo app which lives in its own repository.

@alexsmartens
Copy link
Author

@javierjulio I've been looking into optimizing my app boot up time for dev/test environments. The boot profiling indicated that most of the booting time comes from loading gems. active_admin was one of the slowest gems.

I tried to find any commonalities between the slow gems and one commonality is premature loading of ActiveRecord. Rails Guideline on this topic:

Rails is responsible for the load order of these frameworks, so when you load frameworks, such as ActiveRecord::Base, prematurely you are violating an implicit contract your application has with Rails. Moreover, by loading code such as ActiveRecord::Base on boot of your application you are loading entire frameworks which may slow down your boot time and could cause conflicts with load order and boot of your application.

That's when I identified the root cause of the issue and created this PR. In my app, this change reduced active_admin loading time from 755ms to 655ms

For reference, active_admin is still the slowest loading gem. I'll be happy to push up more changes if I do further optimizations

@tagliala
Copy link
Contributor

tagliala commented Sep 9, 2025

Hello,

sorry for the silence (but as I already mentioned I cannot do to much on this)

So this is a performance improvement rather than a bug fix and it also may allow us to fix an issue (that I can't replicate) when I use activeadmin together with an Active Record adapter different than the default one.

In fact, under some circumstances, we've seeing ActiveAdmin::DatabaseHitDuringLoad error. But again, I'm not able to replicate it (ifad/chronomodel#349)

I was trying to experiment myself against this branch to see if there are improvements. How do you measure Rails boot time?

I'm trying with time rails runner 'puts "hello"' but I cannot find differences on a small application

@alexsmartens
Copy link
Author

hi there,
just checking in, any plans to merge this?

@javierjulio
Copy link
Member

@alexsmartens no because you haven't replied with what we asked for. We'd like to be able to try this for ourselves to vet the change. Thank you.

@alexsmartens
Copy link
Author

Is there anything else other than #8782 (comment)?

@tagliala
Copy link
Contributor

Hello again,

I would like to test the performance improvements by myself. As I mentioned in my previous comment, I'm not familiar with benchmarks of rails boot, and my attempts to compare results between master and this branch were not successful (no visible gain on a small application)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants