Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve performance of importing models with validations #266

Merged
merged 2 commits into from
Jun 22, 2016

Conversation

jkowens
Copy link
Collaborator

@jkowens jkowens commented Apr 4, 2016

Currently, when validating models on import, the models are converted to arrays of values and then are reinitialized as models to call valid? It seems unnecessary to do this extra reinitialization step. Inspired by #118.

Running the MySQL benchmarks on my laptop shows a 2x performance increase (over previous implementation) for importing models with validations.

if models
import_with_validations( column_names, array_of_attributes, options ) do |failed|
models.each_with_index do |model, i|
model = model.dup if options[:recursive]
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Duping the model was necessary when the :recursive option was set so that the parent model would be imported even if child associations failed validation. This is apparently the desired behavior since it is described in this test.

@@ -350,12 +350,7 @@ def import_helper( *args )
# this next line breaks sqlite.so with a segmentation fault
# if model.new_record? || options[:on_duplicate_key_update]
column_names.map do |name|
name = name.to_s
if respond_to?(:defined_enums) && defined_enums.key?(name) # ActiveRecord 5
Copy link
Collaborator Author

@jkowens jkowens Apr 5, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This logic doesn't seem to be necessary if models aren't converted to arrays of values and then back to models for validation.

@jkowens jkowens changed the title Refactor code for validating models on import Improve performance of importing models with validations Jun 13, 2016
@jkowens jkowens force-pushed the model_validation branch 2 times, most recently from 37a24d8 to 92dbc36 Compare June 13, 2016 20:08
# keep track of the instance and the position it is currently at. if this fails
# validation we'll use the index to remove it from the array_of_attributes
model = new
arr.each_with_index do |hsh, i|
hsh.each_pair { |k, v| model[k] = v }
Copy link
Collaborator Author

@jkowens jkowens Jun 13, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zdennis instead of instantiating a new instance for every record, here I changed it to instantiate one instance and just update the attributes on that model. If it fails validation, the model is then duped.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice!

@zdennis
Copy link
Owner

zdennis commented Jun 22, 2016

Thanks @jkowens !

@zdennis zdennis merged commit 8f9c3fd into zdennis:master Jun 22, 2016
@zdennis zdennis added the 0.14.0 label Jun 22, 2016
@zdennis
Copy link
Owner

zdennis commented Jun 22, 2016

This will go out in the 0.14.0 release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants