aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib
Commit message (Collapse)AuthorAgeFilesLines
* Reduce allocations when running AR callbacks.Pete Higgins2014-09-285-14/+14
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Inspired by @tenderlove's work in c363fff29f060e6a2effe1e4bb2c4dd4cd805d6e, this reduces the number of strings allocated when running callbacks for ActiveRecord instances. I measured that using this script: ``` require 'objspace' require 'active_record' require 'allocation_tracer' ActiveRecord::Base.establish_connection adapter: "sqlite3", database: ":memory:" ActiveRecord::Base.connection.instance_eval do create_table(:articles) { |t| t.string :name } end class Article < ActiveRecord::Base; end a = Article.create name: "foo" a = Article.find a.id N = 10 result = ObjectSpace::AllocationTracer.trace do N.times { Article.find a.id } end result.sort.each do |k,v| p k => v end puts "total: #{result.values.map(&:first).inject(:+)}" ``` When I run this against master and this branch I get this output: ``` pete@balloon:~/projects/rails/activerecord$ git checkout master M Gemfile Switched to branch 'master' pete@balloon:~/projects/rails/activerecord$ bundle exec ruby benchmark_allocation_with_callback_send.rb > allocations_before pete@balloon:~/projects/rails/activerecord$ git checkout remove-dynamic-send-on-built-in-callbacks M Gemfile Switched to branch 'remove-dynamic-send-on-built-in-callbacks' pete@balloon:~/projects/rails/activerecord$ bundle exec ruby benchmark_allocation_with_callback_send.rb > allocations_after pete@balloon:~/projects/rails/activerecord$ diff allocations_before allocations_after 39d38 < {["/home/pete/projects/rails/activesupport/lib/active_support/callbacks.rb", 81]=>[40, 0, 0, 0, 0, 0]} 42c41 < total: 630 --- > total: 590 ``` In addition to this, there are two micro-optimizations present: * Using `block.call if block` vs `yield if block_given?` when the block was being captured already. ``` pete@balloon:~/projects$ cat benchmark_block_call_vs_yield.rb require 'benchmark/ips' def block_capture_with_yield &block yield if block_given? end def block_capture_with_call &block block.call if block end def no_block_capture yield if block_given? end Benchmark.ips do |b| b.report("block_capture_with_yield") { block_capture_with_yield } b.report("block_capture_with_call") { block_capture_with_call } b.report("no_block_capture") { no_block_capture } end pete@balloon:~/projects$ ruby benchmark_block_call_vs_yield.rb Calculating ------------------------------------- block_capture_with_yield 124979 i/100ms block_capture_with_call 138340 i/100ms no_block_capture 136827 i/100ms ------------------------------------------------- block_capture_with_yield 5703108.9 (±2.4%) i/s - 28495212 in 4.999368s block_capture_with_call 6840730.5 (±3.6%) i/s - 34169980 in 5.002649s no_block_capture 5821141.4 (±2.8%) i/s - 29144151 in 5.010580s ``` * Defining and calling methods instead of using send. ``` pete@balloon:~/projects$ cat benchmark_method_call_vs_send.rb require 'benchmark/ips' class Foo def tacos nil end end my_foo = Foo.new Benchmark.ips do |b| b.report('send') { my_foo.send('tacos') } b.report('call') { my_foo.tacos } end pete@balloon:~/projects$ ruby benchmark_method_call_vs_send.rb Calculating ------------------------------------- send 97736 i/100ms call 151142 i/100ms ------------------------------------------------- send 2683730.3 (±2.8%) i/s - 13487568 in 5.029763s call 8005963.9 (±2.7%) i/s - 40052630 in 5.006604s ``` The result of this is making typical ActiveRecord operations slightly faster: https://gist.github.com/phiggins/e46e51dcc7edb45b5f98
* some object allocation reduction for new AR objectsAaron Patterson2014-09-272-3/+7
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | Benchmark: ```ruby require 'objspace' require 'active_record' ActiveRecord::Base.establish_connection adapter: "sqlite3", database: ":memory:" ActiveRecord::Base.connection.instance_eval do create_table(:articles) { |t| t.string :name } end class Article < ActiveRecord::Base; end a = Article.create name: "foo" a = Article.find a.id N = 10 ObjectSpace::AllocationTracer.trace do N.times { Article.find a.id } end ObjectSpace::AllocationTracer.allocated_count_table table.sort_by { |_,x| x }.each do |k,v| p k => (v / N) end ```
* Preparing for 4.2.0.beta2 releaseRafael Mendonça França2014-09-261-1/+1
|
* ar/connection_pool: honor overriden rack.test in middlewareSimon Eskildsen2014-09-231-1/+1
| | | | | | | Honoring an overidden `rack.test` allows testing closed connection between multiple requests. This is useful if you're working on database resiliency, to ensure the connection is in the expected state from one request to another on the same worker.
* add a truncate method to the connectionAaron Patterson2014-09-223-0/+13
| | | | | | it doesn't work on SQLite3 since it doesn't support truncate, but that's OK. If you call truncate on the connection, you're now bound to that database (same as if you use hstore or any other db specific feature).
* Clarify the documentation of the primary_key option for associationsPeter Marsh2014-09-221-1/+1
| | | | | | | Previously the documentation stated that `primary_key` should be the name of a *method* that returns the primary key used for the association. This is incorrect. This changes the documentation to say that the value must be the name of a column.
* fix fixtures not loading if a belongs_to association is defined with a ↵Will Bryant2014-09-221-1/+1
| | | | :foreign_key option that's a symbol
* No need to call to_sym hereGodfrey Chan2014-09-203-10/+10
| | | | | The hash is now string-keyed, and [_]reflect_on_association calls `to_s` on the argument anyway.
* Fix find_by with associations not working with adequate recordGodfrey Chan2014-09-201-0/+3
| | | | | | | | | For now, we will just skip the cache when a non-column key is used in the hash. If the future, we can probably move some of the logic in PredicateBuilder.expand up the chain to make caching possible for association queries. Closes #16903 Fixes #16884
* Quick fix to address the borken build caused by #15791Godfrey Chan2014-09-201-1/+1
| | | | | | This is pretty lame though, so feel free to send a PR with a better fix! ;) cc @zev @tenderlove
* Merge pull request #15791 from zev/add_model_to_recordnotfound_messageAaron Patterson2014-09-191-8/+8
|\ | | | | Update RecordNotFound exception cases to include a message with the
| * Update RecordNotFound exception cases to include a message with theZev Blut2014-06-181-8/+8
| | | | | | | | Model that the Record was not found in.
* | Updated comment about features of fixturesPrathamesh Sonpatki2014-09-191-0/+3
| | | | | | | | | | | | - Added a note about some features which are not dependent on auto-generated ID's. - [ci skip]
* | Use #inject over #sum to build PG create DB statementGeoff Harcourt2014-09-171-2/+2
| | | | | | | | | | | | | | | | | | | | | | | | While investigating #16951 I found that another library's monkey-patching of `Enumerable` was causing the test migrations helper to break when trying to build the `CREATE DATABASE` statement. The prior approach used `#sum` to build the string from the options hash. As the code that combines the options to build the database statement is not user-facing, using `#inject` here instead will remove the only place where the database creation/migration code is dependent on ActiveSupport's monkey-patching of `Enumerable`.
* | Use has_attribute?Rafael Mendonça França2014-09-171-1/+1
| |
* | Merge pull request #16875 from alan/dont_autosave_has_one_through_recordRafael Mendonça França2014-09-171-1/+3
|\ \ | | | | | | | | | | | | | | | | | | Don't autosave unchanged has_one through records Conflicts: activerecord/CHANGELOG.md
| * | Don't autosave unchanged has_one through recordsAlan Kennedy2014-09-151-1/+3
| | |
* | | Merge pull request #16943 from sgrif/sg-fix-tz-converter-serializationRafael Mendonça França2014-09-174-3/+20
|\ \ \ | | | | | | | | Allow YAML serialization when using TZ aware attributes
| * | | Allow YAML serialization when using TZ aware attributesSean Griffin2014-09-174-3/+20
| | | |
* | | | Merge pull request #16929 from ↵Rafael Mendonça França2014-09-171-0/+2
|\ \ \ \ | |/ / / |/| | | | | | | | | | | kongregate/fix-activerecord-deprecated_finders-find-symbol ActiveRecord deprecated finders are broken in 4.2
| * | | Backwards compat for activerecord-deprecated_findersstopdropandrew2014-09-161-0/+2
| |/ / | | | | | | | | | | | | | | | | | | Maintain compatibility for: * ActiveRecord::Base#find(:all) * ActiveRecord::Base#find(:first) * ActiveRecord::Base#find(:last)
* | | do not dump foreign keys for ignored tables.Yves Senn2014-09-171-1/+1
| | |
* | | pg, correctly dump foreign keys targeting tables in a different schema.Yves Senn2014-09-171-1/+2
|/ / | | | | | | | | | | Closes #16907. [Matthew Draper & Yves Senn]
* | Merge pull request #13656 from chanks/rollback_transactions_in_killed_threadsMatthew Draper2014-09-141-5/+11
|\ \ | | | | | | | | | Data corruption risk: Roll back open transactions when the running thread is killed.
| * | Roll back open transactions when the running thread is killed.Chris Hanks2014-08-221-1/+5
| | |
* | | Merge pull request #16669 from aantix/dangerous_attribute_error_better_messageRafael Mendonça França2014-09-121-1/+1
|\ \ \ | | | | | | | | Improved the error messaging for the DangerousAttributeError exception
| * | | Changed the DangerousAttributeError exception message to include messaging ↵Jim Jones2014-09-101-1/+1
| | | | | | | | | | | | | | | | that the conflict could be because of a conflicting attribute.
* | | | A `NullRelation` should represent nothing. Closes #15176.Yves Senn2014-09-111-2/+2
|/ / / | | | | | | | | | | | | | | | [Matthew Draper & Yves Senn] Closes #16860. (pull request to discuss the implementation)
* | | Include default column limits in schema.rbJeremy Kemper2014-09-101-4/+8
| | | | | | | | | | | | | | | | | | Allows :limit defaults to be changed without pulling the rug out from under old migrations that omitted :limit because it matched the default at the time.
* | | MySQL: schema.rb now includes TEXT and BLOB column limits.Jeremy Kemper2014-09-101-4/+7
| | |
* | | MySQL: correct LONGTEXT and LONGBLOB limits from 2GB to their true 4GBJeremy Kemper2014-09-101-2/+2
| | |
* | | Merge pull request #16379 from JackDanger/update-preloader-documentationRafael Mendonça França2014-09-101-23/+32
|\ \ \ | | | | | | | | Updating Associations::Preloader docs
| * | | Updating Associations::Preloader docsJack Danger Canty2014-08-171-23/+32
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Much of the previous documentation introduced features new in 2011. This commit refreshes it to provide clearer code examples and spends more time describing the normal case (preloaded associations) and less time describing the fallback. [ci skip]
* | | | Merge pull request #14056 from girishso/14041Yves Senn2014-09-091-1/+1
|\ \ \ \ | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | SQLite3Adapter now checks for views in table_exists? fixes: 14041 Conflicts: activerecord/CHANGELOG.md
| * | | | SQLite3Adapter now checks for views in table_exists? fixes: 14041Girish S2014-02-141-1/+1
| | | | |
* | | | | introduce `connection.supports_views?` and basic view tests.Yves Senn2014-09-094-4/+17
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | `AbstractAdapter#supports_views?` defaults to `false` so we have to turn it on in adapter subclasses. Currently the flag only controls test execution. /cc @yahonda
* | | | | Allow included modules to override association methods.Yves Senn2014-09-092-2/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Closes #16684. This is achieved by always generating `GeneratedAssociationMethods` when `ActiveRecord::Base` is subclassed. When some of the included modules of `ActiveRecord::Base` were reordered this behavior was broken as `Core#initialize_generated_modules` was no longer called. Meaning that the module was generated on first access.
* | | | | Merge pull request #16825 from cristianbica/fix-ar-nested-arraysMatthew Draper2014-09-081-4/+12
|\ \ \ \ \ | | | | | | | | | | | | Fix query with nested array in Active Record
| * | | | | Fix query with nested array in Active RecordCristian Bica2014-09-061-4/+12
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | `User.where(id: [[1,2],3])` was equal to `User.where(id:[1, 2, 3])` in Rails 4.1.x but because of some refactoring in Arel this stopped working in 4.2.0. This fixes it in Rails. [Dan Olson & Cristian Bica]
* | | | | | Schema dumper: all connection adapters implement #primary_key, so rely on it ↵Jeremy Kemper2014-09-071-10/+1
| | | | | | | | | | | | | | | | | | | | | | | | exclusively
* | | | | | Fix warnings for undefined local variableTee Parham2014-09-071-6/+10
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | * Add private method primary_key_for, which more clearly shows that the expected return value is nil when a primary key is not found.
* | | | | | Prefer "if any?" to "unless empty?"Tee Parham2014-09-071-9/+6
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | * Consistent whitespace * Remove unnecessary parentheses
* | | | | | Convert string concatenations to substitutionsTee Parham2014-09-071-15/+15
|/ / / / /
* | | | | Do not mark object as persisted after an association is savedRafael Mendonça França2014-09-051-2/+0
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Callback order in Active Record objects are important. Users should not define callbacks before the association definition or surprising behaviours like the described at #3798 will happen. This callback order dependency is documented at https://github.com/rails/rails/blob/31bfcdc77ca0d8cec9b5fe513bdc6f05814dd4f1/activerecord/lib/active_record/associations.rb#L1222-1227. This reverts #15728. Fixes #16620.
* | | | | Freeze ADAPTER_NAME in adaptersAbdelkader Boudih2014-09-056-17/+6
| | | | |
* | | | | Fixed regression with referencing polymorphic assoc in eager-loadGodfrey Chan2014-09-052-2/+3
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This is cased by 03118bc + 9b5d603. The first commit referenced the undefined local variable `column` when it should be using `reflection.type` as the lookup key. The second commit changed `build_arel` to not modify the `bind_values` in- place so we need to combine the arel's `bind_values` with the relation's when building the SQL. Fixes #16591 Related #15821 / #15892 / 7aeca50
* | | | | Merge pull request #16788 from codeodor/fix-16761Aaron Patterson2014-09-043-2/+3
|\ \ \ \ \ | | | | | | | | | | | | Skip StatementCache for eager loaded associations
| * | | | | Skip StatementCache for eager loaded associations (Fixes #16761)Sammy Larbi2014-09-043-2/+3
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Eagerly loaded collection and singular associations are ignored by the StatementCache, which causes errors when the queries they generate reference columns that were not eagerly loaded. This commit skips the creation of the StatementCache as a fix for these scenarios.
* | | | | | always reorder bind parameters. fixes #15920Aaron Patterson2014-09-041-6/+4
|/ / / / /
* | | | | Merge pull request #16801 from ↵Rafael Mendonça França2014-09-042-7/+6
|\ \ \ \ \ | | | | | | | | | | | | | | | | | | | | | | | | eileencodes/refactor-scope_chain-on-through-refelction-to-eliminate-branch-in-eval_scope Always add lambda to scope chain to eliminate branch in eval_scope