From ac5be236c64c3e0f4294a84e4e37b6e1c3c07220 Mon Sep 17 00:00:00 2001 From: Tom Werner Date: Tue, 4 Sep 2007 18:42:29 -0700 Subject: [PATCH] optimize timeline since logs rely on it heavily --- bin/god | 2 ++ lib/god/timeline.rb | 19 ++++++++++++++++++- test/test_timeline.rb | 12 ++++++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/bin/god b/bin/god index 0e96c9b..a95a639 100755 --- a/bin/god +++ b/bin/god @@ -14,6 +14,7 @@ Usage: god [command] [options] Commands: start + restart stop monitor unmonitor @@ -146,6 +147,7 @@ elsif command = ARGV[0] begin puts "Sending '#{command}' command" + puts "This may take a few seconds depending on the grace periods assigned to your watches" # send command watches = server.control(name, command) diff --git a/lib/god/timeline.rb b/lib/god/timeline.rb index 2445071..12c1904 100644 --- a/lib/god/timeline.rb +++ b/lib/god/timeline.rb @@ -6,9 +6,26 @@ module God @max_size = max_size end + # Push a value onto the Timeline + # + # Implementation explanation: + # A performance optimization appears here to speed up the push time. + # In essence, the code does this: + # + # def push(val) + # super(val) + # shift if size > @max_size + # end + # + # But that's super slow due to the shift, so we resort to reverse! and pop + # which gives us a 2x speedup with 100 elements and a 6x speedup with 1000 def push(val) + if (size + 1) > @max_size + reverse! + pop + reverse! + end super(val) - shift if size > @max_size end def <<(val) diff --git a/test/test_timeline.rb b/test/test_timeline.rb index 1c0862d..c714f98 100644 --- a/test/test_timeline.rb +++ b/test/test_timeline.rb @@ -21,4 +21,16 @@ class TestTimeline < Test::Unit::TestCase @timeline.push(1) assert_equal [], @timeline.clear end + + # def test_benchmark + # require 'benchmark' + # + # count = 1_000_000 + # + # t = Timeline.new(10) + # + # Benchmark.bmbm do |x| + # x.report("go") { count.times { t.push(5) } } + # end + # end end -- 2.11.4.GIT