Re:Reducing Memory Usage in Ruby
by igravious on Tuesday January 23, 2018 @06:43PM
(#16219009)
What I'd like to be able to do is load ActiveRecord (or any other v. heavy lib) asynchronously in non web-based apps so that I can paint the UI quicker. By the time the user will do/need anything db-related a proxy object should be swapped out for an actual ActiveRecord object. Using Rails' ORM is so nice and ergonomic but it kills start-up time with all the file requires, Bootsnap helps a bit but I'd prefer not to have to use it as it is yet one other moving part to consider.
[ Parent
| on HN ]
I don’t quite understand why this is a problem. You pay the ActiveRecord boot time once, at app start, and then it’s no longer an issue.
I am actually facing a similar problem. I also find that Rails just creates a db connection everytime I do Model.new even though I have not yet saved it.
This is because active record models learn their attributes by querying the database scheme. I haven’t tried it but there appears to be a way to cache this information in the local file system which would likely avoid the early dB connect if you really needed to do that. Also avoids querying the scheme on processs boot entirely if you are willing to trust the cached scheme matches the. database scheme. See https://github.com/rails/rails/issues/24133 for discussion