I'd like to think that the community was on vacation last week, or that the winning entry, which happened to be the first and only entry, was so brilliant that it scared everyone off.  A week ago Friday, I kicked off what I hope will be a frequent experience, opening up a simple optimization contest to the community in the forums.  The challenge was to take the dice rolling iRule from Lori's post on multi-classing your load balancer, optimize it, and submit it.  One community user, natty76, took the challenge.  So, by default, natty76, you are the Optimize This! inaugural champion, congratulations!

Natty76's Winning Entry

when RULE_INIT {   set static::max 10   set static::f(0) { $num }   set i 1   while { $i < $static::max } {     eval "set static::f($i) { $static::f([expr $i - 1]) + int(rand() * \$d) }"     incr i   } } when HTTP_REQUEST {   if { [HTTP::uri] starts_with "/roll"} {     set num [URI::query [HTTP::uri] "num"]     set d [URI::query [HTTP::uri] "d"]    HTTP::respond 200 content "$num d $d = [expr $static::f($num)]"   } }


Natty76's use of RULE_INIT to build the expression eliminates the need for the per-request for loop in Lori's rule, and results in a savings of 13.55% in clock cycles.  Nice work, Nat.  This could be pruned a little more for maximum efficiency, but there's something to be said for readability.  Natty76 also noted a couple potential downsides to this implementation.  One, there is no error handling as the code assumes that $num and $d are always within the limit of the array.  Two, the memory footprint required to store the array are higher as $num & $d are higher.

Thanks much, Natty76, hopefully you'll have more (OK, some) competition next go-round.



Follow me on Twitter Follow me on LinkedIn Follow me on Facebook Add to Technorati Favorites