1
Fork 0

Auto merge of #101313 - SparrowLii:mk_attr_id, r=cjgillot

make `mk_attr_id` part of `ParseSess`

Updates #48685

The current `mk_attr_id` uses the `AtomicU32` type, which is not very efficient and adds a lot of lock contention in a parallel environment.

This PR refers to the task list in #48685, uses `mk_attr_id` as a method of the `AttrIdGenerator` struct, and adds a new field `attr_id_generator` to `ParseSess`.

`AttrIdGenerator` uses the `WorkerLocal`, which has two advantages: 1. `Cell` is more efficient than `AtomicU32`, and does not increase any lock contention. 2. We put the index of the work thread in the first few bits of the generated `AttrId`, so that the `AttrId` generated in different threads can be easily guaranteed to be unique.

cc `@cjgillot`
This commit is contained in:
bors 2022-09-14 20:52:18 +00:00
commit 750bd1a7ff
13 changed files with 111 additions and 27 deletions

View file

@ -87,6 +87,7 @@ impl<'a> Parser<'a> {
// Always make an outer attribute - this allows us to recover from a misplaced
// inner attribute.
Some(attr::mk_doc_comment(
&self.sess.attr_id_generator,
comment_kind,
ast::AttrStyle::Outer,
data,
@ -138,7 +139,13 @@ impl<'a> Parser<'a> {
this.error_on_forbidden_inner_attr(attr_sp, inner_parse_policy);
}
Ok(attr::mk_attr_from_item(item, None, style, attr_sp))
Ok(attr::mk_attr_from_item(
&self.sess.attr_id_generator,
item,
None,
style,
attr_sp,
))
} else {
let token_str = pprust::token_to_string(&this.token);
let msg = &format!("expected `#`, found `{token_str}`");
@ -291,7 +298,13 @@ impl<'a> Parser<'a> {
} else if let token::DocComment(comment_kind, attr_style, data) = self.token.kind {
if attr_style == ast::AttrStyle::Inner {
self.bump();
Some(attr::mk_doc_comment(comment_kind, attr_style, data, self.prev_token.span))
Some(attr::mk_doc_comment(
&self.sess.attr_id_generator,
comment_kind,
attr_style,
data,
self.prev_token.span,
))
} else {
None
}