Skip to content

Commit

Permalink
Speed up performance of default application.
Browse files Browse the repository at this point in the history
- Ruby's instance_exec was just too slow for applying defaults, and
  since the id and type fields were now depending on this their
  performance was affected.
  • Loading branch information
durran committed Apr 30, 2012
1 parent 14ecca2 commit f45ec14
Showing 1 changed file with 49 additions and 1 deletion.
50 changes: 49 additions & 1 deletion lib/mongoid/fields/standard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,53 @@ def versioned?

private

# Get the name of the default method for this field.
#
# @api private
#
# @example Get the default name.
# field.default_name
#
# @return [ String ] The method name.
#
# @since 3.0.0
def default_name
@default_name ||= "__#{name}_default__"
end

# Has the default method been defined?
#
# @api private
#
# @example Is the default method defined?
# field.default_defined?
#
# @return [ true, false, nil ] If the default is defined.
#
# @since 3.0.0
def default_defined?
@default_defined
end

# Define the method for getting the default on the document.
#
# @api private
#
# @example Define the method.
# field.define_default_method(doc)
#
# @note Ruby's instance_exec was just too slow.
#
# @param [ Document ] doc The document.
#
# @since 3.0.0
def define_default_method(doc)
unless default_defined?
doc.class.__send__(:define_method, default_name, default_val)
@default_defined = true
end
end

# Is the field included in the fields that were returned from the
# database? We can apply the default if:
# 1. The field is included in an only limitation (field: 1)
Expand Down Expand Up @@ -215,7 +262,8 @@ def evaluated_default(doc)
#
# @since 3.0.0
def evaluate_default_proc(doc)
serialize_default(doc.instance_exec(&default_val))
define_default_method(doc)
serialize_default(doc.__send__(default_name))
end

# This is used when default values need to be serialized. Most of the
Expand Down

0 comments on commit f45ec14

Please sign in to comment.