
Excessive validation when creating multiple children at once
Reported by Greg Campbell | December 15th, 2009 @ 03:54 PM | in 1.0.0
The following code causes the parent in a one-to-many relationship to be validated once for every child being created.
require 'rubygems'
require 'dm-core'
require 'dm-validations'
DataMapper::Logger.new(STDOUT, :debug)
DataMapper.setup(:default, 'sqlite3::memory')
class Zoo
include DataMapper::Resource
property :id, Serial
property :name, String, :unique_index => true
#before :valid?, :validating
validates_is_unique :name
has n, :animals
end
class Animal
include DataMapper::Resource
property :id, Serial
property :species, String
belongs_to :zoo
end
DataMapper.auto_migrate!
z = Zoo.new(:name => "name")
50.times {|i| z.animals.new(:species => i.to_s)}
z.save #Note that the uniqueness query on Zoo is performed once per animal: SELECT "id" FROM "zoos" WHERE "name" = 'name' ORDER BY "id" LIMIT 1
Comments and changes to this ticket
-
Greg Campbell December 15th, 2009 @ 04:14 PM
From further research, the culprit appears to be line 60 of dm-validations.rb:
return false unless validation_context_stack.empty? || valid?(current_validation_context)
This is called in the course of save_parents for each child resource, and will always run validations. I was able to "fix" it by changing the code to:
return false unless !dirty_self? || validation_context_stack.empty? || valid?(current_validation_context)
...but have not yet been able to verify whether this might cause other problems.
-
Greg Campbell December 15th, 2009 @ 04:27 PM
Even with the aforementioned fix, though, save!() is still far faster than save() in my use case (~200 milliseconds vs. ~16 seconds), so there must be something else causing slowness here. I'll continue investigating.
-
Greg Campbell December 15th, 2009 @ 04:40 PM
Apparently the remaining slowness was caused by dm-timestamps, which I have since removed. I'll work on a patch for dm-validations, and figure out why dm-timestamps was causing such a performance degradation.
-
Greg Campbell December 16th, 2009 @ 12:09 PM
- Tag changed from dm-validations to dm-validations, performance
See ticket #1155 for the related dm-timestamps issue.
-
Greg Campbell December 16th, 2009 @ 12:50 PM
- Tag changed from dm-validations, performance to dm-validations, performance, regression
Tagging this as a regression - see ticket #1155 for the justification. If this is inappropriate, please remove the tag, of course.
-
Greg Campbell December 28th, 2009 @ 02:46 PM
- State changed from new to unconfirmed
martinemde posted a related gist: http://gist.github.com/264903 - apparently before save hooks also exhibit this behavior.
-
Dan Kubb (dkubb) February 2nd, 2010 @ 02:41 AM
- State changed from unconfirmed to accepted
- Assigned user set to Dan Kubb (dkubb)
-
Dan Kubb (dkubb) February 2nd, 2010 @ 03:05 AM
martinemde's issue is resolved with the fix to #1155.
-
Dan Kubb (dkubb) February 2nd, 2010 @ 03:18 AM
I will be adding this fix, and make Resource#dirty_self? semipublic to allow plugins like this to use it.
-
Dan Kubb (dkubb) February 2nd, 2010 @ 03:58 AM
- State changed from accepted to resolved
(from [178bc58788b1e425a07ee3ba7871771cf22e2925]) Only validate a dirty resource when saving
- Prevents multiple validations when saving parent and children, since previously every child that was saved would tell the parent to save too. Now the parent will be saved, and will not be be dirty any longer, so the unnecessary validation will be skipped.
[#1154 state:resolved] http://github.com/datamapper/dm-more/commit/178bc58788b1e425a07ee3b...
-
Dan Kubb (dkubb) February 2nd, 2010 @ 03:59 AM
(from [273f48b147245701ebf610d944b46a9792a13985]) Update Resource#dirty_self? to be semipublic
[#1154] http://github.com/datamapper/dm-core/commit/273f48b147245701ebf610d...
-
Dan Kubb (dkubb) February 2nd, 2010 @ 04:23 AM
- Milestone set to 1.0.0
-
Dan Kubb (dkubb) February 2nd, 2010 @ 04:23 AM
- no changes were found...
Please Sign in or create a free account to add a new ticket.
With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
People watching this ticket
Referenced by
-
1154 Excessive validation when creating multiple children at once [#1154 state:resolved] http://github.com/datamapper/dm-m...
-
1154 Excessive validation when creating multiple children at once [#1154] http://github.com/datamapper/dm-core/commit/273f...