Skip to content

Preallocate DefIds for lang items and use lang items in lowering #60607

Closed

Description

Right now, our lowering code uses string-paths to call e.g. Iterator::next when lowering for loops:

let next_path = &["iter", "Iterator", "next"];
let next_path = P(self.expr_std_path(head_sp, next_path, None, ThinVec::new()));
let next_expr = P(self.expr_call(head_sp, next_path, hir_vec![ref_mut_iter]));

This triggers a name resolution call

let mut path = self.resolver
.resolve_str_path(span, self.crate_root, components, is_value);
path.segments.last_mut().unwrap().args = params;
for seg in path.segments.iter_mut() {
if seg.hir_id.is_some() {
seg.hir_id = Some(self.next_id());
}
}
path

which will then give us a resolved path, but that's

  1. potentially expensive, since it happens every time a specific path is generated (which happens a lot for ? and for expansions/lowerings)
  2. seems uncool since it's stringly typed

I think it would be neat if we could instead create the lang_items table early in the process and just grab the appropriate item out of that table.

cc @Centril @eddyb @petrochenkov

As a first experiment to check out the impact we could create a cache for path resolutions so we only run the path resolution code once and just overwrite all HirIds in the generated path with new HirIds whenever we fetch a cached resolution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

Labels

A-HIRArea: The high-level intermediate representation (HIR)A-lang-itemArea: Language itemsC-cleanupCategory: PRs that clean code up or issues documenting cleanup.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions