Malfeasance in 64-bit PowerPC MySQL Gem Compilation

Posted on May 12, 2008

For the three people in the world building the Rails mysql gem on a PowerPC G5-based OS X Server with the 64-bit MySQL installed getting this crazy error:

lazy symbol binding failed: Symbol not found: _mysql_init

The magic ninja gem install command that will cure all your ills goes a little something like this:

sudo env ARCHFLAGS="-arch ppc64" gem install -V mysql -- --with-mysql-include=/usr/local/mysql/include/ --with-mysql-lib=/usr/local/mysql/lib --with-mysql-config=/usr/local/mysql/bin/mysql_config --with-mysql-dir=/usr/local/mysql

Pretty obvious when you think about it. Not sure why it took me a little over an hour to discover the crucial lynchpin for correcting this system-wide thought-tastrophy.

CSS Templating with Sass

Posted on July 09, 2007

I don’t hear many people talking about Sass, but it is indeed fantastic. Basically, Sass is kind of like the Smarty of CSS Templating. Sass files compile to CSS after every change just like Smarty’s TPL files compile directly to PHP. Sass fits well into the Rails don’t-repeat-yourself methodology because you can define color and style constants within CSS rather than explicitly specifying them twenty times. The beauty of Sass is that you get a level of abstraction without any runtime overhead. As a result, Sass performs well and allows quick updates to color schemes and typography without resorting to perl/find or ninja text editor foo. So, why aren’t more people using it?

Ruby CAPTCHA / Ruby-GD StringFT Errors Revisited

Posted on July 09, 2007

Well, I finally solved the “undefined method `stringFT' for GD::Image:Class” error under Mac OS X. The rb-gd darwinport seems to have fallen off the face of the earth. Fortunately, I’ve learned that you can download the source from ruby-lang.org. So, I just rolled my own GD.bundle.

wget http://raa.ruby-lang.org/cache/ruby-gd/ruby-GD-0.7.4-1.tar.gz
cd ruby-GD-0.7.4
ruby extconf.rb --with-freetype --with-xpm --with-ttf
make
sudo make install
sudo mv /usr/local/lib/ruby/site_ruby/1.8/i686-darwin8.8.2/GD.bundle /usr/local/lib/ruby/1.8/i686-darwin8.8.2/

This only works if you have an up-to-date ruby installed under /usr/local (via the Hivelogic instructions). If you’ve installed from ports or are using the older ruby shipped with Leopard, you’ll need to switch your prefix to /opt/local or /usr respectively.

Problems with Ruby’s CAPTCHA gem

Posted on June 11, 2007

Even though gd, gd-devel and the ruby GD and CAPTCHA gems were installed on my Red Hat Enterprise Linux 4 server, Ruby’s CAPTCHA gem seemed to blow-up with the error:

undefined method `StringFT'

Hmmm. I googled around and found that you need a library to bind the GD calls to ruby. This makes perfect sense, but documentation for the issue is scarce.

Eventually, I found the following rpm and installed it.

wget http://ftp.at.gnucash.org/opsys/linux/redhat.com/contrib/libc5/i686/ruby-GD-1.0_97111
rpm -ivh ruby-GD-0.7.4-0vl1.i386.rpm

Unfortunately, the RPM assumed that ruby would be installed under /local, but on RHEL everything is under /usr. So, I found where the RPM stored the library and relocated it to the proper directory.

$ rpm -qpl ruby-GD-0.7.4-0vl1.i386.rpm
/local/lib/site_ruby/1.8/i386-linux/GD.so
sudo mv /local/lib/site_ruby/1.8/i386-linux/GD.so /usr/lib/site_ruby/1.8/i386-linux/

Lo and behold, Ruby’s CAPTCHA gem worked like the clappers after this was sorted. Unfortunately, it took me over an hour to sort out the problem. It would seem that something as mundane as CAPTCHA would have just worked under Rails. The major problem seems to be that the ruby-gd maintainer’s site http://tam.0xfa.com is out of commission.

Under Mac OS X, it’s a bit easier because you can use Darwin Ports to install rb-gd. However, that install appeared to bomb for me because I’m using the latest ruby compiled via the Hivelogic instructions. I attempted to symbolically link the ruby binaries to /opt/local/bin, but that didn’t resolve the issue. I’m still looking for a workaround to this.

Iterating Through The Last 30 Days with Ruby on Rails

Posted on June 10, 2007

