From: Stefan Kaes Date: 2005-05-01T19:05:37+09:00 Subject: Re: mysql_adapter question Stefan Kaes wrote: > Hi everyone, > > since I don't know who wrote that code originally and I am absolutely > puzzled by it, I'll post the question here. In mysql_adapter.rb, the > function to execute a select statement is written like this: (I've > changed the varaible names to make it bit easier to understand) > > def select(sql, name = nil) > result = nil > @connection.query_with_result = true > result = execute(sql, name) > rows = [] > row_template = result.fetch_fields.inject({}) {|t, f| t[f.name] = > nil; t } > result.each_hash { |row| rows << row_template.dup.update(row) } > rows > end > > Since each_hash already returns a hash for each row of the result set > already, the last part could have been written like this: > > rows = [] > result.each_hash { |row| rows << row } > result.free > rows > > I have found this version to be much faster than the original (10% > improvement on a complete request) and to create much less garbage > too. (Why was the free statment missing?) > > Does anyone know why this code exists in it's current version? > > -- stefan > > > > Sorry, wrong mailing list. My apologies. -- stefan