Skip to content

Optimize option value detection#481

Closed
Watson1978 wants to merge 1 commit intoruby:masterfrom
Watson1978:optimize-option
Closed

Optimize option value detection#481
Watson1978 wants to merge 1 commit intoruby:masterfrom
Watson1978:optimize-option

Conversation

@Watson1978
Copy link
Contributor

This patch introduces functions same as rb_hash_has_key()
to reduce rb_funcall() calling because it has a overhead.

before after result
JSON.parse 302.109k 343.547k 1.14x
JSON.generate 569.705k 568.668k

Environment

  • MacBook Air (M1, 2020)
  • macOS 12.0 beta
  • Apple M1
  • Ruby 3.0.2

Before

Warming up --------------------------------------
          JSON.parse    30.347k i/100ms
       JSON.generate    56.987k i/100ms
Calculating -------------------------------------
          JSON.parse    302.109k (± 0.2%) i/s -      1.517M in   5.022546s
       JSON.generate    569.705k (± 0.2%) i/s -      2.849M in   5.001462s

After

Warming up --------------------------------------
          JSON.parse    35.109k i/100ms
       JSON.generate    57.611k i/100ms
Calculating -------------------------------------
          JSON.parse    343.547k (± 0.3%) i/s -      1.720M in   5.007637s
       JSON.generate    568.668k (± 0.3%) i/s -      2.881M in   5.065486s

Test code

require 'benchmark/ips'
require 'json'

json =<<-EOF
{
  "$id": "https://example.com/person.schema.json",
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "title": "Person",
  "type": "object",
  "properties": {
    "firstName": {
      "type": "string",
      "description": "The person's first name."
    },
    "lastName": {
      "type": "string",
      "description": "The person's last name."
    },
    "age": {
      "description": "Age in years which must be equal to or greater than zero.",
      "type": "integer",
      "minimum": 0
    }
  }
}
EOF

Benchmark.ips do |x|
  x.report('JSON.parse') { JSON.parse(json, symbolize_names: true) }

  data = JSON.parse(json, symbolize_names: true)
  x.report('JSON.generate') { JSON.generate(json, indent: "\t") }
end

This patch introduces functions same as rb_hash_has_key()
to reduce rb_funcall() calling because it has a overhead.

−               | before   | after    | result
--               | --       | --       | --
JSON.parse       | 302.109k | 343.547k | 1.14x
JSON.generate    | 569.705k | 568.668k | −

### Environment
- MacBook Air (M1, 2020)
- macOS 12.0 beta
- Apple M1
- Ruby 3.0.2

### Before
```
Warming up --------------------------------------
          JSON.parse    30.347k i/100ms
       JSON.generate    56.987k i/100ms
Calculating -------------------------------------
          JSON.parse    302.109k (± 0.2%) i/s -      1.517M in   5.022546s
       JSON.generate    569.705k (± 0.2%) i/s -      2.849M in   5.001462s
```

### After
```
Warming up --------------------------------------
          JSON.parse    35.109k i/100ms
       JSON.generate    57.611k i/100ms
Calculating -------------------------------------
          JSON.parse    343.547k (± 0.3%) i/s -      1.720M in   5.007637s
       JSON.generate    568.668k (± 0.3%) i/s -      2.881M in   5.065486s
```

### Test code
```ruby
require 'benchmark/ips'
require 'json'

json =<<-EOF
{
  "$id": "https://example.com/person.schema.json",
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "title": "Person",
  "type": "object",
  "properties": {
    "firstName": {
      "type": "string",
      "description": "The person's first name."
    },
    "lastName": {
      "type": "string",
      "description": "The person's last name."
    },
    "age": {
      "description": "Age in years which must be equal to or greater than zero.",
      "type": "integer",
      "minimum": 0
    }
  }
}
EOF

Benchmark.ips do |x|
  x.report('JSON.parse') { JSON.parse(json, symbolize_names: true) }

  data = JSON.parse(json, symbolize_names: true)
  x.report('JSON.generate') { JSON.generate(json, indent: "\t") }
end
```
@byroot
Copy link
Member

byroot commented Oct 17, 2024

Superseeded by: #622

Thank you.

@byroot byroot closed this Oct 17, 2024
@Watson1978 Watson1978 deleted the optimize-option branch October 17, 2024 23:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants