Skip to content

Thread Safety Analysis: Use replaceLock instead of removeLock+addLock #141500

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

Merged
merged 1 commit into from
Jun 5, 2025

Conversation

melver
Copy link
Contributor

@melver melver commented May 26, 2025

In ScopedLockableFactEntry::unlock(), we can avoid a second search, pop_back(), and push_back() if we use the already obtained iterator into the FactSet to replace the old FactEntry and take its position in the vector.

In ScopedLockableFactEntry::unlock(), we can avoid a second search,
pop_back(), and push_back() if we use the already obtained iterator into
the FactSet to replace the old FactEntry and take its position in the
vector.
@melver melver requested a review from aaronpuchert May 26, 2025 15:09
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:analysis labels May 26, 2025
@llvmbot
Copy link
Member

llvmbot commented May 26, 2025

@llvm/pr-subscribers-clang

Author: Marco Elver (melver)

Changes

In ScopedLockableFactEntry::unlock(), we can avoid a second search, pop_back(), and push_back() if we use the already obtained iterator into the FactSet to replace the old FactEntry and take its position in the vector.


Full diff: https://github.com/llvm/llvm-project/pull/141500.diff

1 Files Affected:

  • (modified) clang/lib/Analysis/ThreadSafety.cpp (+3-3)
diff --git a/clang/lib/Analysis/ThreadSafety.cpp b/clang/lib/Analysis/ThreadSafety.cpp
index 198e324414be9..9dffb8fcd2204 100644
--- a/clang/lib/Analysis/ThreadSafety.cpp
+++ b/clang/lib/Analysis/ThreadSafety.cpp
@@ -1071,9 +1071,9 @@ class ScopedLockableFactEntry : public FactEntry {
         return;
       }
 
-      FSet.removeLock(FactMan, Cp);
-      FSet.addLock(FactMan,
-                   std::make_unique<LockableFactEntry>(!Cp, LK_Exclusive, loc));
+      FSet.replaceLock(
+          FactMan, It,
+          std::make_unique<LockableFactEntry>(!Cp, LK_Exclusive, loc));
     } else if (Handler) {
       SourceLocation PrevLoc;
       if (const FactEntry *Neg = FSet.findLock(FactMan, !Cp))

@llvmbot
Copy link
Member

llvmbot commented May 26, 2025

@llvm/pr-subscribers-clang-analysis

Author: Marco Elver (melver)

Changes

In ScopedLockableFactEntry::unlock(), we can avoid a second search, pop_back(), and push_back() if we use the already obtained iterator into the FactSet to replace the old FactEntry and take its position in the vector.


Full diff: https://github.com/llvm/llvm-project/pull/141500.diff

1 Files Affected:

  • (modified) clang/lib/Analysis/ThreadSafety.cpp (+3-3)
diff --git a/clang/lib/Analysis/ThreadSafety.cpp b/clang/lib/Analysis/ThreadSafety.cpp
index 198e324414be9..9dffb8fcd2204 100644
--- a/clang/lib/Analysis/ThreadSafety.cpp
+++ b/clang/lib/Analysis/ThreadSafety.cpp
@@ -1071,9 +1071,9 @@ class ScopedLockableFactEntry : public FactEntry {
         return;
       }
 
-      FSet.removeLock(FactMan, Cp);
-      FSet.addLock(FactMan,
-                   std::make_unique<LockableFactEntry>(!Cp, LK_Exclusive, loc));
+      FSet.replaceLock(
+          FactMan, It,
+          std::make_unique<LockableFactEntry>(!Cp, LK_Exclusive, loc));
     } else if (Handler) {
       SourceLocation PrevLoc;
       if (const FactEntry *Neg = FSet.findLock(FactMan, !Cp))

@melver melver requested a review from AaronBallman June 4, 2025 22:01
Copy link
Collaborator

@AaronBallman AaronBallman left a comment

Choose a reason for hiding this comment

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

LGTM, nice improvement!

@melver melver merged commit 2d7f53b into llvm:main Jun 5, 2025
14 checks passed
rorth pushed a commit to rorth/llvm-project that referenced this pull request Jun 11, 2025
…llvm#141500)

In ScopedLockableFactEntry::unlock(), we can avoid a second search,
pop_back(), and push_back() if we use the already obtained iterator into
the FactSet to replace the old FactEntry and take its position in the
vector.
DhruvSrivastavaX pushed a commit to DhruvSrivastavaX/lldb-for-aix that referenced this pull request Jun 12, 2025
…llvm#141500)

In ScopedLockableFactEntry::unlock(), we can avoid a second search,
pop_back(), and push_back() if we use the already obtained iterator into
the FactSet to replace the old FactEntry and take its position in the
vector.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:analysis clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants