Skip to content
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

Limit chalk by time instead of iteration count #16981

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions crates/hir-ty/src/traits.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
//! Trait solving using Chalk.

use std::env::var;
use std::{
env::var,
time::{Duration, Instant},
};

use chalk_ir::{fold::TypeFoldable, DebruijnIndex, GoalData};
use chalk_recursive::Cache;
Expand All @@ -22,7 +25,7 @@ use crate::{
};

/// This controls how much 'time' we give the Chalk solver before giving up.
const CHALK_SOLVER_FUEL: i32 = 1000;
const CHALK_TIMEOUT: Duration = Duration::from_secs(1);

#[derive(Debug, Copy, Clone)]
pub(crate) struct ChalkContext<'a> {
Expand Down Expand Up @@ -144,16 +147,16 @@ fn solve(
tracing::debug!("solve goal: {:?}", goal);
let mut solver = create_chalk_solver();

let fuel = std::cell::Cell::new(CHALK_SOLVER_FUEL);

let start = Instant::now();
let should_continue = || {
db.unwind_if_cancelled();
let remaining = fuel.get();
fuel.set(remaining - 1);
if remaining == 0 {
tracing::debug!("fuel exhausted");

let elapsed = start.elapsed();
if elapsed > CHALK_TIMEOUT {
tracing::warn!("chalk solver timed out ({elapsed:.1?})");
return false;
}
remaining > 0
true
};

let mut solve = || {
Expand Down