This project is archived and is in readonly mode.

#1038 ✓stale
Ben Johnson

assert_valid_keys slow?

Reported by Ben Johnson | September 12th, 2008 @ 11:24 PM | in 3.x

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!

Comments and changes to this ticket

  • josh

    josh December 20th, 2008 @ 05:27 PM

    • Assigned user set to “Jeremy Kemper”
    • State changed from “new” to “open”

    Cool, can you turn it into a diff patch plz

  • Pratik

    Pratik December 20th, 2008 @ 05:33 PM

    • 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.

  • Matías Flores

    Matías Flores September 27th, 2009 @ 04:33 PM

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

    Matías Flores September 27th, 2009 @ 04:37 PM

    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.

  • Rizwan Reza

    Rizwan Reza February 12th, 2010 @ 12:46 PM

    • Tag changed from activesupport, bugmash, performance to activesupport, performance
  • Jeremy Kemper

    Jeremy Kemper May 4th, 2010 @ 06:48 PM

    • Milestone changed from 2.x to 3.x
  • Santiago Pastorino

    Santiago Pastorino February 2nd, 2011 @ 05:05 PM

    • 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.

  • Santiago Pastorino

    Santiago Pastorino February 2nd, 2011 @ 05:05 PM

    • State changed from “open” to “stale”
  • af001

    af001 May 5th, 2011 @ 03:01 AM

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

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

People watching this ticket

Pages