-
-
Notifications
You must be signed in to change notification settings - Fork 3
Fix viewer lookup, cache notice parts. #120
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
base: master
Are you sure you want to change the base?
Conversation
Summary of ChangesHello @vLuckyyy, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request primarily focuses on performance optimizations and code refinement within the notice broadcasting system. Key changes include introducing caching for notice parts and translated formatters to reduce redundant computations, streamlining viewer collection logic, and improving exception handling practices. These updates aim to enhance the efficiency and robustness of how notices are processed and delivered. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
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.
Code Review
This pull request introduces caching for notice parts and translated formatters to improve performance, and refactors some viewer lookup logic. The caching for notice parts has a minor thread-safety issue. The caching for translated formatters has more significant issues: it uses a non-thread-safe HashMap in a concurrent context, and the cache invalidation logic (isDirty) is flawed, which could lead to stale data being used. I've provided detailed comments and suggestions to address these points.
| }); | ||
| } | ||
|
|
||
| private final Map<Locale, TranslatedFormatter> formatterCache = new 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.
The formatterCache is initialized as a HashMap, which is not thread-safe. Since send() can be executed asynchronously via sendAsync(), the prepareFormatterForLanguage method can be called by multiple threads concurrently. This can lead to race conditions when accessing formatterCache, potentially causing data corruption.
To ensure thread safety, you should use a thread-safe map implementation like java.util.concurrent.ConcurrentHashMap.
| private final Map<Locale, TranslatedFormatter> formatterCache = new HashMap<>(); | |
| private final Map<Locale, TranslatedFormatter> formatterCache = new java.util.concurrent.ConcurrentHashMap<>(); |
| protected boolean isDirty() { | ||
| return NoticeBroadcastImpl.this.formatters.size() != expectedFormatterCount | ||
| || NoticeBroadcastImpl.this.placeholders.size() != expectedPlaceholderCount; | ||
| } |
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.
The isDirty check is flawed because it only checks the size of the formatters and placeholders collections. This logic fails to detect modifications that don't change the collection size, such as replacing a placeholder with a new value for an existing key (e.g., calling placeholder("{key}", "new_value") after placeholder("{key}", "old_value") was already called). This can lead to stale formatters being served from the cache, resulting in incorrect messages.
A more robust approach would be to use a modification counter (e.g., an int or AtomicInteger field in NoticeBroadcastImpl) that is incremented every time formatters or placeholders are modified. The TranslatedFormatter would then store the modCount at creation time and compare it with the current modCount in isDirty().
| if (cachedParts == null) { | ||
| cachedParts = List.copyOf(this.parts.values()); | ||
| } |
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.
The lazy initialization of cachedParts is not thread-safe. If multiple threads call parts() concurrently when cachedParts is null, they might all enter this if block, leading to redundant computation. While this is a benign race condition in this specific case, it's better to use a standard thread-safe lazy initialization pattern.
One option is to use double-checked locking, which would require making cachedParts volatile.
private volatile List<NoticePart<?>> cachedParts;
//...
public List<NoticePart<?>> parts() {
if (cachedParts == null) {
synchronized (this) {
if (cachedParts == null) {
cachedParts = List.copyOf(this.parts.values());
}
}
}
return cachedParts;
}Alternatively, a simpler and safer approach would be to initialize cachedParts in the constructor and make it final, since the parts map is effectively immutable after construction.
No description provided.