On Jul 31, 2006, at 11:17 AM, Caio Chassot wrote:
Do you really think it's a good idea to run filters more than once?

How about maybe adding a uniq option to pre/append_filter_to_chain, that would ensure a filter is never added twice (but related conditions may be changed)?

It would seem to me most filters that end up running twice or thrice do so harmlessly, but needlessly. At most, hindering performance.

I think you're right that most filters should only run once. But I worry that doing this might cause problems in border cases. What about putting warnings in the logs when filters are run more than once rather than prohibiting it all together. Or maybe throw an exception, but allow that to be suppressed by providing something like:
before_filter :foo, :rerun => true

Perhaps :rerun isn't the best name, but I think you get the idea. You could throw the exception in before_filter using .include? and avoid the overhead of keeping a call record or constantly making the list unique.

-Mat

PS., I ran a quick benchmark for uniq vs. include? and found this:

Rehearsal -----------------------------------------------
uniq          1.430000   0.070000   1.500000 (  1.531704)
if include?   0.050000   0.000000   0.050000 (  0.042735)
-------------------------------------- total: 1.550000sec

                  user     system      total        real
uniq          6.470000   0.430000   6.900000 (  7.013875)
if include?   0.040000   0.000000   0.040000 (  0.044006)

here's the benchmark code incase I missed anything:

--- begin uniq_vs_include.rb
require 'benchmark'

n = 5000

Benchmark.bmbm do |b|
  array = []
  50.times do
    array << rand(50)
  end
  array.uniq

  items= []
  n.times do
    items << rand(50)
  end

  b.report("uniq") do
    n.times do |i|
      item = items[i]
      array << item
      array.uniq
    end
  end

  b.report("if include?") do
    n.times do |i|
      item = items[i]
      if array.include? item
        array << item
      end
    end
  end
end
---- end uniq_vs_include.rb


_______________________________________________
Rails-core mailing list
Rails-core@lists.rubyonrails.org
http://lists.rubyonrails.org/mailman/listinfo/rails-core

Reply via email to