-
Notifications
You must be signed in to change notification settings - Fork 257
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: duplicate read proto #1741
fix: duplicate read proto #1741
Conversation
WalkthroughWalkthroughThe recent updates in the codebase introduce a caching mechanism for file reading, aimed at enhancing efficiency and reducing redundant operations. The changes involve integrating Changes
Assessment against linked issues
Recent Review DetailsConfiguration used: CodeRabbit UI Files selected for processing (1)
Additional Context UsedPath-based Instructions (1)
Learnings (1)
Additional comments not posted (1)
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (invoked as PR comments)
Additionally, you can add CodeRabbit Configration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 3
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #1741 +/- ##
==========================================
- Coverage 86.93% 86.89% -0.05%
==========================================
Files 155 155
Lines 15540 15574 +34
==========================================
+ Hits 13510 13533 +23
- Misses 2030 2041 +11 ☔ View full report in Codecov by Sentry. |
src/resource_reader.rs
Outdated
} | ||
|
||
impl ResourceReader { | ||
pub fn init(runtime: TargetRuntime) -> Self { | ||
Self { runtime } | ||
pub fn init(runtime: TargetRuntime, cache: Arc<Mutex<HashMap<String, String>>>) -> Self { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pub fn init(runtime: TargetRuntime, cache: Arc<Mutex<HashMap<String, String>>>) -> Self { | |
pub fn init(runtime: TargetRuntime, cache: bool) -> Self { |
If cache is true, we should initialize it inside of init
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And there is another issue, the first reading is called by ConfigReader::resource_reader, the second is called by ConfigReader::proto_reader, they are different object. So we need to get the HashMap from outside.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about using Option<HashMap>
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And there is another issue, the first reading is called by ConfigReader::resource_reader, the second is called by ConfigReader::proto_reader, they are different object. So we need to get the HashMap from outside.
The cache should not be shared. If the cache needs to be shared, we should do that by sharing the ResourceReader.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that's just an additional unwrapping that makes the code difficult to read. You can create two resource readers
struct ResourceReader<A>(A);
impl<A> ResourceReader<A> {
fn direct(runtime: TargetRuntime) -> ResourceReader<Direct>
fn cached(runtime: TargetRuntime) -> ResourceReader<Cache>
}
// Reads the files directly without cache
Direct {runtime: TargetRuntime }
// Reads it and maintains a cache
pub Cache {
reader: Direct,
cache: Arc<Mutex<HashMap<String, FileRead>>
}
Can we do something like above? We will know at compile time, if the reader is caching or not.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Static dispatch is contagious, so I use the option to control whether enable cache.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It makes the code a lot more modular, type-safe and easy to test. The current implementation has a lot of checks before reading, increasing the potential of bugs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried it, but got compile error about not found self.resource_reader.read_file method of ProtoReader
. I'm not familiar with this, maybe I need a trait bound to help compiler?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, I switch to static dispatch and add a reader trait.
Self { proto_reader: ProtoReader::init(runtime) } | ||
Self { | ||
proto_reader: ProtoReader::init(Arc::new(ResourceReader::init(runtime, false))), | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are we changing the API here? I don't think it's required.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ProtoReader need to share ResourceReader to avoid duplicate reading.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 5
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 2
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 2
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 3
@tusharmath Please take a look. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 2
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 3
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 4
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 3
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 3
|
||
impl Cached { | ||
pub fn init(runtime: TargetRuntime) -> Self { | ||
Self { direct: Direct::init(runtime), cache: Default::default() } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Initialize the cache explicitly to ensure it matches the expected type and is thread-safe.
- cache: Default::default(),
+ cache: Arc::new(Mutex::new(HashMap::new())),
Explicitly initializing the cache as Arc<Mutex<HashMap<String, String>>>
ensures that the type is correct and that thread safety is maintained.
Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.
Self { direct: Direct::init(runtime), cache: Default::default() } | |
Self { direct: Direct::init(runtime), cache: Arc::new(Mutex::new(HashMap::new())) } |
Co-authored-by: Tushar Mathur <[email protected]> Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Summary:
Briefly describe the changes made in this PR.
According to @tusharmath 's comments. use HashMap to avoid duplicate read.
Current output:
Issue Reference(s):
Fixes #... (Replace "..." with the issue number)
Close #1734
/claim #1734
Build & Testing:
cargo test
successfully../lint.sh --mode=fix
to fix all linting issues raised by./lint.sh --mode=check
.Checklist:
<type>(<optional scope>): <title>
Summary by CodeRabbit
New Features
Enhancements
Refactor