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

Remove needless traits along with async-trait usage #445

Merged
merged 1 commit into from
Aug 25, 2024

Conversation

Swatinem
Copy link
Contributor

I originally just found a redundant Arc::clone within GetOrInsert::insert.
But then I discovered that GetOrInsert only has a single implementor and is thus unnecessary indirection.

The similar thing was true for GetOrRemoveEntry and InnerSync, so I removed those as well. One reason they might have existed was using a trait for code encapsulation, as removing them resulted in a bit more pub(crate) visibility, and a bunch more trait bounds all over the place.

However, in particular #[async_trait] usage was allocating Box<dyn Future> and using dynamic dispatch at runtime. Now that overhead is avoided and the compiler should be able to produce better code.

I originally just found a redundant `Arc::clone` within `GetOrInsert::insert`.

But then I discovered that `GetOrInsert` only has a single implementor and is thus unnecessary indirection.

The similar thing was true for `GetOrRemoveEntry` and `InnerSync`, so I removed those as well.
One reason they might have existed was using a trait for code encapsulation,
as removing them resulted in a bit more `pub(crate)` visibility, and a bunch more trait bounds all over the place.

However, in particular `#[async_trait]` usage was allocating `Box<dyn Future>` and using dynamic dispatch at runtime.
Now that overhead is avoided and the compiler should be able to produce better code.
Copy link

codecov bot commented Jul 21, 2024

Codecov Report

Attention: Patch coverage is 93.44262% with 8 lines in your changes missing coverage. Please review.

Project coverage is 94.88%. Comparing base (19abacf) to head (103ef62).
Report is 6 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #445      +/-   ##
==========================================
- Coverage   95.04%   94.88%   -0.17%     
==========================================
  Files          44       44              
  Lines       20979    20938      -41     
==========================================
- Hits        19939    19866      -73     
- Misses       1040     1072      +32     

@tatsuya6502 tatsuya6502 self-requested a review July 22, 2024 01:06
Copy link
Member

@tatsuya6502 tatsuya6502 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 very much for working on this change! It looks good to me.

For just in case you are still curious why those traits existed, here is a bit of the history:

GetOrRemoveEntry and InnerSync traits were needed by moka prior to v0.12 to meet the trait and lifetime bounds that ScheduledThreadPool (from scheduled-thead-pool crate) required. I do not remember the very details, but it was hard to give a closure having an Arc<base_cache::Inner> to ScheduledThreadPool as Inner had many internal data structures including ones who are !Send. So I created these traits to hide (encapsulate) the internal data structures.

When I worked for moka v0.12 and removed ScheduledThreadPool, I tried to minimize the changes, so I did not touch the traits and their implementations.

I added GetOrInsert trait when I worked on moka v0.12. Some of the ValueInitializer methods used to take closures to get or insert an cache entry. When I had to create an async version of ValueInitializer for v0.12, I simply translated the closures to trait methods as Rust did not support async closures. (Later, I added the remove method to the trait when I added entry().and_compute() method.)

But, GetOrInsert trait was not absolutely necessary and now I think I should have tried what you did; to pass a reference to base_cache::Inner.

@tatsuya6502 tatsuya6502 merged commit e854989 into moka-rs:main Aug 25, 2024
23 checks passed
@tatsuya6502 tatsuya6502 added the enhancement New feature or request label Aug 25, 2024
@tatsuya6502 tatsuya6502 added this to the v0.12.9 milestone Aug 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants