Browse files

Precalculate a map of column names -> column indices

This allows us to avoid using Array#index to map names to indices, which
requires N**2 comparisons between column names to fully hydrate a row.
Performance improvements are considerable for large result rows.
  • Loading branch information...
1 parent 7cc3ad8 commit 6390951b41b28a849e2e513c74b6c5b0286e477d @outoftime committed Jan 12, 2013
Showing with 9 additions and 1 deletion.
  1. +9 −1 lib/cassandra-cql/row.rb
10 lib/cassandra-cql/row.rb
@@ -27,7 +27,7 @@ def initialize(row, schema)
column_index = key
column_name = key
- column_index = column_names.index(key)
+ column_index = column_index(key)
if column_index.nil?
@@ -48,6 +48,14 @@ def column_names
ColumnFamily.cast(, @schema.names[])
+ def column_indices
+ @column_indices ||= Hash[column_names.each_with_index.to_a]
+ end
+ def column_index(column_name)
+ column_indices[column_name]
+ end
def column_values do |name|

1 comment on commit 6390951


Nice research .. I like the general idea, but a few tweaks will make it better imo:

  • I don't think we need a column_index helper as a reference to column_indices, it doesn't add clarity to me, just another layer
  • Since @column_indices are now used on the first access to the row (except for index only access, though we deserialize the names at that point anyway), line 25 should also use the hash and has_key instead of include?. Should speed up integer CFs quite a bit as well (benchmark? :) )


Please sign in to comment.