activeldap/activeldap — Issue #27: cut down initialize time
Author: mihu
Created: 2011-12-26 10:28:36+00:00
State: open

Please consider patch inlined below. The boost for initialization in my test case is something like 20%.

The main idea is to go around hooked setter and slow respond_to? method. The bonus is remove of find_every from dirty module, because we are no longer dirtying when initializing.

```
diff --git a/lib/active_ldap/base.rb b/lib/active_ldap/base.rb
index 4c22d90..89661cf 100644
--- a/lib/active_ldap/base.rb
+++ b/lib/active_ldap/base.rb
@@ -1065,10 +1065,25 @@ module ActiveLdap
classes, attributes = extract_object_class(attributes)
self.classes = classes
self.dn = dn
- self.attributes = attributes
+ initialize_attributes(attributes)
yield self if block_given?
end

+ def initialize_attributes(attributes)
+ _schema = _local_entry_attribute = nil
+ targets = sanitize_for_mass_assignment(attributes)
+ targets.each do |key, value|
+ unless have_attribute?(key)
+ _schema ||= schema
+ attribute = _schema.attribute(key)
+ _local_entry_attribute ||= local_entry_attribute
+ _local_entry_attribute.register(attribute)
+ end
+ set_attribute(key, value)
+ end
+ end
+ private :initialize_attributes
+
def instantiate(args)
dn, attributes, options = args
options ||= {}
diff --git a/lib/active_ldap/attribute_methods/dirty.rb b/lib/active_ldap/attribute_methods/dirty.rb
index 735e273..49e464b 100644
--- a/lib/active_ldap/attribute_methods/dirty.rb
+++ b/lib/active_ldap/attribute_methods/dirty.rb
@@ -28,16 +28,6 @@ module ActiveLdap
end
end

- module ClassMethods
- # dirty/clear hack
- # only place that we can ovveride and hook to clear the new record
- def find_every(options)
- super.each do |result|
- result.changed_attributes.clear
- end
- end
- end
-
protected
def attribute=(attr, val)
attribute_will_change!(attr) unless val == get_attribute(attr)
```