mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-12 05:08:52 +00:00
internal: add API to check what are the max limits in practice
This commit is contained in:
parent
a423b307e6
commit
f952dc61d1
3 changed files with 35 additions and 4 deletions
|
@ -22,7 +22,9 @@ use crate::{
|
||||||
///
|
///
|
||||||
/// If an invocation produces more tokens than this limit, it will not be stored in the database and
|
/// If an invocation produces more tokens than this limit, it will not be stored in the database and
|
||||||
/// an error will be emitted.
|
/// an error will be emitted.
|
||||||
const TOKEN_LIMIT: Limit = Limit::new(524288);
|
///
|
||||||
|
/// Actual max for `analysis-stats .` at some point: 30672.
|
||||||
|
static TOKEN_LIMIT: Limit = Limit::new(524_288);
|
||||||
|
|
||||||
#[derive(Debug, Clone, Eq, PartialEq)]
|
#[derive(Debug, Clone, Eq, PartialEq)]
|
||||||
pub enum TokenExpander {
|
pub enum TokenExpander {
|
||||||
|
|
|
@ -6,4 +6,6 @@ license = "MIT OR Apache-2.0"
|
||||||
authors = ["rust-analyzer developers"]
|
authors = ["rust-analyzer developers"]
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
|
|
||||||
[dependencies]
|
[features]
|
||||||
|
tracking = []
|
||||||
|
default = ["tracking"]
|
||||||
|
|
|
@ -1,15 +1,26 @@
|
||||||
//! limit defines a struct to enforce limits.
|
//! limit defines a struct to enforce limits.
|
||||||
|
|
||||||
|
use std::sync::atomic::AtomicUsize;
|
||||||
|
|
||||||
/// Represents a struct used to enforce a numerical limit.
|
/// Represents a struct used to enforce a numerical limit.
|
||||||
pub struct Limit {
|
pub struct Limit {
|
||||||
upper_bound: usize,
|
upper_bound: usize,
|
||||||
|
#[allow(unused)]
|
||||||
|
max: AtomicUsize,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Limit {
|
impl Limit {
|
||||||
/// Creates a new limit.
|
/// Creates a new limit.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub const fn new(upper_bound: usize) -> Self {
|
pub const fn new(upper_bound: usize) -> Self {
|
||||||
Self { upper_bound }
|
Self { upper_bound, max: AtomicUsize::new(0) }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Creates a new limit.
|
||||||
|
#[inline]
|
||||||
|
#[cfg(feature = "tracking")]
|
||||||
|
pub const fn new_tracking(upper_bound: usize) -> Self {
|
||||||
|
Self { upper_bound, max: AtomicUsize::new(1) }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Gets the underlying numeric limit.
|
/// Gets the underlying numeric limit.
|
||||||
|
@ -21,10 +32,26 @@ impl Limit {
|
||||||
/// Checks whether the given value is below the limit.
|
/// Checks whether the given value is below the limit.
|
||||||
/// Returns `Ok` when `other` is below `self`, and `Err` otherwise.
|
/// Returns `Ok` when `other` is below `self`, and `Err` otherwise.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub const fn check(&self, other: usize) -> Result<(), ()> {
|
pub fn check(&self, other: usize) -> Result<(), ()> {
|
||||||
if other > self.upper_bound {
|
if other > self.upper_bound {
|
||||||
Err(())
|
Err(())
|
||||||
} else {
|
} else {
|
||||||
|
#[cfg(feature = "tracking")]
|
||||||
|
loop {
|
||||||
|
use std::sync::atomic::Ordering;
|
||||||
|
let old_max = self.max.load(Ordering::Relaxed);
|
||||||
|
if other <= old_max || old_max == 0 {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if self
|
||||||
|
.max
|
||||||
|
.compare_exchange(old_max, other, Ordering::Relaxed, Ordering::Relaxed)
|
||||||
|
.is_ok()
|
||||||
|
{
|
||||||
|
eprintln!("new max: {}", other);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue