Skip the fold allocation when the buffer has no doubled bar

The fold in `fold_dsml_bars` only rewrites the doubled-bar spelling of DSML tags; a buffer that contains a canonical (single-bar) `DSML` tag needs no work. Add a `DOUBLED_BAR` precheck after the `DSML` precheck so the common "DSML but well-formed" path returns without allocating a fresh `String`. Drive-by: - Rename `DSML_MARKER_NAME` to `DSML_TAG_NAME` (matches DeepSeek's "DSML tag" terminology) and lift `DOUBLED_BAR` to a const so the precheck doesn't allocate on every call. - Expand the `push` comment to explain that both single- and doubled-bar prefixes are partial against the single-bar `START_MARKERS`, so a doubled-bar prefix is flushed as text, the fold rewrites the buffer, and the next push re-runs `drain` against canonical markers. - Note on `bar_run_len` that the caller passes the slice anchored to `DSML`, so the counted run always sits at the boundary. Co-Authored-By: siGit Code v1.6.8-acp <noreply@sigit.si>

Seto Elkahfi committed Oct 12, 2026 at 23:12 UTC dc268501dab975509bd861e6e0919183fa46308e
1 file changed +20 -7
src/inline_tool_calls.rs
+20 -7
@@ -59,8 +59,10 @@ const K3_SEP: &str = "<|sep|>";
59
60 /// `|` (U+FF5C), the bar every DSML marker above is built from.
61 const FULLWIDTH_BAR: char = '|';
62 +/// Two of them — the spelling [`fold_dsml_bars`] rewrites into one.
63 +const DOUBLED_BAR: &str = "||";
64 /// The name those markers wrap, and the only place [`fold_dsml_bars`] folds.
63 -const DSML_MARKER_NAME: &str = "DSML";
65 +const DSML_TAG_NAME: &str = "DSML";
66
67 const DSML_TOOL_CALLS_OPEN: &str = "<|DSML|tool_calls>";
68 const DSML_TOOL_CALLS_CLOSE: &str = "<|DSML|/tool_calls>";
@@ -459,6 +461,10 @@ impl<'a> StreamScanner<'a> {
461 // The fold runs on the whole buffer, not the chunk: a doubled bar split
462 // across two deltas is only visible once they are joined, and a marker
463 // is never emitted before then because a partial prefix is held back.
464 + // Both the single- and doubled-bar spellings are partial prefixes
465 + // (START_MARKERS is single-bar), so a doubled-bar prefix is flushed as
466 + // text on this push; the fold then rewrites the buffer and the next
467 + // push re-runs `drain` against canonical markers.
468 if let Some(folded) = fold_dsml_bars(&self.pending) {
469 self.pending = folded;
470 }
@@ -679,15 +685,21 @@ fn looks_like_dsml_call(after_marker: &str) -> bool {
685 /// turns out not to be a call comes back with single bars — markup either way.
686 ///
687 /// Returns `None` when there is nothing to fold — in particular for any buffer
682 -/// without a `DSML` marker, which is the common case and costs no allocation.
688 +/// without a doubled bar, which is the common case and costs no allocation.
689 fn fold_dsml_bars(text: &str) -> Option<String> {
684 - if !text.contains(DSML_MARKER_NAME) {
690 + if !text.contains(DSML_TAG_NAME) {
691 + return None;
692 + }
693 + // A DSML tag with only single bars is canonical; only the doubled-bar
694 + // spelling needs rewriting, so the buffer-walking allocation can be
695 + // skipped when the only defect isn't present.
696 + if !text.contains(DOUBLED_BAR) {
697 return None;
698 }
699 let mut folded = String::with_capacity(text.len());
700 let mut changed = false;
701 let mut rest = text;
690 - while let Some(idx) = rest.find(DSML_MARKER_NAME) {
702 + while let Some(idx) = rest.find(DSML_TAG_NAME) {
703 let (before, from_name) = rest.split_at(idx);
704 // The run of bars closing the tag being opened, i.e. `<||` in
705 // `<||DSML||invoke`. Counted in bars, not bytes: a lone fullwidth bar
@@ -698,8 +710,8 @@ fn fold_dsml_bars(text: &str) -> Option<String> {
710 changed |= opening_run > 1;
711 folded.push(FULLWIDTH_BAR);
712 }
701 - folded.push_str(DSML_MARKER_NAME);
702 - let after = &from_name[DSML_MARKER_NAME.len()..];
713 + folded.push_str(DSML_TAG_NAME);
714 + let after = &from_name[DSML_TAG_NAME.len()..];
715 // The run opening the tag being closed, i.e. `||>` in
716 // `<|DSML||/invoke>`.
717 let closing_run = bar_run_len(after, true);
@@ -714,7 +726,8 @@ fn fold_dsml_bars(text: &str) -> Option<String> {
726 }
727
728 /// Number of fullwidth bars in the run at the end of `text`, or at its start
717 -/// when `leading` is set.
729 +/// when `leading` is set. The caller passes the slice anchored to `DSML`, so
730 +/// the run being counted always sits at the boundary, never in the middle.
731 fn bar_run_len(text: &str, leading: bool) -> usize {
732 let bytes = if leading {
733 text.len() - text.trim_start_matches(FULLWIDTH_BAR).len()