Skip to content

[NFC] Replace uses of find(x) != end() idiom with contains(x) for StringRef and Set-like types #35229

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 2 commits into from
Feb 8, 2021

Conversation

mininny
Copy link
Contributor

@mininny mininny commented Dec 27, 2020

A little while back, LLVM Set-like data types got contains(t) methods which make code more readable. Many places in the compiler use the find(a) != .end() idiom to check for membership (and in some places find(a) == .end() to check if the item is not present).

It would be nice to update the code to use contains(a) to improve the readability.

This PR replaces usages of .find(Key) != .end() idiom with .contains(Key).

Resolves SR-13950.

@varungandhi-apple Could you please review this? :) 🙏

Comment on lines 859 to 863
} else {
if (trace)
printf("Worker[%d] HIT for key = %d, value = %d.\n", i, key,
value->second);
found = true; // cache hit, no need to grab write lock
}
if (trace)
printf("Worker[%d] HIT for key = %d, value = %d.\n", i, key,
value->second);
found = true; // cache hit, no need to grab write lock
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I also added the missing else in this code, but I'm not sure if it is okay to fix it in this PR as well?

Copy link
Contributor

Choose a reason for hiding this comment

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

I would put it in a different PR. Different high level thing.

Copy link
Contributor

@gottesmm gottesmm left a comment

Choose a reason for hiding this comment

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

A few thoughts:

  1. You are not only changing set like things here. In fact, it makes me worried in general about this patch. Instead you are changing some sort like things and in some cases StringRef. Please separate the StringRef changes into a separate commit.
  2. I found at least 1-2 cases where the patch is changing a single lookup into multiple lookups. Please remove that (I marked it).

Before this goes in I want to look at it again.

@mininny mininny force-pushed the switch-find-to-contains branch 3 times, most recently from 65e65fa to 0af5519 Compare December 28, 2020 15:31
@gottesmm gottesmm changed the title [NFC] Replace uses of find(x) != end() idiom with contains(x) for Set-like types [NFC] Replace uses of find(x) != end() idiom with contains(x) for StringRef and Set-like types Dec 29, 2020
@@ -233,8 +230,7 @@ static void printModule(SILModule *Mod, bool EmitVerboseSIL) {
std::find(SILPrintOnlyFun.begin(), SILPrintOnlyFun.end(), F.getName()))
F.dump(EmitVerboseSIL);

if (!SILPrintOnlyFuns.empty() &&
F.getName().find(SILPrintOnlyFuns, 0) != StringRef::npos)
Copy link
Contributor

@gottesmm gottesmm Dec 29, 2020

Choose a reason for hiding this comment

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

A note for myself. Whats with the 0? (I am doing a surface level review scan now).

Copy link
Contributor

Choose a reason for hiding this comment

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

@mininny I looked in StringRef.h. Looks like the 0 is a default argument to find that says start checking at the beginning. (Just answering my own question).

@gottesmm
Copy link
Contributor

Just please move that last commit to a different PR and I think this is ready to go in.

@gottesmm
Copy link
Contributor

@swift-ci test

@gottesmm
Copy link
Contributor

Doing a quick test to see if everything works.

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - 0af551930dafd9c1a36b9f2cd524eed860b37e55

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - 0af551930dafd9c1a36b9f2cd524eed860b37e55

