Last active
November 2, 2015 17:19
-
-
Save olistik/46a0b2879d6b5e7f616a to your computer and use it in GitHub Desktop.
In Rails, when you save an instance variable and the an exception occurs, the error reporting process calls `.inspect` on each instance variable, multiple times. This is extremely bad if you store objects whose representation is really expensive. An example is an Active Record scope matching a lot of objects (> thousands). The application simply…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ApplicationController < ActionController::Base | |
def index | |
foo1 # creates the first instance variable | |
foo2 # creates the second instance variable | |
request.xxx # raises an exception | |
# see the logs, you should expect `.inspect` being called several times for each instance variable | |
end | |
private | |
def foo1 | |
@foo1 ||= Foo.new(:foo1) | |
end | |
def foo2 | |
@foo2 ||= Foo.new(:foo2) | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Foo | |
def initialize(name) | |
@name = name | |
log(:initialize) | |
end | |
def inspect | |
log(:inspect) | |
end | |
def to_s | |
log(:to_s) | |
end | |
def log(context) | |
Rails.logger.debug "XXX Foo(#{@name}).#{context}" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment