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

Refactor scope registry #41825

Merged
merged 2 commits into from
Apr 8, 2021
Merged

Refactor scope registry #41825

merged 2 commits into from
Apr 8, 2021

Conversation

tenderlove
Copy link
Member

I'm trying to reduce the number of calls to Thread.current as it seems we call this a lot even for simple queries.

We're spending time validating symbol parameters of the ScopeRegistry.
It's an internal class, and we can stop validating symbols by converting
to methods (you'll automatically get an error if you try to call a
method that doesn't exist).  Second, since we only have 3 things to keep
track of, rather than keep those things in a hash, just break it out in
to 3 instance variables.  (This is absolutely not a memory bottleneck,
but technically this patch will save some memory as the 3 ivars will be
embedded in the object rather than require a full st_table for the
original wrapper hash)
I noticed in profiles of simple queries (like `Post.where(id: 1).first`)
we're spending lots of time looking up the current thread.  I couldn't
find any way to speed this up in Ruby, so I thought maybe we could call
`Thread.current` fewer times per query.

This patch should eliminate 4 calls to `Thread.current` per query.

For this benchmark:

```ruby
  StackProf.run(mode: :wall, out: 'out.dump') do
    8000.times { Post.where(id: id).first }
  end
```

`Thread.current` goes from 7% to 4.7% of time:

```
==================================
  Mode: wall(1000)
  Samples: 1633 (0.00% miss rate)
  GC: 51 (3.12%)
==================================
     TOTAL    (pct)     SAMPLES    (pct)     FRAME
       140   (8.6%)         140   (8.6%)     String#sub!
       114   (7.0%)         114   (7.0%)     Thread.current
```

```
==================================
  Mode: wall(1000)
  Samples: 1719 (0.00% miss rate)
  GC: 51 (2.97%)
==================================
     TOTAL    (pct)     SAMPLES    (pct)     FRAME
       134   (7.8%)         134   (7.8%)     String#sub!
        99   (5.8%)          99   (5.8%)     Module#===
        81   (4.7%)          81   (4.7%)     Thread.current
```

This isn't huge, but I think we need to find more sources of
Thread.current.  It's surprising to me that we spend so much time
looking up the current thread when doing a query that is so "easy"
@tenderlove tenderlove merged commit f95c0b7 into main Apr 8, 2021
@tenderlove tenderlove deleted the refactor-scope-registry branch April 8, 2021 15:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant