Issue #12009 has been updated by normalperson (Eric Wong).

Status changed from Assigned to Rejected

To be solved in a more general manner [Feature #14225].

(Partially solved for untainted data in [Feature #13725])


----------------------------------------
Feature #12009: [PATCH] psych: pre-freeze string keys for hashes
https://bugs.ruby-lang.org/issues/12009#change-69095

* Author: normalperson (Eric Wong)
* Status: Rejected
* Priority: Normal
* Assignee: tenderlovemaking (Aaron Patterson)
* Target version: 
----------------------------------------
With the following example, this reduces allocations from 346 to 324
strings when calling `Psych.load` on a 26-entry hash:

---------------------------------------------------------------
~~~ruby
require 'psych'
require 'objspace'
before = {}
after = {}
str = [ '---', *(('a'..'z').map { |k| "#{k * 11}: 1" }), '' ].join("\n")
GC.disable
ObjectSpace.count_objects(before)
h = Psych.load(str)
ObjectSpace.count_objects(after)
p(after[:T_STRING] - before[:T_STRING])
~~~
---------------------------------------------------------------

Allocating 324 strings for 26 hash keys is still expensive.  More
work will be needed to reduce allocations further...

Tested on x86-64.

It would actually be awesome if `Psych` could use the `fstring` table
and have string keys auto-deduped as in Ruby source code.



---Files--------------------------------
0001-psych-pre-freeze-string-keys-for-hashes.patch (1.41 KB)


-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>