1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
Ruby on Rails
Find a file
Aaron Patterson 960de47f0e drop array allocations when iterating over the hash
`each_with_object` allocates an array for each kv pair.  Switching to
the slightly more verbose but less allocatey `each_pair` eliminates
array allocations.  Eliminating this allocation returns AR objects to
have constant array allocations regardless of the number of columns the
object has.

Here is test code:

```ruby
require 'active_record'

class Topic < ActiveRecord::Base
end

20.times do |i|
  Process.waitpid fork {
    ActiveRecord::Base.establish_connection adapter: 'sqlite3', database: ':memory:'

    ActiveRecord::Base.connection.instance_eval do
      create_table(:topics) do |t|
        t.string   :title, limit: 250
        t.string   :author_name
        t.string   :author_email_address
        t.string   :parent_title
        t.string   :type
        t.string   :group
        i.times do |j|
          t.string :"aaa#{j}"
        end
        t.timestamps null: true
      end
    end

    ObjectSpace::AllocationTracer.setup(%i{type})

    Topic.create title: "aaron" # heat cache

    result = ObjectSpace::AllocationTracer.trace do
      10.times do |i|
        Topic.create title: "aaron #{i}"
      end
    end

    puts "#{Topic.columns.length},#{(result.find { |k,v| k.first == :T_ARRAY }.last.first / 10)}"
  }
end
```

Before this commit:

```
9,166
10,167
11,168
12,169
13,170
14,171
15,172
16,173
17,174
18,175
19,176
20,177
21,178
22,179
23,180
24,181
25,182
26,183
27,184
28,185
```

After:

```
9,157
10,157
11,157
12,157
13,157
14,157
15,157
16,157
17,157
18,157
19,157
20,157
21,157
22,157
23,157
24,157
25,157
26,157
27,157
28,157
```

Left side is the number of columns, right is the number of allocations
2015-10-15 14:44:28 -07:00
actionmailer
actionpack
actionview
activejob
activemodel All strings returned by ImmutableString should be frozen 2015-10-15 09:55:30 -07:00
activerecord Add an immutable string type to opt out of string duping 2015-10-15 09:50:37 -07:00
activesupport drop array allocations when iterating over the hash 2015-10-15 14:44:28 -07:00
ci
guides Update active_record_migrations.md 2015-10-15 16:37:11 -04:00
railties
tasks
tools
.gitignore
.travis.yml
.yardopts
CODE_OF_CONDUCT.md
CONTRIBUTING.md
Gemfile
Gemfile.lock
load_paths.rb
rails.gemspec
RAILS_VERSION
Rakefile
README.md
RELEASING_RAILS.md
version.rb

Welcome to Rails

Rails is a web-application framework that includes everything needed to create database-backed web applications according to the Model-View-Controller (MVC) pattern.

Understanding the MVC pattern is key to understanding Rails. MVC divides your application into three layers, each with a specific responsibility.

The Model layer represents your domain model (such as Account, Product, Person, Post, etc.) and encapsulates the business logic that is specific to your application. In Rails, database-backed model classes are derived from ActiveRecord::Base. Active Record allows you to present the data from database rows as objects and embellish these data objects with business logic methods. You can read more about Active Record in its README. Although most Rails models are backed by a database, models can also be ordinary Ruby classes, or Ruby classes that implement a set of interfaces as provided by the Active Model module. You can read more about Active Model in its README.

The Controller layer is responsible for handling incoming HTTP requests and providing a suitable response. Usually this means returning HTML, but Rails controllers can also generate XML, JSON, PDFs, mobile-specific views, and more. Controllers load and manipulate models, and render view templates in order to generate the appropriate HTTP response. In Rails, incoming requests are routed by Action Dispatch to an appropriate controller, and controller classes are derived from ActionController::Base. Action Dispatch and Action Controller are bundled together in Action Pack. You can read more about Action Pack in its README.

The View layer is composed of "templates" that are responsible for providing appropriate representations of your application's resources. Templates can come in a variety of formats, but most view templates are HTML with embedded Ruby code (ERB files). Views are typically rendered to generate a controller response, or to generate the body of an email. In Rails, View generation is handled by Action View. You can read more about Action View in its README.

Active Record, Active Model, Action Pack, and Action View can each be used independently outside Rails. In addition to them, Rails also comes with Action Mailer (README), a library to generate and send emails; Active Job (README), a framework for declaring jobs and making them run on a variety of queueing backends; and Active Support (README), a collection of utility classes and standard library extensions that are useful for Rails, and may also be used independently outside Rails.

Getting Started

  1. Install Rails at the command prompt if you haven't yet:

     gem install rails
    
  2. At the command prompt, create a new Rails application:

     rails new myapp
    

    where "myapp" is the application name.

  3. Change directory to myapp and start the web server:

     cd myapp
     rails server
    

    Run with --help or -h for options.

  4. Using a browser, go to http://localhost:3000 and you'll see: "Welcome aboard: You're riding Ruby on Rails!"

  5. Follow the guidelines to start developing your application. You may find the following resources handy:

Contributing

We encourage you to contribute to Ruby on Rails! Please check out the Contributing to Ruby on Rails guide for guidelines about how to proceed. Join us!

Everyone interacting in Rails and its sub-projects' codebases, issue trackers, chat rooms, and mailing lists is expected to follow the Rails code of conduct.

Code Status

Build Status

License

Ruby on Rails is released under the MIT License.