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

Use aset instead of aset-* for performance #286

Merged
merged 1 commit into from
Oct 19, 2023
Merged

Conversation

athos
Copy link
Member

@athos athos commented Oct 19, 2023

aset-* (aset-int, aset-long, etc.) is a set of reflective array operations that are tens of times slower than inline aset calls and can have a significant performance impact:

user=> (let [arr (int-array 1000)] (cr/quick-bench (dotimes [i 1000] (aset arr i i))))
Evaluation count : 587442 in 6 samples of 97907 calls.
             Execution time mean : 1.012114 µs
    Execution time std-deviation : 5.606968 ns
   Execution time lower quantile : 1.005990 µs ( 2.5%)
   Execution time upper quantile : 1.020545 µs (97.5%)
                   Overhead used : 6.755950 ns
nil
user=> (let [arr (int-array 1000)] (cr/quick-bench (dotimes [i 1000] (aset-int arr i i))))
Evaluation count : 8418 in 6 samples of 1403 calls.
             Execution time mean : 73.125862 µs
    Execution time std-deviation : 1.971431 µs
   Execution time lower quantile : 71.480682 µs ( 2.5%)
   Execution time upper quantile : 75.451133 µs (97.5%)
                   Overhead used : 6.755950 ns
nil
user=>

This PR replaces all the invocations to aset-* in the codebase with aset for performance improvement.

This change contributes specifically to the performance improvement of cljam.algo.depth, which is more than 1.5x faster after the change:

;; before change
user=> (time (dorun (with-open [r (sam/reader "sample.bam")] (depth/depth r {:chr "chr1"} {:unchecked? true}))))
"Elapsed time: 41775.877042 msecs"
nil
user=>

;; after change
user=> (time (dorun (with-open [r (sam/reader "sample.bam")] (depth/depth r {:chr "chr1"} {:unchecked? true}))))
"Elapsed time: 24674.470916 msecs"
nil
user=>
before change after change
flamegraph-before-change flamegraph-after-change

@athos athos self-assigned this Oct 19, 2023
@athos athos requested review from alumi and a team as code owners October 19, 2023 01:02
@athos athos requested review from niyarin and removed request for a team October 19, 2023 01:02
@codecov
Copy link

codecov bot commented Oct 19, 2023

Codecov Report

Merging #286 (3966774) into master (d3fb1f2) will not change coverage.
The diff coverage is 38.70%.

@@           Coverage Diff           @@
##           master     #286   +/-   ##
=======================================
  Coverage   88.86%   88.86%           
=======================================
  Files          79       79           
  Lines        6772     6772           
  Branches      475      475           
=======================================
  Hits         6018     6018           
  Misses        279      279           
  Partials      475      475           
Files Coverage Δ
src/cljam/io/twobit/writer.clj 81.10% <100.00%> (ø)
src/cljam/io/fasta/reader.clj 91.71% <0.00%> (ø)
src/cljam/util/sequence.clj 78.94% <33.33%> (ø)
src/cljam/io/sam/util/sequence.clj 92.00% <50.00%> (ø)
src/cljam/algo/depth.clj 75.28% <28.57%> (ø)
src/cljam/io/pileup.clj 86.36% <0.00%> (ø)

Copy link
Member

@alumi alumi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for sending the performance improvement PR!
I hadn't noticed at all that aset-* wasn't being inlined and was calling the reflection API.
I confirmed the speedup here as well using the cljam.algo.depth-bench code.
LGTM 👍

Copy link
Contributor

@niyarin niyarin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks!

@niyarin niyarin merged commit 9091809 into master Oct 19, 2023
18 checks passed
@niyarin niyarin deleted the fix/aset-primitives branch October 19, 2023 07:06
@NoahTheDuke
Copy link

This is being tracked in CLJ-2840, so hopefully such changes won't be necessary in the future.

@athos
Copy link
Member Author

athos commented May 21, 2024

@NoahTheDuke Thank you for letting us know! We are already watching the ticket and hope that fix will further improve Clojure.

However, for this library, similar fixes might still be necessary in the future since we need to support older versions of Clojure as well.

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.

4 participants