Permalink
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 outoftime committed Jan 12, 2013
Showing with 9 additions and 1 deletion.
  1. +9 −1 lib/cassandra-cql/row.rb
View
@@ -27,7 +27,7 @@ def initialize(row, schema)
column_index = key
else
column_name = key
- column_index = column_names.index(key)
+ column_index = column_index(key)
end
if column_index.nil?
@@ -48,6 +48,14 @@ def column_names
ColumnFamily.cast(column.name, @schema.names[column.name])
end
end
+
+ 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
column_names.map do |name|

0 comments on commit 6390951

Please sign in to comment.