Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
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...
commit 6390951b41b28a849e2e513c74b6c5b0286e477d 1 parent 7cc3ad8
Mat Brown authored
Showing with 9 additions and 1 deletion.
  1. +9 −1 lib/cassandra-cql/row.rb
10 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|

1 comment on commit 6390951

Kelley Reynolds

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? :) )

Thanks

Please sign in to comment.
Something went wrong with that request. Please try again.