ruby on rails - ActiveRecord Slow Object Instantiation -


i maintain website of events taking place in city.

my website homepage 5-days calendar events taking place today 4 days in future.

i have done quite job "activerecord" code have around ~120ms spent in mysql (double checked rackminiprofiler) load ~200-300 events.

however response time slow (1.5s - 2s).

most time spent in instantiation of ar's objects queries

using objectspace, see ~6k ar::base objects instantiated +300 events displayed.

the reason why there many objects event model has many associated models (e.g. venue, occurrences, categories, etc...), of contain bits of information need show.

as expected, profiling proved activerecord's object instantiation consuming task during request.

i not experienced enough in neither activerecord nor performance.

is such speed expected or should objects instantiated faster?

should move away ar , use simple ruby hashes?

is rails standard when data model complex?

========= update 1 =========

this pastebin contains service class use load events single day in calendar. hope it's understandable, did not have time document since it's still work in progress improve performance.

========= update 2 =========

loading these objects has drawback: causes gc runs while page being rendered, adding ~100ms after every n events rendered, becomes total overhead of ~500ms.

to give idea of how data i'm loading (sadly 99% of needed), if dump json 47k file.

========= update 3 =========

as mentioned @thesuper, though not improve ar's performance, fragment caching indeed friend i'm rendering quite amount of data while under gc runs. applying fragment caching yielded 1-1.2s improvement, huge.

however still cannot overcome 600ms wall of ar. possible improvement "selective includes" discussed in this answer, few cases need small portion of attributes of included model, ugly , inflexible.


Comments

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -