rspec-expectations icon indicating copy to clipboard operation
rspec-expectations copied to clipboard

Speed up the ContainExactly matcher

Open bclayman-sq opened this issue 4 years ago • 8 comments

Speed up the ContainExactly matcher by pre-emptively matching up corresponding elements in the expected and actual arrays.

This addresses issues #1006, #1161.

Before this PR, comparing two arrays of 50 random integers never finished. Now, comparing arrays of 10,000 random integers takes 1 second.

This PR is a collaboration between @genehsu and I based on a couple of our earlier PRs and discussion that resulted:

  1. https://github.com/rspec/rspec-expectations/pull/1325
  2. https://github.com/rspec/rspec-expectations/pull/1328

Co-authored-by: Gene Hsu [email protected]

bclayman-sq avatar Oct 29 '21 17:10 bclayman-sq

@genehsu, Could I get your email so I can update the "co-authored-by" section of the PR description?

bclayman-sq avatar Oct 29 '21 17:10 bclayman-sq

If this superseeds the other PR(s) can you close it/them? Will help with the review backlog which is currently sitting at 25 tabs

JonRowe avatar Oct 29 '21 17:10 JonRowe

If this superseeds the other PR(s) can you close it/them? Will help with the review backlog which is currently sitting at 25 tabs

Yeah, I'm happy to! Mostly wanted to be sure Gene was satisfied with this outcome and I wasn't stepping on any toes, so I'll wait for them to comment before closing 👍

bclayman-sq avatar Oct 29 '21 17:10 bclayman-sq

@bclayman-sq

Looks good. I'd suggest that 1000 random elements should be enough since we're hitting timing issues on one of the Jruby test suites.

@genehsu, Could I get your email so I can update the "co-authored-by" section of the PR description?

gene at hsufarm dot com

I'm closing my previous PR now

genehsu avatar Oct 29 '21 18:10 genehsu

@bclayman-sq

Looks good. I'd suggest that 1000 random elements should be enough since we're hitting timing issues on one of the Jruby test suites.

@genehsu, Could I get your email so I can update the "co-authored-by" section of the PR description?

gene at hsufarm dot com

I'm closing my previous PR now

Ah gotcha, I ended up bumping to 2s to get around that pesky jruby one but agree in general that 1000 is probably plenty 👍

Sounds good, will update the PR description and close my earlier PR!

bclayman-sq avatar Oct 29 '21 18:10 bclayman-sq

Great work, can we get a benchmark of the old implementation vs the new implementation added as a comment above the implementation to explain why its so complex, plus I have some wording tweaks for the tests as suggestion, especially the subject bit as we prefer named subjects where possible.

Of course! This is really helpful feedback; I just:

  1. Added a comment with benchmark data to justify the complexity
  2. Incorporated the wording tweaks
  3. Added named subjects where possible

One note on 3.: I didn't change the shared examples to use a named subject like expect_actual_to_contain_exactly_expected instead of subject. The specs use shared examples for both positive and negative expectations, so changing either shared example to use expect_actual_to_contain_exactly_expected or expect_actual_not_to_contain_exactly_expected wouldn't exercise the behavior we want and would cause a couple failing tests. Relying on subject in both shared examples lets me override it as appropriate in the context for a positive expectation and the context for a negative expectation.

Let me know if I've misunderstood anything here and thanks for the review!

bclayman-sq avatar Nov 06 '21 19:11 bclayman-sq

@JonRowe @pirj Hi, hope you two are doing well! I've added a comment with benchmark data per Jon's request and wanted to see if there was anything I could do to get this PR merged in. Let me know!

(I have a couple other PRs from several months ago that I'll comment on similarly!)

bclayman-sq avatar Mar 28 '22 19:03 bclayman-sq

@JonRowe @pirj Hi, hope you two are doing well! I've added a comment with benchmark data per Jon's request and wanted to see if there was anything I could do to get this PR merged in. Let me know!

(I have a couple other PRs from several months ago that I'll comment on similarly!)

👋 Hi @JonRowe, @pirj has approved this and I've managed to incorporate your requested changes (benchmark data, wording tweaks). I followed up a month or so ago and imagine you're slammed. I do still want to get this in, however! Would you feel OK merging this?

If it helps, I'm happy to review a pending PR or two to lighten the load on the core reviewers 👍 Appreciate your help as always!

bclayman-sq avatar Apr 25 '22 23:04 bclayman-sq

Migrated to the monorepo as rspec/rspec#127

JonRowe avatar Nov 30 '24 12:11 JonRowe