rocdaddy
2013-Mar-31 21:57 UTC
model object form helpers, instance variable life cycle, and garbage collection
Hey folks: In a rails app I''m building, the home page will have two forms: one to sign-up for an account, another one to sign-in to an existing account. Now, if I were to use object form helpers to build these forms, the controller for the homepage would need to create instances of the relevant models each time someone visits the homepage. This concerns me a bit, because a visitor to the home page may or may not end up using either the sign-up or sign-in forms. For instance, they may instead click a link that gives a "tour" of the app. If they do so, will the instance variables created by the controller in anticipation of the POTENTIAL use of the forms be garbage-collected? I''m just thinking about efficiency here -- don''t want to have a bunch of extra instance variables taking up space in the runtime memory just for the sake of the (slight) convenience of using model object forms helpers rather than plain-vanilla form handlers. Thanks! -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/cOuG0uV40sIJ. For more options, visit https://groups.google.com/groups/opt_out.
Frederick Cheung
2013-Apr-01 09:18 UTC
Re: model object form helpers, instance variable life cycle, and garbage collection
On Sunday, March 31, 2013 10:57:24 PM UTC+1, rocdaddy wrote:> Hey folks: > > In a rails app I''m building, the home page will have two forms: one to sign-up for an account, another one to sign-in to an existing account. > > Now, if I were to use object form helpers to build these forms, the controller for the homepage would need to create instances of the relevant models each time someone visits the homepage. > > This concerns me a bit, because a visitor to the home page may or may not end up using either the sign-up or sign-in forms. For instance, they may instead click a link that gives a "tour" of the app. If they do so, will the instance variables created by the controller in anticipation of the POTENTIAL use of the forms be garbage-collected? >They would be, along with many hundreds or thousands of other objects created for the request: strings, request related objects (parsed headers, cookies etc). A few model objects are neither here nor there in the grand of scheme. For me this falls into the category of premature optimization. If you really do end up serving millions of daily hits on your homepage then you''ll get a far bigger win from caching than from skimping on a few instance variables Fred> I''m just thinking about efficiency here -- don''t want to have a bunch of extra instance variables taking up space in the runtime memory just for the sake of the (slight) convenience of using model object forms helpers rather than plain-vanilla form handlers. > > Thanks!-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/iz34APKbU30J. For more options, visit https://groups.google.com/groups/opt_out.
Colin Law
2013-Apr-01 09:24 UTC
Re: model object form helpers, instance variable life cycle, and garbage collection
On 31 March 2013 22:57, rocdaddy <stuvjordan-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hey folks: > > In a rails app I''m building, the home page will have two forms: one to > sign-up for an account, another one to sign-in to an existing account. > > Now, if I were to use object form helpers to build these forms, the > controller for the homepage would need to create instances of the relevant > models each time someone visits the homepage. > > This concerns me a bit, because a visitor to the home page may or may not > end up using either the sign-up or sign-in forms. For instance, they may > instead click a link that gives a "tour" of the app. If they do so, will > the instance variables created by the controller in anticipation of the > POTENTIAL use of the forms be garbage-collected?The use of the objects is only for building the view, once the view is rendered those objects are not used again. Remember when you post a form it is not the model object that is posted, only the contents of the fields as strings. Therefore it does not make any difference to the objects whether the form is posted or not, they will be garbage collected after the view is created in the same way whether the form is used or not.> > I''m just thinking about efficiency here -- don''t want to have a bunch of > extra instance variables taking up space in the runtime memory just for the > sake of the (slight) convenience of using model object forms helpers rather > than plain-vanilla form handlers.Don''t worry about efficiency until it becomes an issue. A long career in s/w development has shown me that the bottlenecks in an app are never where you expect them to be so optimising before you know where the critical parts are is a waste of time. Spend the time instead on ensuring full test coverage so that if you do need to optimise (which is unlikely) you can refactor the code without worrying about whether you have messed something up. Colin -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.