Commit aec385b
authored
Avoid needless word-list copies in Lorem.words (#3292)
Only concatenate the supplemental list when requested, and only
duplicate the word list when more words are asked for than the list
contains. The list returned by translate is never mutated, since it may
be the array cached by the I18n backend.
Benchmark (Ruby 3.4.9, arm64-darwin25, benchmark-ips):
require 'benchmark/ips'
require 'faker'
Benchmark.ips do |x|
x.config(warmup: 2, time: 5)
x.report('words(number: 4)') { Faker::Lorem.words(number: 4) }
x.report('sentence') { Faker::Lorem.sentence }
x.report('paragraph') { Faker::Lorem.paragraph }
end
Results:
main: words(number: 4) 126.489k (+/- 2.2%) i/s
sentence 40.797k (+/- 4.4%) i/s
paragraph 12.455k (+/- 0.7%) i/s
this commit: words(number: 4) 132.863k (+/- 1.0%) i/s (~1.05x)
sentence 43.273k (+/- 0.4%) i/s (~1.06x)
paragraph 12.733k (+/- 1.4%) i/s (~1.02x)1 parent a85b836 commit aec385b
1 file changed
Lines changed: 6 additions & 5 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
36 | 36 | | |
37 | 37 | | |
38 | 38 | | |
39 | | - | |
40 | | - | |
41 | | - | |
42 | | - | |
| 39 | + | |
| 40 | + | |
43 | 41 | | |
44 | 42 | | |
45 | 43 | | |
46 | 44 | | |
47 | | - | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
48 | 49 | | |
49 | 50 | | |
50 | 51 | | |
| |||
0 commit comments