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

Fix cloning static query on every query call #76

Closed
alexpovel opened this issue Jul 20, 2024 · 1 comment · Fixed by #99 or #72 · May be fixed by #170
Closed

Fix cloning static query on every query call #76

alexpovel opened this issue Jul 20, 2024 · 1 comment · Fixed by #99 or #72 · May be fixed by #170

Comments

@alexpovel
Copy link
Owner

alexpovel commented Jul 20, 2024

I think this call would only need to happen a single time, yet it takes up 17% of time:

image

Source: flamegraph via sudo CARGO_PROFILE_RELEASE_DEBUG=true cargo flamegraph --open -F5000 -- --go comments '(\\d+)(\\d)' inside of benches/kubernetes.

Source of problem might be clone here:

self.query.clone().into()

There is no need to clone, as the query remains identical through the lifetime of the application. I was just lazy with respect to lifetimes.

alexpovel added a commit that referenced this issue Jul 28, 2024
A query is static across the entire lifetime of a
run. It does not change, but it was `clone()`d on
each call to `scope_via_query`, which is
expensive. This change constructs the *actually*
required `TSQuery` only once (alongside its
"negative" counterpart, which also allows later
parts to just take a `&TSQuery` instead of `&mut
TSQuery`).

Closes #76
@alexpovel alexpovel linked a pull request Jul 28, 2024 that will close this issue
alexpovel added a commit that referenced this issue Jul 28, 2024
A query is static across the entire lifetime of a
run. It does not change, but it was `clone()`d on
each call to `scope_via_query`, which is
expensive. This change constructs the *actually*
required `TSQuery` only once (alongside its
"negative" counterpart, which also allows later
parts to just take a `&TSQuery` instead of `&mut
TSQuery`).

Closes #76
@alexpovel
Copy link
Owner Author

Results look good now:

$ hyperfine --warmup 1 './srgn --gitignored --fail-empty-glob --go comments --files '\''kubernetes/**/*.go'\'' '\''[tT]he (\w+)'\''' './srgn-new --gitignored --fail-empty-glob --go comments --files '\''kubernetes/**/*.go'\'' '\''[tT]he (\w+)'\'''
Benchmark 1: ./srgn --gitignored --fail-empty-glob --go comments --files 'kubernetes/**/*.go' '[tT]he (\w+)'
  Time (mean ± σ):      2.307 s ±  0.078 s    [User: 23.973 s, System: 1.714 s]
  Range (min … max):    2.213 s …  2.439 s    10 runs

Benchmark 2: ./srgn-new --gitignored --fail-empty-glob --go comments --files 'kubernetes/**/*.go' '[tT]he (\w+)'
  Time (mean ± σ):      2.015 s ±  0.112 s    [User: 20.166 s, System: 1.577 s]
  Range (min … max):    1.889 s …  2.216 s    10 runs

Summary
  ./srgn-new --gitignored --fail-empty-glob --go comments --files 'kubernetes/**/*.go' '[tT]he (\w+)' ran
    1.14 ± 0.07 times faster than ./srgn --gitignored --fail-empty-glob --go comments --files 'kubernetes/**/*.go' '[tT]he (\w+)'

where srgn-new contains the changes from #99 . It's faster by the expected margin. See also

flamegraph-fast

(slow section is no longer even visible)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant