Skip to content
This repository

Freeze columns before using them as hash keys #7631

Merged
merged 1 commit into from over 1 year ago

12 participants

Jeremy Evans Rafael Mendonça França Bill Lipa Xavier Noria Aaron Patterson Steve Klabnik Santiago Pastorino Michael Koziarski Damien Mathieu Pedro Nascimento Evan Phoenix
Jeremy Evans

This reduces the number of allocated strings from columns * (rows + 1) to just columns. This should fix #7629.

Jeremy Evans Freeze columns before using them as hash keys
This reduces the number of allocated strings from columns * (rows + 1) to just columns.
3aef5ce
Rafael Mendonça França

Could you add benchmark to the commit message?

cc @tenderlove @jonleighton

Jeremy Evans jeremyevans closed this September 13, 2012
Jeremy Evans

Why should I spend more time helping the competition? ;)

Jeremy Evans jeremyevans reopened this September 13, 2012
Rafael Mendonça França

lol. Right. I just ask, we don't need to do. I'll not merge without @tenderlove and @jonleighton approval.

Also thank you for the pull request.

Damien Mathieu dmathieu commented on the diff September 13, 2012
activerecord/lib/active_record/result.rb
@@ -11,7 +11,7 @@ class Result
11 11
     attr_reader :columns, :rows, :column_types
12 12
 
13 13
     def initialize(columns, rows, column_types = {})
14  
-      @columns      = columns
  14
+      @columns      = columns.map{|c| c.freeze}
11
Damien Mathieu Collaborator

master is only 1.9 compatible. So you can use columns.map(&:freeze)

@lunks, I saw that before you opened the pull request. Like I said, it's up to rails core to change the style after they merge it, if they even decide to merge it.

Steve Klabnik Collaborator

to change the style after they merge it,

Naw, style changes are always asked for before merging, not after. We actually don't do style-only commits.

Are the values in @columns actually used as Hash keys? If not always, then this change is actually worse than without it because this code always allocates another copy of each string and then freezes it. Basically, it's paying constant upfront cost instead of a variable cost. You'll need to just show why that trade off is valid.

@steveklabnik I don't plan on doing additional work on this patch. If that means you won't merge the patch due to style issues, that's fine with me. I'm not an ActiveRecord user, for obvious reasons.

@evanphx most callers are going to call hash_rows, which is going to use the columns as hash keys (https://github.com/jeremyevans/rails/blob/3aef5ce9b35a4659379201eb6bb1dba355a83ba4/activerecord/lib/active_record/result.rb#L57). It's possible to move this map call into hash_rows if you are worried about affecting the other cases. Also, this should not allocate any additional strings, since String#freeze just modifies the object's flags in-place, it doesn't allocate another object. However, Array#map does allocate an additional array. The only situation where this change allocates more objects is in the case where 0 rows are returned or hash_rows is not called, in which it allocates 1 additional object (the array). If a single row with a single column is returned, this will allocate the same number of objects (an array object instead of a string object). In all other cases, it should allocate fewer objects (columns * rows - 1 fewer).

@jeremyevans Oh yes yes. Just an extra Array. Ooops! I hadn't had dinner yet!

We can do @columns = columns.each(&:freeze) or @columns = columns.each {|c| c.freeze} to avoid allocating that extra array.

Aaron Patterson Owner
Pedro Nascimento
lunks added a note September 15, 2012

Perhaps it's better to dup, then freeze them?

Bill Lipa
wlipa added a note September 15, 2012

That should work almost as well, since the Result isn't per returned instance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Bill Lipa

I verified this fix worked for the case I was studying in #7629, bringing the number of extra strings per AR model instance down from 15 to 2. Thanks!

Rafael Mendonça França

@wlipa do you have to check this case that can be shared?

Bill Lipa

Here's a two line check that relies on an AR model with more than one instance in the db.

  arr = MyModel.all # or any other AR query that returns > 1 of the same object type
  arr.first.instance_variable_get("@attributes").keys.first.object_id == arr.last.instance_variable_get("@attributes").keys.first.object_id

That will be true if the keys aren't getting duplicated.

(Note, this assumes that columns are always stored in the same order in the attributes hash. Not sure if that's a safe assumption. If not one could select the key named "id".)

Xavier Noria
Owner

While @tenderlove or @jonleighton say something... I'd like to suggest adding a comment. Without a comment the next guy reading that file will wonder why that is done.

Aaron Patterson
Owner

Can we actually prove that this is a performance bottleneck before merging? "Many objects allocated" does not equal "slow system". Also, note that while we may be reducing memory here, we're also trading it for speed (extra method calls to freeze). Please, please, please someone make hard numbers and graphs before we embark on this micro-optimization hunt.

I am a hard :-1: on this pull request until someone can actually demonstrate a real performance increase.

Bill Lipa

For this optimization, it shouldn't be necessary to call freeze more than N times per process where N is the number of db fields in AR classes (ie, a small constant cost at startup). That would probably change the patch though.

Bill Lipa

@jeremyevans stated that Ruby internally dups and calls freeze on strings used as hash keys. Given that, this patch should substantially reduce the number of calls to freeze, simply because there are so many fewer strings in play.

That said I'll take a look at gathering some metrics. Never a bad idea!

Pavel Argentov argent-smith referenced this pull request in rubynoname/ShowNotes September 13, 2012
Closed

об оптимизации #33

Bill Lipa

I took a more detailed look at an AR model with 9 integer fields, 2 datetimes, and 2 strings. Querying the full table out from my db with MyModel.all returned 9200 rows.

The growth in process resident set size after the query was 21m (from 84m to 105m) with the patch applied. Without the patch, the growth was 29m (from 87m to 116m). So the patch yielded at 27.5% reduction in process rss growth for this query.

The average CPU time to do the query was 1.16s patched and 1.22s unpatched, so 5% reduction there.

This was with Ruby 1.9.3p194, Ubuntu 12.04, and Rails 3.2.8.

Steve Klabnik
Collaborator

@wlipa could you provide the benchmark script, please?

Bill Lipa

You mean "MyModel.all" with the database as described? It's really as simple as that.

Aaron Patterson
Owner

Pre-freezing String keys before using them as keys in multiple Hash instances is a very cool optimisation that I myself frequently use. This should be merged.

Bill Lipa

Here are instructions for replicating from scratch in a new app.

rails new querypig -d mysql
rails generate model MyModel user_id:integer kind:integer product_id:integer comment_id:integer duration:integer category_id:integer ip:string leaderboard_id:integer status_id:integer description:string
rake db:create db:migrate

Populate the database:

10000.times do
  MyModel.create({
    :user_id => rand(1..100),
    :kind => rand(1..100),
    :product_id => rand(1..100),
    :comment_id => rand(1..100),
    :duration => rand(1..100),
    :category_id => rand(1..100),
    :ip => rand(1..100).to_s,
    :leaderboard_id => rand(1..100),
    :status_id => rand(1..100),
    :description => rand(1..100).to_s,
  })
end

Measure the memory usage:

def resident_mb
  `ps -o rss= -p #{Process.pid}`.to_i / 1.kilobyte
end

def stats
  GC.start
  rmb = resident_mb
  puts "pid #{Process.pid} rss: #{rmb}m gc: #{GC.stat}"
  rmb
end


# preflight
MyModel.first

initial_rss = cur_rss = stats

8.times do 
  # potentially piggish
  mm = MyModel.all
  cur_rss = stats
end

puts "rss delta: #{cur_rss - initial_rss}"

I did that by sticking the above in script/measure.rb and using the command "rails runner script/measure.rb". On OSX I consistently got a delta of right near 18m with the patch, and a varying but always larger amount between 25-41m without it.

Santiago Pastorino
Owner

I wonder why do adapters return strings instead of symbols.

Michael Koziarski
Owner

As @jeremyevans points out, generating columns.size strings is considerably less GC pressure than columns.size * result_set.size. These strings are the keys to the attributes hash, so I don't think we need to worry about users expecting to be able to do something crazy like

  @attributes.keys.map(&:upcase!)

This seems like a complete no brainer to me, the only change I'd suggest is just interning the strings to symbols but that's something that could surprise users.

Jeremy Evans

Note that you can't upcase! hash string keys in ruby anyway, even if the string used as a key wasn't frozen:

$ ruby -e '{"a"=>1}.keys.map{|s| s.upcase!}' 
-e:1:in `upcase!': can't modify frozen String (RuntimeError)
        from -e:1:in `block in <main>'
        from -e:1:in `map'
        from -e:1:in `<main>'

This is because ruby uses a frozen dup of the string as the hash key if the string isn't already frozen, so pretty much all string hash keys in ruby are already frozen.

Aaron Patterson tenderlove merged commit 2004ef2 into from September 19, 2012
Aaron Patterson tenderlove closed this September 19, 2012
Santiago Pastorino
Owner

I've moved the code to hash_rows methods and added a comment 2068d30
Thanks @jeremyevans

Bill Lipa

Doesn't that change it by calling @columns.map { |c| c.freeze } once per row rather than once per result?

Santiago Pastorino
Owner

Ouch yeah sorry. Fixed da400fb

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Sep 13, 2012
Jeremy Evans Freeze columns before using them as hash keys
This reduces the number of allocated strings from columns * (rows + 1) to just columns.
3aef5ce
This page is out of date. Refresh to see the latest.
2  activerecord/lib/active_record/result.rb
@@ -11,7 +11,7 @@ class Result
11 11
     attr_reader :columns, :rows, :column_types
12 12
 
13 13
     def initialize(columns, rows, column_types = {})
14  
-      @columns      = columns
  14
+      @columns      = columns.map{|c| c.freeze}
15 15
       @rows         = rows
16 16
       @hash_rows    = nil
17 17
       @column_types = column_types
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.