mirror of
https://github.com/storytold/filmcraft.git
synced 2026-12-02 20:19:02 -05:00
tts: look for a pause marker's closing bracket only a marker's length ahead (#4189)
* tts: look for a pause marker's closing bracket only a marker's length ahead script::parse searched the whole remaining text for a closing bracket after every '[' and lowercased the candidate, so text with many unclosed or distant brackets parsed in quadratic time (the speech panel re-parses on every redraw). A pause marker is short; the search now looks at most 64 characters ahead. * address review: drop the wall-clock assertion from the linear parse test
This commit is contained in:
1 parent
5d4e4aa692
commit
9731ad3fcc
1 file changed
+14
-1
@@ -18,6 +18,9 @@ pub enum Segment {
|
||||
/// The marker *Add pause* inserts.
|
||||
pub const PAUSE_MARKER: &str = "[pause 1s]";
|
||||
|
||||
/// Longest `[pause …]` marker recognised, brackets included (`[pause 123456789012ms]` is 22).
|
||||
const MAX_MARKER_CHARS: usize = 64;
|
||||
|
||||
/// Split `text` into speech and pauses, in order. Empty speech pieces are dropped.
|
||||
pub fn parse(text: &str) -> Vec<Segment> {
|
||||
let mut out = Vec::new();
|
||||
@@ -26,7 +29,9 @@ pub fn parse(text: &str) -> Vec<Segment> {
|
||||
while let Some(open) = rest.find('[') {
|
||||
let (before, from) = rest.split_at(open);
|
||||
speech.push_str(before);
|
||||
let close = from.find(']');
|
||||
// A marker is short; looking only that far keeps each `[` from rescanning (and
|
||||
// lowercasing) the rest of a long script.
|
||||
let close = from.char_indices().take(MAX_MARKER_CHARS).find(|&(_, c)| c == ']').map(|(i, _)| i);
|
||||
match (close, close.and_then(|c| from.get(..=c)).and_then(pause_millis)) {
|
||||
(Some(close), Some(millis)) => {
|
||||
flush(&mut out, &mut speech);
|
||||
@@ -102,4 +107,12 @@ mod tests {
|
||||
}
|
||||
assert_eq!(parse(" "), vec![]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn many_open_brackets_parse_in_linear_time() {
|
||||
// every `[` used to search (and copy) the whole remainder for its `]`
|
||||
let text = format!("{}]", "[".repeat(400_000));
|
||||
// a quadratic parse would not finish this input (a hang is the failure signal, not a wall-clock threshold)
|
||||
assert_eq!(parse(&text), vec![sp(&text)]);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user