Skip to content

Swift: extract lazy declarations #12335

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 5 commits into from
Mar 3, 2023

Conversation

AlexDenisov
Copy link
Contributor

No description provided.

@github-actions github-actions bot added the Swift label Feb 28, 2023
@AlexDenisov AlexDenisov force-pushed the alexdenisov/extract-lazy-declarations branch 3 times, most recently from 0dde5ab to abeb31f Compare March 1, 2023 14:19
@AlexDenisov AlexDenisov force-pushed the alexdenisov/extract-lazy-declarations branch from abeb31f to ded0e57 Compare March 1, 2023 14:22
@AlexDenisov AlexDenisov marked this pull request as ready for review March 1, 2023 14:24
@AlexDenisov AlexDenisov requested a review from a team as a code owner March 1, 2023 14:24
@AlexDenisov AlexDenisov requested a review from redsun82 March 1, 2023 14:24
@AlexDenisov AlexDenisov changed the title Swift: extract lazy declarations (WIP) Swift: extract lazy declarations Mar 1, 2023
@AlexDenisov
Copy link
Contributor Author

I'll start DCA as soon as the PR is green

Copy link
Contributor

@redsun82 redsun82 left a comment

Choose a reason for hiding this comment

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

Mostly harmless comments on my side. Nice stuff!

if (primaryFile) {
return resolvePath(primaryFile->getFilename());
}
if (lazyDeclaration) {
static SwiftMangler mangler;
Copy link
Contributor

Choose a reason for hiding this comment

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

hmm, that's a singleton in disguise.

Not that I'm completely against using a singleton here, especially a (probably) stateless singleton like this, but then I would make it a more explicit singleton everywhere. That is, I'd make SwiftMangler's constructor private, give it a

static SwiftMangler& instance() {
   static SwiftMangler ret;
   return ret;
}

and use that everywhere including the Decl translator.

By the way, how much stateless is this mangler? I don't think the original swift mangler is truly stateless, but we might be calling only state-invariant methods on it. If that's the case, maybe I'd make the mangledName method const and mark the internal swift mangler as mutable.

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 ended up switching to a simple local variable instead. It doesn't have to be a singleton, and there is actually no good reason for it to be.

Copy link
Contributor

Choose a reason for hiding this comment

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

yeah, you are right. Maybe the only reason we could consider to turn it into a singleton, would be for mocking purposes in unit tests (that is, we could introduce a mangler singleton settable in tests to a mock). As we don't really do those, local variables and class members are good enough!

Comment on lines 217 to 219
std::vector<const swift::Decl*> worklist;
std::copy(std::begin(state.pendingDeclarations), std::end(state.pendingDeclarations),
std::back_inserter(worklist));
Copy link
Contributor

Choose a reason for hiding this comment

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

when you create a vector from scratch from another container, you can pass the iterators directly to the constructor

Suggested change
std::vector<const swift::Decl*> worklist;
std::copy(std::begin(state.pendingDeclarations), std::end(state.pendingDeclarations),
std::back_inserter(worklist));
std::vector<const swift::Decl*> worklist(std::begin(state.pendingDeclarations), std::end(state.pendingDeclarations));

although, see my other comment on this block

Comment on lines 230 to 232
std::vector<const swift::Decl*> worklist;
std::copy(std::begin(state.pendingDeclarations), std::end(state.pendingDeclarations),
std::back_inserter(worklist));
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
std::vector<const swift::Decl*> worklist;
std::copy(std::begin(state.pendingDeclarations), std::end(state.pendingDeclarations),
std::back_inserter(worklist));
std::vector<const swift::Decl*> worklist(std::begin(state.pendingDeclarations), std::end(state.pendingDeclarations));

Comment on lines 28 to 29
std::unordered_set<const swift::Decl*> emittedDeclarations;
std::unordered_set<const swift::Decl*> pendingDeclarations;
Copy link
Contributor

Choose a reason for hiding this comment

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

some short comments explaining these would be welcome I think

@@ -12,6 +12,7 @@ enum class TrapType {
module,
invocation,
linkage,
lazy_declarations,
Copy link
Contributor

Choose a reason for hiding this comment

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

cosmetic nit for consistency with other consts:

Suggested change
lazy_declarations,
lazy_declaration,

@AlexDenisov AlexDenisov marked this pull request as draft March 2, 2023 18:12
@AlexDenisov AlexDenisov marked this pull request as ready for review March 3, 2023 09:58
@AlexDenisov AlexDenisov requested a review from redsun82 March 3, 2023 09:58
Copy link
Contributor

@redsun82 redsun82 left a comment

Choose a reason for hiding this comment

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

Nice!

@AlexDenisov AlexDenisov merged commit 4aeff0f into main Mar 3, 2023
@AlexDenisov AlexDenisov deleted the alexdenisov/extract-lazy-declarations branch March 3, 2023 15:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants