Watson
|
7611463f02
|
Increase performance in Liquid::Lexer#tokenize
To obtain String Literal, the regular expression might be executed at two times.
It would be slightly faster to run them all at once using `Regexp.union`.
− | before | after | result
-- | -- | -- | --
parse | 63.418 | 65.183 | 1.028x
render | 195.389 | 195.648 | -
parse & render | 46.091 | 46.917 | 1.018x
### Environment
- MacBook Pro (14 inch, 2021)
- macOS 13.0 Beta
- Apple M1 Max
- Ruby 3.1.2
### Before
```
Running benchmark for 10 seconds (with 5 seconds warmup).
Warming up --------------------------------------
parse: 6.000 i/100ms
render: 19.000 i/100ms
parse & render: 4.000 i/100ms
Calculating -------------------------------------
parse: 63.418 (± 0.0%) i/s - 636.000 in 10.028939s
render: 195.389 (± 0.5%) i/s - 1.957k in 10.016466s
parse & render: 46.091 (± 0.0%) i/s - 464.000 in 10.067445s
```
### After
```
Running benchmark for 10 seconds (with 5 seconds warmup).
Warming up --------------------------------------
parse: 6.000 i/100ms
render: 19.000 i/100ms
parse & render: 4.000 i/100ms
Calculating -------------------------------------
parse: 65.183 (± 0.0%) i/s - 654.000 in 10.033549s
render: 195.648 (± 1.0%) i/s - 1.957k in 10.003511s
parse & render: 46.917 (± 0.0%) i/s - 472.000 in 10.060782s
```
|
2022-08-24 04:23:05 +09:00 |
|
Watson
|
fad58ef436
|
Use String#match? instead of String#=~ to reduce allocation for backreferecne
## Test code
```ruby
require 'benchmark/ips'
WhitespaceOrNothing = /\A\s*\z/
token = " " * 20
token =~ WhitespaceOrNothing
Benchmark.ips do |x|
x.report("=~") {
token =~ WhitespaceOrNothing
}
x.report("match?") {
token.match?(WhitespaceOrNothing)
}
x.compare!
end
```
## Result
```
Warming up --------------------------------------
=~ 271.356k i/100ms
match? 579.655k i/100ms
Calculating -------------------------------------
=~ 2.717M (± 0.4%) i/s - 13.839M in 5.092947s
match? 5.695M (± 1.6%) i/s - 28.983M in 5.090640s
Comparison:
match?: 5694747.3 i/s
=~: 2717370.9 i/s - 2.10x (± 0.00) slower
```
|
2022-03-16 12:44:27 +09:00 |
|
Watson
|
22568080b1
|
Revert "Use strip & empty? to detect Whitespaces"
This reverts commit dd7ed00ec4.
|
2022-03-16 12:33:45 +09:00 |
|
Watson
|
dd7ed00ec4
|
Use strip & empty? to detect Whitespaces
## Test code
```ruby
require 'benchmark/ips'
WhitespaceOrNothing = /\A\s*\z/
token = " " * 20
Benchmark.ips do |x|
x.report("WhitespaceOrNothing") {
token =~ WhitespaceOrNothing
}
x.report("strip & empty?") {
token.strip.empty?
}
x.compare!
end
```
## Result
```
Warming up --------------------------------------
WhitespaceOrNothing 266.391k i/100ms
strip & empty? 1.044M i/100ms
Calculating -------------------------------------
WhitespaceOrNothing 2.705M (± 0.4%) i/s - 13.586M in 5.023453s
strip & empty? 10.400M (± 1.1%) i/s - 52.182M in 5.017990s
Comparison:
strip & empty?: 10400286.2 i/s
WhitespaceOrNothing: 2704552.3 i/s - 3.85x (± 0.00) slower
```
|
2022-03-12 19:24:56 +09:00 |
|
Watson
|
1667c1180e
|
Use start_with? and end_with? to detect SQUARE_BRAKET
## Test code
```ruby
require 'benchmark/ips'
SQUARE_BRACKETED = /\A\[(.*)\]\z/m
markup = "[product.catchall]"
Benchmark.ips do |x|
x.report("SQUARE_BRACKETED") {
if markup =~ SQUARE_BRACKETED
Regexp.last_match(1)
end
}
x.report("start/end_with?") {
if markup&.start_with?('[') && markup&.end_with?(']')
markup[1..-2]
end
}
x.compare!
end
```
## Result
```
Warming up --------------------------------------
SQUARE_BRACKETED 261.300k i/100ms
start/end_with? 548.813k i/100ms
Calculating -------------------------------------
SQUARE_BRACKETED 2.632M (± 0.6%) i/s - 13.326M in 5.064085s
start/end_with? 5.471M (± 0.5%) i/s - 27.441M in 5.015770s
Comparison:
start/end_with?: 5470994.1 i/s
SQUARE_BRACKETED: 2631642.3 i/s - 2.08x (± 0.00) slower
```
|
2022-03-12 18:14:16 +09:00 |
|
Watson
|
ebdfdb80e5
|
Detect quoted string using String#{start_with?, end_with?} to reduce Regexp#=== calling
|
2021-09-26 04:30:49 +09:00 |
|
Watson
|
95e9fa5010
|
Use String#=~ and Regexp.last_match instead to retrieve the markup content
If the first value is only used obtained with String#scan,
it will increase the performance if replace with `String#=~` and `Regexp.last_match`.
### Environment
- MacBook Air (M1, 2020)
- macOS 12.0 beta 7
- Apple M1
- Ruby 3.0.2
### Test code
```ruby
require 'benchmark/ips'
WhitespaceControl = '-'
VariableStart = /\{\{/
VariableEnd = /\}\}/
ContentOfVariable = /\A#{VariableStart}#{WhitespaceControl}?(.*?)#{WhitespaceControl}?#{VariableEnd}\z/om
token = "{{item.product.featured_image | product_img_url: 'thumb' }}"
Benchmark.ips do |x|
x.report("String#scan") { token.scan(ContentOfVariable) {|content| break } }
x.report("String#match") { m = token.match(ContentOfVariable); m[1] }
x.report("String#=~") { token =~ ContentOfVariable; Regexp.last_match(1) }
x.compare!
end
```
### Result
```
Warming up --------------------------------------
String#scan 135.724k i/100ms
String#match 117.397k i/100ms
String#=~ 151.637k i/100ms
Calculating -------------------------------------
String#scan 1.351M (± 0.8%) i/s - 6.786M in 5.021955s
String#match 1.169M (± 1.3%) i/s - 5.870M in 5.020429s
String#=~ 1.520M (± 0.9%) i/s - 7.733M in 5.087427s
Comparison:
String#=~: 1520250.9 i/s
String#scan: 1351399.0 i/s - 1.12x (± 0.00) slower
String#match: 1169384.1 i/s - 1.30x (± 0.00) slower
```
|
2021-09-26 04:12:53 +09:00 |
|