From 620c3afd408ce360b62632fe37c6c9b02e81b9ba Mon Sep 17 00:00:00 2001 From: Omer Ben Yosef Date: Thu, 11 Apr 2024 15:39:53 +0900 Subject: [PATCH] memoize database field names for faster fields access --- lib/mongoid/fields.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/mongoid/fields.rb b/lib/mongoid/fields.rb index 06e9646d1d..d09328fd59 100644 --- a/lib/mongoid/fields.rb +++ b/lib/mongoid/fields.rb @@ -456,7 +456,8 @@ def attribute_names # # @return [ String ] The name of the field as it's stored in the db. def database_field_name(name) - Fields.database_field_name(name, relations, aliased_fields, aliased_associations) + @database_field_names ||= {} + @database_field_names[name] ||= Fields.database_field_name(name, relations, aliased_fields, aliased_associations) end # Defines all the fields that are accessible on the Document @@ -561,6 +562,7 @@ def add_defaults(field) # # @api private def add_field(name, options = {}) + @database_field_names[name] = nil if @database_field_names aliased = options[:as] aliased_fields[aliased.to_s] = name if aliased field = field_for(name, options)