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.

assert_valid_keys slow?

#1038

I don't know if this is helpful or not, but I figured I would share it.

I am writing a library that uses ActiveSupport. I have been using the Hash.assert_valid_keys method in various parts throughout the library. What was really surprising was when I started profiling the library to try and increase performance. The Hash.assert_valid_keys method was my biggest bottle neck.

A test that once took 19.2 seconds is now taking 12.6 seconds once I stopped using Hash.assert_valid_keys. The culprit is Array.flatten. I rewrote the method and got a 35% performance increase.


def fast_assert_valid_keys(valid_keys)
  unknown_keys = keys - valid_keys
  raise(ArgumentError, "Unknown key(s): #{unknown_keys.join(", ")}") unless unknown_keys.empty?
end

I know this changes how the method is used and what kind of arguments it takes, but I thought a 35% increase was significant enough to report.

I don't mean to nitpick either.

Thanks!

Reported by Ben Johnson · September 12th, 2008 @ 11:24 PM

State: stale
Milestone: 3.x
Assigned to: Pratik Pratik
Importance: none

Activity

  1. josh
    josh
    • Assigned user set to Jeremy Kemper
    • State changed from new to open

    Cool, can you turn it into a diff patch plz

    December 20th, 2008 @ 05:27 PM

  2. Pratik
    Pratik
    • Assigned user changed from Jeremy Kemper to Pratik
    • State changed from open to incomplete

    Could you please submit a patch please - http://rails.lighthouseapp.com/p... ?

    Thanks.

    December 20th, 2008 @ 05:33 PM

  3. Matías Flores
    Matías Flores
    • Tag changed from activesupport, performance to activesupport, bugmash, performance

    September 27th, 2009 @ 04:33 PM

  4. Matías Flores
    Matías Flores

    verified the difference in performance is significant, but at the cost of changing the current behavior.

    -1 as it breaks backwards compatibility. E.g. any code using something like { :name => 'Rob', :years => '28' }.assert_valid_keys(:name, :age) would break with this change.

    September 27th, 2009 @ 04:37 PM

  5. Rizwan Reza
    Rizwan Reza
    • Tag changed from activesupport, bugmash, performance to activesupport, performance

    February 12th, 2010 @ 12:46 PM

  6. Jeremy Kemper
    Jeremy Kemper
    • Milestone changed from 2.x to 3.x

    May 4th, 2010 @ 06:48 PM

  7. Santiago Pastorino
    Santiago Pastorino
    • State changed from incomplete to open
    • Importance changed from to

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

    February 2nd, 2011 @ 05:05 PM

  8. Santiago Pastorino
    Santiago Pastorino
    • State changed from open to stale

    February 2nd, 2011 @ 05:05 PM

  9. af001
    af001

    私の中で、総合評価のとっても低いアバアバクロホリスタークロ銀座店。アバクロは大好きなんですけどね。一昨日の東京駅付近での打ち合わせの後、散歩がてら久々に行ってきました。そしたらビックリ!相変わらアバクロず、踊っているだけの店員さんとかもいましたが、

    May 5th, 2011 @ 03:01 AM