Feature #9143

Optimize CSV to share hash key objects between rows

Added by Andrew Vit almost 2 years ago. Updated almost 2 years ago.

[ruby-core:58510]
Status:Closed
Priority:Normal
Assignee:James Gray

Description

Currently, when CSV headers are strings, then each row receives a copy of the strings for hash keys. This patch is an optimization to share the same frozen string keys between all rows.

Please verify: in the implementation of Hash::[], are strings the only object that is dup'd, or should other types be frozen so they aren't copied for each row too?

csv-freeze-headers.patch Magnifier (1.33 KB) Andrew Vit, 11/23/2013 05:05 AM

csv-freeze-headers-2.patch Magnifier (2.1 KB) Andrew Vit, 11/24/2013 08:35 AM

History

#1 Updated by Andrew Vit almost 2 years ago

Note, this can be worked around by doing csv.header_converters << proc{|h| h.freeze } but I think there should be some built-in optimization for this.

#2 Updated by Nobuyoshi Nakada almost 2 years ago

=begin
You can use (({assert_instance_of})) and (({assert_same})).
=end

#3 Updated by Andrew Vit almost 2 years ago

Thanks nobu, I write too much RSpec lately.

Updated patch attached: I also added freeze in Row#initialize, not sure if both places are needed, or which is better. Please review.

#4 Updated by Nobuyoshi Nakada almost 2 years ago

assert_predicate(string_key, :frozen?) will show better message when it fails.

#5 Updated by Nobuyoshi Nakada almost 2 years ago

  • Assignee set to James Gray
  • Status changed from Open to Assigned

#6 Updated by James Gray almost 2 years ago

  • % Done changed from 50 to 100
  • Status changed from Assigned to Closed

Also available in: Atom PDF