@@ -142,8 +142,8 @@ static bool canonicalizeInputFunction(Function &F, ARCEntryPointBuilder &B,
// Have not encountered a strong retain/release. keep it in the
// unknown retain/release list for now. It might get replaced
// later.
if (NativeRefs.find(ArgVal) == NativeRefs.end()) {
UnknownObjectRetains[ArgVal].push_back(&CI);
if (!NativeRefs.contains(ArgVal)) {
Copy link
Contributor

@LucianoPAlmeida LucianoPAlmeida Dec 30, 2020

Choose a reason for hiding this comment

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

Seems the build is failing becausellvm::DenseSet doesn't have a contains method, @gottesmm we normally use count so perhaps we could use this here as well? :)

Copy link
Contributor Author

@mininny mininny Dec 30, 2020

Choose a reason for hiding this comment

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

It seems like the change to llvm::DenseSet hasn't landed on swift/main branch so it's causing this issue..? The contains method exists on llvm-project's apple/stable/20210107 branch; I'm not sure how the CI works but can this be fixed by moving the base branch? :)

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, seems like it hasn't ... I would say that use count instead for Set like would just be fine and already an improvement, but since the whole point of the SR is to use contains I'm not really sure about that, so cc @varungandhi-apple @gottesmm

Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm, I seem to have made a mistake here. I looked at the git log and I saw that contains was added for SmallPtrSet, so I figured methods for all the Set-like data types were available on swift/main, since they were merged in succession to LLVM's main branch. However, looks like it's only present for SmallPtrSet on swift/main. Sorry about that.

We can wait for a couple of weeks; I'll ping this PR when we have the rebranch so we can run tests and land the change. I don't think cherry-picking changes onto other branches is the right fix here, as that might lead to more merge conflicts.

@mininny mininny force-pushed the switch-find-to-contains branch from 0af5519 to 09faa98 Compare December 30, 2020 01:21
@CodaFi
Copy link
Contributor

CodaFi commented Jan 5, 2021

LGTM

@swift-ci test

@swift-ci
Copy link
Contributor

swift-ci commented Jan 5, 2021

Build failed
Swift Test Linux Platform
Git Sha - 09faa98bd81cd1338489071610262e5c158d3070

@swift-ci
Copy link
Contributor

swift-ci commented Jan 5, 2021

Build failed
Swift Test OS X Platform
Git Sha - 09faa98bd81cd1338489071610262e5c158d3070

@CodaFi
Copy link
Contributor

CodaFi commented Jan 5, 2021

Please revert the DenseSet refactoring

/home/buildnode/jenkins/workspace/swift-PR-Linux/branch-main/swift/lib/LLVMPasses/LLVMARCOpts.cpp:145:25: error: no member named 'contains' in 'llvm::DenseSet<llvm::Value *, llvm::DenseMapInfo<llvm::Value *> >'
14:36:13         if (!NativeRefs.contains(ArgVal)) {
14:36:13              ~~~~~~~~~~ ^
14:36:13 /home/buildnode/jenkins/workspace/swift-PR-Linux/branch-main/swift/lib/LLVMPasses/LLVMARCOpts.cpp:192:25: error: no member named 'contains' in 'llvm::DenseSet<llvm::Value *, llvm::DenseMapInfo<llvm::Value *> >'
14:36:13         if (!NativeRefs.contains(ArgVal)) {
14:36:13              ~~~~~~~~~~ ^

@varungandhi-apple
Copy link
Contributor

@CodaFi I posted this in a thread

We can wait for a couple of weeks; I'll ping this PR when we have the rebranch so we can run tests and land the change. I don't think cherry-picking changes onto other branches is the right fix here, as that might lead to more merge conflicts.

Reverting is going to be more work, instead we can wait it out for changes to land on the LLVM side.

@varungandhi-apple
Copy link
Contributor

Mishal just posted information on the rebranch in the forums: https://forums.swift.org/t/updating-llvm-project-swift-main-branch/44019. We can land this change once the rebranch is done.

@varungandhi-apple
Copy link
Contributor

@mininny can you resolve the merge conflicts? I can run the tests after that.

@varungandhi-apple
Copy link
Contributor

I think it's okay for now, but for the future, please prefer to rebase for resolving conflicts instead of merging, so that we have a cleaner history.

@swift-ci smoke test

@mininny
Copy link
Contributor Author

mininny commented Feb 3, 2021

I think it's okay for now, but for the future, please prefer to rebase for resolving conflicts instead of merging, so that we have a cleaner history.

@swift-ci smoke test

Oh didn't realize that! Okay, Thanks! :)

@varungandhi-apple
Copy link
Contributor

@swift-ci please smoke test

@mininny mininny force-pushed the switch-find-to-contains branch from c215f4d to 2d8df59 Compare February 6, 2021 09:22
@mininny mininny force-pushed the switch-find-to-contains branch from 2d8df59 to 53d1fc4 Compare February 6, 2021 09:24
@LucianoPAlmeida
Copy link
Contributor

@swift-ci smoke test

@varungandhi-apple
Copy link
Contributor

@swift-ci smoke test macOS

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.

6 participants