Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

deprecate duplicable? and remove it from the framework

#2900

Active Support has a predicate called duplicable? that returns true/false according to a hard-coded list of cases. For example strings are duplicable? but numbers are not.

That's used in the framework to conditionally call dup/clone on arbitrary values. However, classes have means to disallow duplication, like removing dup/clone or raising exceptions from them, so that technique is not generic enough.

This patch replaces usage of duplicable? in Rails with a robust rescue idiom, and deprecates the predicate itself.

Reported by Xavier Noria · July 11th, 2009 @ 01:40 AM

State: wontfix
Milestone: 2.x
Assigned to: nobody
Importance: none

Activity

  1. Michael Koziarski
    Michael Koziarski

    rescues are quite expensive, this is why we added the duplicable thing in the first place ;)

    Not sure what the right fix is here, what values have you seen this being called on?

    August 3rd, 2009 @ 06:13 AM

  2. Xavier Noria
    Xavier Noria

    My only concern is that the current code does not play by the rules for arbitrary values.

    Deviating from standard Ruby approaches has to be very justified in my view. If experience shows the current approach works in practice, and that avoiding rescue in those points pays off in terms of performance, then no prob (I can't personally tell).

    In that case I'd perhaps document the motivation in the rdoc of duplicable?.

    November 15th, 2009 @ 08:21 PM

  3. Matt Jones
    Matt Jones

    Looks like it's primarily used in ActiveRecord (2.3.4) in clone_attribute_value, which gets called by the dirty mechanism. So removing it in favor of rescue would impose additional execution cost on pretty much every attribute assignment that isn't duplicable (ie, integers and booleans).

    November 15th, 2009 @ 09:31 PM

  4. Xavier Noria
    Xavier Noria

    Yes, and in a few more places (see the patch).

    November 15th, 2009 @ 09:35 PM

  5. Xavier Noria
    Xavier Noria

    I did some isolated benchmarks and added this explanation in duplicable.rb:

    http://github.com/lifo/docrails/commit/2ddbef421cb877bc219ac2737bbb...

    Please feel free to close the ticket if the comment looks fine.

    November 18th, 2009 @ 11:18 PM

  6. Xavier Noria
    Xavier Noria
    • State changed from new to wontfix

    February 26th, 2010 @ 05:10 PM