Skip to content

Commit b1fd7f1

Browse files
stepanchegfacebook-github-bot
authored andcommitted
Use stable thread_local! for STACK_DEPTH
Summary: `thread_local!` with `const` initializer and no `Drop` generates identical code as `#[thread_local]`. [Compiler explorer](https://fburl.com/py1lyzml). Reviewed By: ndmitchell Differential Revision: D41063003 fbshipit-source-id: 936c366402d771962f03c6546ae447770023bbfc
1 parent c0ee0da commit b1fd7f1

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

starlark/src/values/stack_guard.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,9 @@ const MAX_RECURSION: u32 = 3000;
4141
// starlark function which calls to_str. We could change all evaluation stack
4242
// signatures to accept some "context" parameters, but passing it as
4343
// thread-local is easier.
44-
#[thread_local]
45-
static STACK_DEPTH: Cell<u32> = Cell::new(0);
44+
thread_local! {
45+
static STACK_DEPTH: Cell<u32> = const { Cell::new(0) };
46+
}
4647

4748
/// Stored previous stack depth before calling `try_inc`.
4849
///
@@ -56,20 +57,24 @@ pub struct StackGuard {
5657

5758
impl Drop for StackGuard {
5859
fn drop(&mut self) {
59-
STACK_DEPTH.set(self.prev_depth);
60+
STACK_DEPTH.with(|stack_depth| {
61+
stack_depth.set(self.prev_depth);
62+
});
6063
}
6164
}
6265

6366
/// Increment stack depth.
6467
fn inc() -> StackGuard {
65-
let prev_depth = STACK_DEPTH.get();
66-
STACK_DEPTH.set(prev_depth + 1);
67-
StackGuard { prev_depth }
68+
STACK_DEPTH.with(|stack_depth| {
69+
let prev_depth = stack_depth.get();
70+
stack_depth.set(prev_depth + 1);
71+
StackGuard { prev_depth }
72+
})
6873
}
6974

7075
/// Check stack depth does not exceed configured max stack depth.
7176
fn check() -> anyhow::Result<()> {
72-
if unlikely(STACK_DEPTH.get() >= MAX_RECURSION) {
77+
if unlikely(STACK_DEPTH.with(|stack_depth| stack_depth.get()) >= MAX_RECURSION) {
7378
return Err(ControlError::TooManyRecursionLevel.into());
7479
}
7580
Ok(())

0 commit comments

Comments
 (0)