Recently, I attempted to create a loop in Ruby on Rails that iterated through the last month worth of dates. Stupidly, I just typed in the following code, and tried to run it… (you need to require ActiveSupport for month.ago to work.

# This code does not work
1.month.ago.step(Time.now, 1.day) { |d|
  print d.day,”\\n”
}

Unfortunately, that doesn’t work, because ActiveSupport returns a Time class value for month.ago. Ruby’s Time class doesn’t define a step method. So, being lazy, I just casted the time to an integer with to_i, and constructed the following loop which does work.

1.month.ago.to_i.step(Time.now.to_i,1.day.to_i) { |d|
   print Time.at(d).day,"\\n"
}

Later on, I learned that Date’s have a step class. So, I simplified the loop.

1.month.ago.to_date.step(Time.now.to_date, 1.day) { |d|
    print d.day,"\\n"
}

It seems like there should be an easier way to do this though. Does anyone have a better idea?

Fibonacci Shootout (CRuby vs JRuby vs Rubinius vs YARV vs Scheme vs C vs Erlang vs Haskell)

Posted on May 06, 2007



OK, I’m all for working smarter not harder, but I don’t see how the Lambda Calculus can help solve fib(n). I mean, one could use the closed-form solution, recursion, or iteration in pretty much any language. Nevertheless, over at Martini Design, he seems to think that Scheme can somehow more succinctly describe Leo’s incredible series. Problem is, fib(n) in closed-form i.e. (Φn+(1-Φ)n)/√5 where the Golden Ratio, Φ=(√5+1)/2 is more computationally efficient for small values of n, and the iterative-form is more efficient when calculating large values of n using only integer maths. I ran some tests on the following implementations to get a handle on the matter. I started out with an iterative, recursive, and libgmp C-implementation versus the demonstrated Scheme design.
#include <stdio.h>
#include <math.h>
#include <gmp.h>

#define PHI 1.61803398874989484

// C recursive solution
unsigned long long fib_r(unsigned long long n) {
  return (n<3) ? 1 : fib_r(n-1) + fib_r(n-2);
}

// C closed-form solution
unsigned long long fib_c(unsigned long long n) {
  return((pow(PHI,n)-pow(1.00-PHI,n))/sqrt(5)+0.5);
}

// C iterative solution
unsigned long long fib_i(unsigned long long n) {
  if (n<=2) return(1);
  unsigned long long i, sum=0, a=1, b=1;
  for(i=3; i&lt=n; i++) {
    sum=a+b;
    a=b;
    b=sum;
  }
  return sum;
}

// C libgmp solution
char* fib_g(unsigned long long n) {
  mpz_t fn;
  mpz_init(fn);
  mpz_fib_ui(fn, n);
  return mpz_get_str(NULL, 10, fn);
}

Here is the original solution in Scheme.

(define (fib x)
  (define (fib2 x1 x2 cnt maxcnt)
    (cond
      ((= cnt 0)
       (fib2 0 0 (+ cnt 1) maxcnt))
      ((= cnt 1)
       (fib2 1 0 (+ cnt 1) maxcnt))
      ((> cnt maxcnt)
       x1)
      (else
       (fib2 (+ x1 x2) x1 (+ cnt 1) maxcnt))))
    (fib2 0 0 0 x))

So, for fun, I benchmarked the Scheme implementation against libgmp on my Macbook Pro (2.33GHz Core 2/2GB) for n=1000000. Here are the timed results.

$ time ./fib_scheme
real    6m0.414s
user    5m54.327s
sys     0m1.889s

$ time ./fib_gmp
real    0m0.167s
user    0m0.152s
sys     0m0.010s

The Scheme solution takes over 6 minutes to complete while the libgmp one finishes in 0.167 seconds. Therefore, the Scheme implementation requires more lines for trivial cases and is computationally slower for non-trivial cases.

Next, I decided to test an iterative ruby implementation for fib(1000000) to compare.

# Ruby - iterative algorithm
  def fib(n)
    return 1 if n<=2
    i=0; a=1; b=1; sum=0
    return 1 if n<=2
    3.upto(n) {
      sum=a+b
      a=b
      b=sum
    }
    return sum;
  end

Here are the results from CRuby.

$ time ruby fib.rb
real    3m18.343s
user    2m6.019s
sys     1m7.913s

Hmmm, what about JRuby, Rubinius and yarv?

$ time jruby-1.0.0RC1/bin/jruby fib.rb
real    5m19.290s
user    5m3.198s
sys     0m6.543s

$ time yarv/ruby ~/fib.rb
real    3m15.521s
user    2m7.299s
sys     1m7.079s

$ time shotgun/rubinius ~/fib.rb
[Using local compiler]
real    4m31.221s
user    0m9.542s
sys     0m53.533s

I ran another functional implementation through Erlang.

%% Erlang - recursive
fibonacci(N) when N < 2 -> 1;
fibonacci(N) -> fibonacci(N-1) + fibonacci(N-2).

This unfortunately did not complete after 27 minutes.

Next up is an iterative Python implementation.

# Python - iterative
def fib(n):
  sum=0
  a=1
  b=1
  if n<=2: return 1
  for i in range(3,n+1):
    sum=a+b
    a=b
    b=sum
  return sum
$ time python fib.py
real    3m16.089s
user    2m28.190s
sys     0m46.314s

Dons and Quhaha from reddit encouraged me to try Haskell. So, I ran the following fast recursion implementation as well.


module Main where
-- Haskell Fast Recursion
f n = fst (l n) where
  l n | n<2 = (1,1)
    | n==2 = (2,1)
    | n>2 = if (rem n 2)==1 then ( k1*(k1+k2)+k1*k2,k1*k1+k2*k2)
                  else ((l1+l2)*(l1+l2)+l1*l1, (l1+l2)*l1+l1*l2)
      where (k1,k2) = l (div (n-1) 2)
         (l1,l2) = l ((div n 2)-1)
f_print n = print(show n ++ "th Fibonacci number is " ++ show (f n))

Surprisingly, the Haskell routine was very fast as a recursive algorithm.

$ time ./fibh
real    0m2.205s
user    0m2.188s
sys     0m0.012s

It appears that the libgmp Fibonacci implementation is orders of magnitude faster than any normal iterative or recursive solution using FP or iterative solutions. Since libgmp is the defacto choice for number theorists, the speed of its custom fibonacci function is to be expected. The most surprising fact is that CRuby is nearly as fast as compiled Python for this calculation, and the Haskell fast recursion algorithm’s excellent speed.

Note: My graph legend specifies “/i” for iterative and “/r” for recursive.

Xaggly Gemified

Posted on May 05, 2007

I finally got bit hard by the slowness of REXML at work, so I compiled my own Ruby XML library parser as a gem for Mac OS X and Linux. You can download these gems from my repository until I figure out how to setup a multiplatform gem server.

To install under Intel-based Macs:

wget http://involution.com/xaggly/xaggly-0.0.1-i686-darwin8.8.2.gem
sudo gem install xaggly-0.0.1-i686-darwin8.8.2.gem

Xaggly, A C-based XML Parser for Ruby Now Released

Posted on January 02, 2007

I finally realeased my XML parser Ruby extension under the MIT license on Saturday. I’m still working on using mkmf to produce a Makefile that will handle both Flex and Bison files, and matz himself has used his glimmering life gem as a torchlight to show me the way. Nevertheless, the compile should work on OS X or Linux right now. You can download the source code here.

Xaggly vs Hpricot vs REXML

Posted on November 28, 2006

Feeling the heat from the usual crowd, I cobbled together a XML-parser Ruby extension in C with a flagon of magic Flex and Bison elixir. This weekend, in a frenetic burst of Tryptophan-obliterating productivity, I had coded to the point where most reasonable XPath queries were working. So, I promoted my prematurely optimized, non-conformant bits to a named program called Xaggly. Tonight, I tested my parser using a ginormous XML file pilfered from the old-timey GPS watch’s serial interface. I am pleased with the pixels fired into the aether and will henceforth scuttle about with a jaunty step. Mr bray and _why: step away from the gogglebox and take heed.

Generating MP3 Durations with PHP and DBDO

Posted on November 16, 2006

I had to update a MySQL database full of mp3 names with the inherent track, album, and genre metadata. So, I grabbed the Zend mp3 library and sorted it out like the clappers using the DataObjects ORM.

    $m = new DataObjects_Tracks;
    $m->addWhere("filename RLIKE '[.period.]mp3′");
    $m->find();
    while($m->fetch()) {
        $mp3 = new MP3($m->filename);
        $mp3->get_id3();
        $mp3->get_info();
        $m->duration = $mp3->info["length"];
        $m->album = $mp3->info["album"];
        $m->title = $mp3->info["title"];
        $m->update();
    }

If ORMs were automobiles, DB_DataObject would be somewhere between a 1976 Pinto and an Edsel, but it gets the job done OK. The most annoying thing about it is that you have to run php DB/DataObject/createTables.php every time you alter the database. Unfortunately, the more I use DBDO the more I miss the ActiveRecord sauce. Once you’ve driven a Porsche, you don’t want to go back to the old Rustang.