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

Perf. improvements to Hash#to_json in pure implementation generator. #203

Merged
merged 1 commit into from Dec 1, 2023

Conversation

vipulnsward
Copy link
Contributor

Perf. improvements to Hash#to_json in pure implementation generator:

  • Use .each instead of for for iterating over elements.
  • Initialize delim, result vars with initial states instead of appending state.object_nl later.
  • Use faster string interpolation where possible on Hash#json_transform instead of appending strings repeatedly.

Benchmark:

    hash ={:a => {:a => {:a => {:a => {:a => 1}}}}}
    require 'benchmark/ips'
    Benchmark.ips do |x|
      x.report("to_json ") {  hash.to_json}
    end
Before:
Calculating -------------------------------------
            to_json       4990 i/100ms
-------------------------------------------------
            to_json     52970.8 (±5.9%) i/s -     264470 in   5.010558s

After:
Calculating -------------------------------------
            to_json       5253 i/100ms
-------------------------------------------------
            to_json     55998.6 (±5.9%) i/s -     283662 in   5.083350s

- Use `.each` instead of `for` for iterating over elements.
- Initialize `delim`, `result` vars with initial states instead of appending `state.object_nl` later.
- Use faster string interpolation where possible on `Hash#json_transform` instead of appending strings repeatedly.

Benchmark:

```ruby
    hash ={:a => {:a => {:a => {:a => {:a => 1}}}}}
    require 'benchmark/ips'
    Benchmark.ips do |x|
      x.report("to_json ") {  hash.to_json}
    end
```

Before:

Calculating -------------------------------------
            to_json       4990 i/100ms
-------------------------------------------------
            to_json     52970.8 (±5.9%) i/s -     264470 in   5.010558s

After:
Calculating -------------------------------------
            to_json       5253 i/100ms
-------------------------------------------------
            to_json     55998.6 (±5.9%) i/s -     283662 in   5.083350s
@marcandre
Copy link
Contributor

This PR has conflicts.
There are some gains here, but they are pretty modest and I think the actually best way to improve the performance would be to build and array and join it at the very end (although I have not tried nor benchmarked this).
I would close this PR.

@hsbt hsbt merged commit e397db4 into flori:master Dec 1, 2023
@hsbt
Copy link
Collaborator

hsbt commented Dec 1, 2023

@vipulnsward Sorry, too late response. I update with the current HEAD and merged!

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

3 participants