From 28bfd4698a6ee330df1819b89fd6b85cdae48491 Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Sun, 21 Feb 2021 00:18:50 +0100 Subject: [PATCH] Add default timeout for inline highlighting --- CHANGELOG.md | 1 + src/text/inline.rs | 7 +++++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 517920d..582c8ce 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ All notable changes to similar are documented here. * Small performance improvments by adding an early opt-out for and inline highlighting. * Added deadlines to all diffing algorithms to bail early. * Deprecated slice diffing methods in the individual algorithm modules. +- Use a default timeout for the inline highlighting feature. ## 1.2.2 diff --git a/src/text/inline.rs b/src/text/inline.rs index 7453990..ecf09eb 100644 --- a/src/text/inline.rs +++ b/src/text/inline.rs @@ -4,9 +4,10 @@ use std::fmt; use crate::text::{DiffableStr, TextDiff}; use crate::types::{Algorithm, Change, ChangeTag, DiffOp, DiffTag}; -use crate::{capture_diff, get_diff_ratio}; +use crate::{capture_diff_deadline, get_diff_ratio}; use std::ops::Index; +use std::time::{Duration, Instant}; use super::utils::upper_seq_ratio; @@ -187,6 +188,7 @@ impl<'s, T: DiffableStr + ?Sized> fmt::Display for InlineChange<'s, T> { } const MIN_RATIO: f32 = 0.5; +const TIMEOUT_MS: u64 = 500; pub(crate) fn iter_inline_changes<'x, 'diff, 'old, 'new, 'bufs, T>( diff: &'diff TextDiff<'old, 'new, 'bufs, T>, @@ -216,12 +218,13 @@ where let old_lookup = MultiLookup::new(old_slices); let new_lookup = MultiLookup::new(new_slices); - let ops = capture_diff( + let ops = capture_diff_deadline( Algorithm::Patience, &old_lookup, 0..old_lookup.len(), &new_lookup, 0..new_lookup.len(), + Some(Instant::now() + Duration::from_millis(TIMEOUT_MS)), ); if get_diff_ratio(&ops, old_lookup.len(), new_lookup.len()) < MIN_RATIO {