ruby-changes:31746
From: jeg2 <ko1@a...>
Date: Sun, 24 Nov 2013 09:44:48 +0900 (JST)
Subject: [ruby-changes:31746] jeg2:r43825 (trunk): * lib/csv.rb: Optimize header hashes by freezing string keys.
jeg2 2013-11-24 09:44:41 +0900 (Sun, 24 Nov 2013) New Revision: 43825 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=43825 Log: * lib/csv.rb: Optimize header hashes by freezing string keys. [ruby-core:58510] Modified files: trunk/ChangeLog trunk/lib/csv.rb trunk/test/csv/test_interface.rb trunk/test/csv/test_row.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 43824) +++ ChangeLog (revision 43825) @@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Sun Nov 24 09:37:20 2013 Andrew Vit <andrew@a...> + + * lib/csv.rb: Optimize header hashes by freezing string keys. + [ruby-core:58510] + Sun Nov 24 09:18:06 2013 Aman Gupta <ruby@t...> * ext/objspace/objspace_dump.c (dump_object): Use PRIuSIZE to print Index: lib/csv.rb =================================================================== --- lib/csv.rb (revision 43824) +++ lib/csv.rb (revision 43825) @@ -235,6 +235,7 @@ class CSV https://github.com/ruby/ruby/blob/trunk/lib/csv.rb#L235 # def initialize(headers, fields, header_row = false) @header_row = header_row + headers.each { |h| h.freeze if h.is_a? String } # handle extra headers or fields @row = if headers.size > fields.size @@ -2208,6 +2209,7 @@ class CSV https://github.com/ruby/ruby/blob/trunk/lib/csv.rb#L2209 # prepare converted and unconverted copies row = @headers if row.nil? @headers = convert_fields(@headers, true) + @headers.each { |h| h.freeze if h.is_a? String } if @return_headers # return headers return self.class::Row.new(@headers, row, true) Index: test/csv/test_interface.rb =================================================================== --- test/csv/test_interface.rb (revision 43824) +++ test/csv/test_interface.rb (revision 43825) @@ -198,6 +198,25 @@ class TestCSV::Interface < TestCSV https://github.com/ruby/ruby/blob/trunk/test/csv/test_interface.rb#L198 end end + def test_write_hash_with_string_keys + File.unlink(@path) + + lines = [{a: 1, b: 2, c: 3}, {a: 4, b: 5, c: 6}] + CSV.open( @path, "wb", headers: true ) do |csv| + csv << lines.first.keys + lines.each { |line| csv << line } + end + CSV.open( @path, "rb", headers: true ) do |csv| + csv.each do |line| + csv.headers.each_with_index do |header, h| + keys = line.to_hash.keys + assert_instance_of(String, keys[h]) + assert_same(header, keys[h]) + end + end + end + end + def test_write_hash_with_headers_array File.unlink(@path) Index: test/csv/test_row.rb =================================================================== --- test/csv/test_row.rb (revision 43824) +++ test/csv/test_row.rb (revision 43825) @@ -297,7 +297,12 @@ class TestCSV::Row < TestCSV https://github.com/ruby/ruby/blob/trunk/test/csv/test_row.rb#L297 end def test_to_hash - assert_equal({"A" => nil, "B" => 2, "C" => 3}, @row.to_hash) + hash = @row.to_hash + assert_equal({"A" => nil, "B" => 2, "C" => 3}, hash) + hash.keys.each_with_index do |string_key, h| + assert_predicate(string_key, :frozen?) + assert_same(string_key, @row.headers[h]) + end end def test_to_csv -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/