Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize parsing option args in JSON.parse #720

Merged
merged 1 commit into from Nov 6, 2021

Conversation

Watson1978
Copy link
Collaborator

This PR will optimize the parsing options in JSON.parse.
It looked up one by one to make sure that the supported options were included in passed options.
This approach of looking up the passed hash each time has an overhead.

This PR will apply the same changing as #683

before after result
Oj.dump 518.709k 528.462k 1.019x

Environment

  • MacBook Pro (M1 Max, 2021)
  • macOS 12.0
  • Apple M1 Max
  • Ruby 3.0.2

Before

Warming up --------------------------------------
          JSON.parse    51.973k i/100ms
Calculating -------------------------------------
          JSON.parse    518.709k (± 0.3%) i/s -      5.197M in  10.019799s

After

Warming up --------------------------------------
          JSON.parse    52.534k i/100ms
Calculating -------------------------------------
          JSON.parse    528.462k (± 0.5%) i/s -      5.306M in  10.040606s

Test code

require 'benchmark/ips'
require 'oj'

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.warmup = 10
  x.time = 10

  Oj.mimic_JSON
  x.report('JSON.parse') { JSON.parse(json, symbolize_names: true) }
end

This PR will optimize the parsing options in `JSON.parse`.
It looked up one by one to make sure that the supported options were included in passed options.
This approach of looking up the passed hash each time has an overhead.

This PR will apply the same changing as ohler55#683

−               | before   | after    | result
--               | --       | --       | --
Oj.dump          | 518.709k | 528.462k | 1.019x

### Environment
- MacBook Pro (M1 Max, 2021)
- macOS 12.0
- Apple M1 Max
- Ruby 3.0.2

### Before
```
Warming up --------------------------------------
          JSON.parse    51.973k i/100ms
Calculating -------------------------------------
          JSON.parse    518.709k (± 0.3%) i/s -      5.197M in  10.019799s
```

### After
```
Warming up --------------------------------------
          JSON.parse    52.534k i/100ms
Calculating -------------------------------------
          JSON.parse    528.462k (± 0.5%) i/s -      5.306M in  10.040606s
```

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

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.warmup = 10
  x.time = 10

  Oj.mimic_JSON
  x.report('JSON.parse') { JSON.parse(json, symbolize_names: true) }
end
```
@ohler55 ohler55 merged commit ed03e0e into ohler55:develop Nov 6, 2021
@ohler55
Copy link
Owner

ohler55 commented Nov 6, 2021

Looks good, thank you.

@Watson1978 Watson1978 deleted the optimize-parsing-option branch November 6, 2021 15:59
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.

None yet

2 participants