1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Improve performance for scope_for_create

```ruby
class Post < ActiveRecord::Base
end

posts = Post.where(id: 1, title: "foo")

Benchmark.ips do |x|
  x.report("scope_for_create") { posts.scope_for_create }
end
```

Before:

```
Warming up --------------------------------------
    scope_for_create    30.125k i/100ms
Calculating -------------------------------------
    scope_for_create    334.033k (± 4.4%) i/s -      1.687M in   5.060493s
```

After:

```
Warming up --------------------------------------
    scope_for_create    35.088k i/100ms
Calculating -------------------------------------
    scope_for_create    388.603k (±11.8%) i/s -      1.930M in   5.080998s
```
This commit is contained in:
Ryuta Kamizono 2020-04-28 18:07:00 +09:00
parent 280d6eb2e1
commit b66235d432
2 changed files with 7 additions and 11 deletions

View file

@ -686,7 +686,9 @@ module ActiveRecord
end
def scope_for_create
where_values_hash.merge!(create_with_value.stringify_keys)
hash = where_values_hash
create_with_value.each { |k, v| hash[k.to_s] = v } unless create_with_value.empty?
hash
end
# Returns true if relation needs eager loading.

View file

@ -47,18 +47,12 @@ module ActiveRecord
end
def to_h(table_name = nil)
equalities = equalities(predicates)
if table_name
equalities = equalities.select do |node|
node.left.relation.name == table_name
end
end
equalities.map { |node|
equalities(predicates).each_with_object({}) do |node, hash|
next if table_name&.!= node.left.relation.name
name = node.left.name.to_s
value = extract_node_value(node.right)
[name, value]
}.to_h
hash[name] = value
end
end
def ast