adzap / validates_timeliness

Date and time validation plugin for ActiveModel and Rails. Supports multiple ORMs and allows custom date/time formats.
MIT License
1.59k stars 227 forks source link

Generate attribute methods once instead of on each object creation #128

Closed monkbroc closed 9 years ago

monkbroc commented 9 years ago

This is a memory and performance improvement change.

The attribute methods for timeliness are generated at each object creation instead of once, at first creation like AR attribute methods. Since creating those methods uses module_eval it's quite slow when it's done each Model.new.

For more details on the performance improvement, see the same change on John Carney's fork: https://github.com/johncarney/validates_timeliness/pull/6

adzap commented 9 years ago

Ah great catch! I thought there was always a guard check using attribute_methods_generated? before the method was called but I can't see it called anywhere in the AR/AM source.

monkbroc commented 9 years ago

Thanks for pulling this in! It was a lot of detective work to find out what was causing all these allocations on each request in my app :)

adzap commented 9 years ago

Thanks for the great work! How many allocations did it save? I realise this is not a generalisable number but I am just interested.

monkbroc commented 9 years ago

It's around 1000 string per object creation. On an index page with 20 objects it saved 20,000 string allocations out of a total of 90,000 allocations for that request. On Sep 25, 2015 12:01 AM, "Adam Meehan" notifications@github.com wrote:

Thanks for the great work! How many allocations did it save? I realise this is not a generalisable number but I am just interested.

— Reply to this email directly or view it on GitHub https://github.com/adzap/validates_timeliness/pull/128#issuecomment-143118148 .