Skip to content

alloc: less static mut + some cleanup #142520

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
Jun 18, 2025
Merged

Conversation

hkBst
Copy link
Member

@hkBst hkBst commented Jun 14, 2025

I'm looking into #125035 and would like some feedback on my approach.

@rustbot rustbot added the T-libs Relevant to the library team, which will review and decide on the PR/issue. label Jun 14, 2025
unsafe {
DROPS += 1;
}
DROPS.with(|drops| drops.update(|v| v + 1));
Copy link
Contributor

Choose a reason for hiding this comment

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

Could be written as DROPS.set(DROPS.get() + 1) thanks to the convenience methods in LocalKey<Cell<T>>. Debatable if that’s better in this case but definitely nicer for cases where you only read or only write.

Copy link
Member Author

Choose a reason for hiding this comment

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

I think it's already nicer even in this case, thanks!

And you think the thread-local is appropriate here?

Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry, I don't have enough context to say whether the overall approach is right, I just had a drive-by comment about the way it's implemented.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks, anyway!

Copy link
Contributor

Choose a reason for hiding this comment

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

thread_local should be fine here, or just an atomic

@hkBst hkBst changed the title alloc: less static mut alloc: less static mut + some cleanup Jun 16, 2025
@hkBst hkBst marked this pull request as ready for review June 16, 2025 09:46
@rustbot
Copy link
Collaborator

rustbot commented Jun 16, 2025

r? @thomcc

rustbot has assigned @thomcc.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jun 16, 2025
Copy link
Contributor

@tgross35 tgross35 left a comment

Choose a reason for hiding this comment

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

One q but r=me after that, thanks for the cleanup!

static mut DROPS: i32 = 0;
macro_rules! struct_with_counted_drop {
($struct_name:ident$(($elt_ty:ty))?, $drop_counter:ident $(=> $drop_stmt:expr)?) => {
thread_local! {static $drop_counter: Cell<i32> = Cell::new(0);}
Copy link
Contributor

Choose a reason for hiding this comment

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

Preexisting, but any reason this was i32 not u32?

Copy link
Member Author

Choose a reason for hiding this comment

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

I kept it on the thought that if things went really badly, this could perhaps become negative, but I suppose since it is only ever incremented, that was silly, so I'll change to u32.

@tgross35
Copy link
Contributor

Thank you!

@bors r+

@bors
Copy link
Collaborator

bors commented Jun 17, 2025

📌 Commit 2d3a37d has been approved by tgross35

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 17, 2025
@tgross35
Copy link
Contributor

@bors rollup

bors added a commit that referenced this pull request Jun 18, 2025
Rollup of 14 pull requests

Successful merges:

 - #141574 (impl `Default` for `array::IntoIter`)
 - #141608 (Add support for repetition to `proc_macro::quote`)
 - #142100 (rustdoc: make srcIndex no longer a global variable)
 - #142371 (avoid `&mut P<T>` in `visit_expr` etc methods)
 - #142517 (Windows: Use anonymous pipes in Command)
 - #142520 (alloc: less static mut + some cleanup)
 - #142588 (Generic ctx imprv)
 - #142605 (Don't unwrap in enzyme builds in case of missing llvm-config)
 - #142608 (Refresh module-level docs for `rustc_target::spec`)
 - #142618 (Lint about `console` calls in rustdoc JS)
 - #142620 (Remove a panicking branch in `BorrowedCursor::advance`)
 - #142631 (Dont suggest remove semi inside macro expansion for redundant semi lint)
 - #142632 (Update cargo)
 - #142635 (Temporarily add back -Zwasm-c-abi=spec)

r? `@ghost`
`@rustbot` modify labels: rollup
@tgross35 tgross35 assigned tgross35 and unassigned thomcc Jun 18, 2025
@bors bors merged commit eb7d246 into rust-lang:master Jun 18, 2025
10 checks passed
@rustbot rustbot added this to the 1.89.0 milestone Jun 18, 2025
rust-timer added a commit that referenced this pull request Jun 18, 2025
Rollup merge of #142520 - hkBst:less-static-mut, r=tgross35

alloc: less static mut + some cleanup

I'm looking into #125035 and would like some feedback on my approach.
@hkBst hkBst deleted the less-static-mut branch June 18, 2025 06:08
lnicola pushed a commit to lnicola/rust-analyzer that referenced this pull request Jun 18, 2025
Rollup of 14 pull requests

Successful merges:

 - rust-lang/rust#141574 (impl `Default` for `array::IntoIter`)
 - rust-lang/rust#141608 (Add support for repetition to `proc_macro::quote`)
 - rust-lang/rust#142100 (rustdoc: make srcIndex no longer a global variable)
 - rust-lang/rust#142371 (avoid `&mut P<T>` in `visit_expr` etc methods)
 - rust-lang/rust#142517 (Windows: Use anonymous pipes in Command)
 - rust-lang/rust#142520 (alloc: less static mut + some cleanup)
 - rust-lang/rust#142588 (Generic ctx imprv)
 - rust-lang/rust#142605 (Don't unwrap in enzyme builds in case of missing llvm-config)
 - rust-lang/rust#142608 (Refresh module-level docs for `rustc_target::spec`)
 - rust-lang/rust#142618 (Lint about `console` calls in rustdoc JS)
 - rust-lang/rust#142620 (Remove a panicking branch in `BorrowedCursor::advance`)
 - rust-lang/rust#142631 (Dont suggest remove semi inside macro expansion for redundant semi lint)
 - rust-lang/rust#142632 (Update cargo)
 - rust-lang/rust#142635 (Temporarily add back -Zwasm-c-abi=spec)

r? `@ghost`
`@rustbot` modify labels: rollup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants