Skip to content

Commit eb513c2

Browse files
bors[bot]matklad
andauthored
Merge #9746
9746: internal: add API to check what are the max limits in practice r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
2 parents 93873b0 + f952dc6 commit eb513c2

File tree

3 files changed

+35
-4
lines changed

3 files changed

+35
-4
lines changed

crates/hir_expand/src/db.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ use crate::{
2222
///
2323
/// If an invocation produces more tokens than this limit, it will not be stored in the database and
2424
/// an error will be emitted.
25-
const TOKEN_LIMIT: Limit = Limit::new(524288);
25+
///
26+
/// Actual max for `analysis-stats .` at some point: 30672.
27+
static TOKEN_LIMIT: Limit = Limit::new(524_288);
2628

2729
#[derive(Debug, Clone, Eq, PartialEq)]
2830
pub enum TokenExpander {

crates/limit/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,6 @@ license = "MIT OR Apache-2.0"
66
authors = ["rust-analyzer developers"]
77
edition = "2018"
88

9-
[dependencies]
9+
[features]
10+
tracking = []
11+
default = ["tracking"]

crates/limit/src/lib.rs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,26 @@
11
//! limit defines a struct to enforce limits.
22
3+
use std::sync::atomic::AtomicUsize;
4+
35
/// Represents a struct used to enforce a numerical limit.
46
pub struct Limit {
57
upper_bound: usize,
8+
#[allow(unused)]
9+
max: AtomicUsize,
610
}
711

812
impl Limit {
913
/// Creates a new limit.
1014
#[inline]
1115
pub const fn new(upper_bound: usize) -> Self {
12-
Self { upper_bound }
16+
Self { upper_bound, max: AtomicUsize::new(0) }
17+
}
18+
19+
/// Creates a new limit.
20+
#[inline]
21+
#[cfg(feature = "tracking")]
22+
pub const fn new_tracking(upper_bound: usize) -> Self {
23+
Self { upper_bound, max: AtomicUsize::new(1) }
1324
}
1425

1526
/// Gets the underlying numeric limit.
@@ -21,10 +32,26 @@ impl Limit {
2132
/// Checks whether the given value is below the limit.
2233
/// Returns `Ok` when `other` is below `self`, and `Err` otherwise.
2334
#[inline]
24-
pub const fn check(&self, other: usize) -> Result<(), ()> {
35+
pub fn check(&self, other: usize) -> Result<(), ()> {
2536
if other > self.upper_bound {
2637
Err(())
2738
} else {
39+
#[cfg(feature = "tracking")]
40+
loop {
41+
use std::sync::atomic::Ordering;
42+
let old_max = self.max.load(Ordering::Relaxed);
43+
if other <= old_max || old_max == 0 {
44+
break;
45+
}
46+
if self
47+
.max
48+
.compare_exchange(old_max, other, Ordering::Relaxed, Ordering::Relaxed)
49+
.is_ok()
50+
{
51+
eprintln!("new max: {}", other);
52+
}
53+
}
54+
2855
Ok(())
2956
}
3057
}

0 commit comments

Comments
 (0)