Commit Graph

16 Commits

Author SHA1 Message Date
Kevin Cheng
16510d609c Deprecate ActiveRecord::Result#to_hash in favor of #to_a
method returns an array of hashes, not a hash
e.g. Hash.try_convert(result) calls #to_hash and raises a TypeError

[Gannon McGibbon + Kevin Cheng]
2018-09-18 16:40:10 -04:00
Aaron Patterson
7d58fa87ef
Speed up homogeneous AR lists / reduce allocations
This commit speeds up allocating homogeneous lists of AR objects.  We
can know if the result set contains an STI column before initializing
every AR object, so this change pulls the "does this result set contain
an STI column?" test up, then uses a specialized instantiation function.
This way we only have to check for an STI column once rather than N
times.

This change also introduces a new initialization function that is meant
for use when allocating AR objects that come from the database.  Doing
this allows us to eliminate one hash allocation per AR instance.

Here is a benchmark:

```ruby
require 'active_record'
require 'benchmark/ips'

ActiveRecord::Base.establish_connection adapter: "sqlite3", database: ":memory:"

ActiveRecord::Migration.verbose = false

ActiveRecord::Schema.define do
  create_table :users, force: true do |t|
    t.string :name
    t.timestamps null: false
  end
end

class User < ActiveRecord::Base; end

2000.times do
  User.create!(name: "Gorby")
end

Benchmark.ips do |x|
  x.report("find") do
    User.limit(2000).to_a
  end
end
```

Results:

Before:

```
[aaron@TC activerecord (master)]$ be ruby -I lib:~/git/allocation_tracer/lib speed.rb
Warming up --------------------------------------
                find     5.000  i/100ms
Calculating -------------------------------------
                find     56.192  (± 3.6%) i/s -    285.000  in   5.080940s
```

After:

```
[aaron@TC activerecord (homogeneous-allocation)]$ be ruby -I lib:~/git/allocation_tracer/lib speed.rb
Warming up --------------------------------------
                find     7.000  i/100ms
Calculating -------------------------------------
                find     72.204  (± 2.8%) i/s -    364.000  in   5.044592s
```
2018-06-25 16:00:04 -07:00
Kir Shatrov
831be98f9a Use frozen-string-literal in ActiveRecord 2017-07-19 22:27:07 +03:00
Matthew Draper
87b3e226d6 Revert "Merge pull request #29540 from kirs/rubocop-frozen-string"
This reverts commit 3420a14590c0e6915d8b6c242887f74adb4120f9, reversing
changes made to afb66a5a598ce4ac74ad84b125a5abf046dcf5aa.
2017-07-02 02:15:17 +09:30
Kir Shatrov
cfade1ec7e Enforce frozen string in Rubocop 2017-07-01 02:11:03 +03:00
Eugene Kenny
90f851a18b Remove checks for Enumerator#size method
The Enumerator#size method was introduced in Ruby 2.0.

These tests were added when Rails 4.1 was current, and Ruby 1.9.3 was
still supported. Since Rails 5 only Ruby >= 2.2.2 is supported, so the
checks are no longer necessary.
2017-04-25 07:40:51 -07:00
Rafael Mendonça França
55f9b8129a
Add three new rubocop rules
Style/SpaceBeforeBlockBraces
Style/SpaceInsideBlockBraces
Style/SpaceInsideHashLiteralBraces

Fix all violations in the repository.
2016-08-16 04:30:11 -03:00
Xavier Noria
9617db2078 applies new string literal convention in activerecord/test
The current code base is not uniform. After some discussion,
we have chosen to go with double quotes by default.
2016-08-06 18:26:53 +02:00
Benjamin Quorning
763de2ac1c Optimize ActiveRecord::Result#last
If you only want the last element of a result set, there's no need to create all
of hash_rows. Also, add a test.
2016-06-24 11:43:48 +02:00
Benjamin Quorning
e292ce8a4f Add ActiveRecord::Result#first
When you only need the first element of a result set, you shouldn't need to
instantiate all of hash_rows.
2016-06-24 11:43:22 +02:00
Aaron Patterson
f0c2c6469f add length to ActiveRecord::Result 2014-10-13 12:01:41 -07:00
Sean Griffin
c083ce6dd2 Encapsulate knowledge of type objects on ActiveRecord::Result
Attempting to reduce the number of places that care about the details of
how type casting occurs. We remove the type casting of the primary key
in `JoinDependecy`, rather than encapsulating it. It was originally
added for consistency with
40898c8c19 (diff-06059df8d3dee3101718fb2c01151ad0R211),
but that conditional was later removed in
d7ddaa530f.

What is important is that the same row twice will have the same value
for the primary key, which it will.
2014-06-22 09:00:15 -06:00
Marc-Andre Lafortune
13d2696c10 Return sized enumerator from Batches#find_each 2014-02-05 16:53:02 -05:00
Marc-Andre Lafortune
4499ab5fde Strengthen test with different nb of rows and columns 2014-02-05 16:45:56 -05:00
Carlos Antonio da Silva
755069ee4e Merge pull request #10993 from Empact/result-each-enumerator
Change Result#each to return an Enumerator when called without a block.
2013-06-25 20:07:59 -03:00
Ben Woosley
d6cfbaea72 Change Result#each to return an Enumerator when called without a block.
As with #10992, this lets us call #with_index, etc on the results.
2013-06-18 15:34:18 -07:00