Skip to content

Commit

Permalink
Improve performance of Promo Code Batch Builder
Browse files Browse the repository at this point in the history
As pointed out in
https://github.com/solidusio/solidus/pull/5383/files#r1391519251, the
new promotion code batch builder had worse performance characteristics
than the one in the legacy promotion system. This gets the
characteristics back to those of the legacy promotion system.

Time spent and memory usage still go up significantly and linearly with
the number of batches. I ran some specs with the following code:

```rb
  context "with a very large number of promotion codes" do
    let(:number_of_codes) { 10000 }

    it "creates the correct number of codes" do
      puts Benchmark.measure { subject.build_promotion_codes }
      expect(promotion.codes.size).to eq(number_of_codes)
    end
  end
```

With a 1000 codes, I measured:

```
Randomized with seed 49097
  1.036322   0.028977   1.065299 (  1.074396)
```

With 10000 codes, I measured:

```
Randomized with seed 33250
  9.606364   0.278920   9.885284 (  9.978242)
```

Memory usage went up linearly as well.
  • Loading branch information
mamhoff committed Sep 2, 2024
1 parent 22b5391 commit 349215f
Showing 1 changed file with 4 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,16 @@ def generate_random_codes
new_codes = Array.new(max_codes_to_generate) { generate_random_code }.uniq
codes_for_current_batch = get_unique_codes(new_codes)

codes_for_current_batch = codes_for_current_batch.map do |value|
codes_for_current_batch.filter! do |value|
SolidusPromotions::PromotionCode.create!(
value: value,
promotion: promotion,
promotion_code_batch: promotion_code_batch
)
rescue ActiveRecord::RecordInvalid
nil
end.compact
false
end

created_codes += codes_for_current_batch.size
end
end
Expand Down

0 comments on commit 349215f

Please sign in to comment.