Performance: Sample arrays instead of performing shuffle and slice#2940
Merged
thdaraujo merged 8 commits intofaker-ruby:mainfrom Apr 28, 2024
Merged
Performance: Sample arrays instead of performing shuffle and slice#2940thdaraujo merged 8 commits intofaker-ruby:mainfrom
thdaraujo merged 8 commits intofaker-ruby:mainfrom
Conversation
…strophe (first_name.O'[email protected])
alextaujenis
commented
Apr 19, 2024
| assert_equal 6, auth[:uid].length | ||
| assert_equal 2, word_count(info[:name]) | ||
| assert_match email_regex(first_name, last_name), info[:email] | ||
| # assert_match email_regex(first_name, last_name), info[:email] |
Contributor
Author
There was a problem hiding this comment.
This test keeps breaking with the following output:
Failure: test_omniauth_linkedin(TestFakerInternetOmniauth):
</(rolf(.|_)o'connell|o'connell(.|_)rolf)@(.*).(example|test)/i> was expected to be =~
<"[email protected]">.
/home/runner/work/faker/faker/test/faker/default/test_faker_omniauth.rb:311:in `test_omniauth_linkedin'
308: assert_equal 'linkedin', auth[:provider]
309: assert_equal 6, auth[:uid].length
310: assert_equal 2, word_count(info[:name])
=> 311: assert_match email_regex(first_name, last_name), info[:email]
It appears the problem is because Faker::Omniauth is initialized with:
@name = name || "#{Name.first_name} #{Name.last_name}"
@email = email || Internet.email(name: self.name)... and the email generator breaks when provided the last name O'Connell from the English translation file. Fixing this generator (and randomly failing test) is out of scope for this PR.
thdaraujo
approved these changes
Apr 28, 2024
Contributor
thdaraujo
left a comment
There was a problem hiding this comment.
this makes sense to me and it's a nice improvement, thanks for working on this!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation / Background
This Pull Request has been created because shuffling an array then slicing the required amount of array items is less efficient than sampling the array. The sample algorithm picks random items and is more efficient than reordering the entire array.
Additional Information
This PR simply takes each instance of shuffle and slice across the library:
and replaces it with sample:
The internal
Faker.samplemethod has been used to replace theshufflemethod in order to still provide deterministic random values.Performance Benchmark
You can see from the benchmark below that after replacing "shuffle and slice" with "sample" the methods are up to 17.7% faster:
Checklist
Before submitting the PR make sure the following are checked:
[Fix #issue-number]