@@ -17,8 +17,9 @@ interface Run {
1717type Line = ReadonlyArray < Run > ;
1818
1919interface TerminalScreen {
20- /** Scrolled-out lines, each ending in newline. */
21- readonly settled : string ;
20+ /** Scrolled-out lines, each ending in newline, as head + tail. Only the tail grows, so a cut
21+ * usually slices the head instead of copying all scrollback on every chunk. */
22+ readonly settled : readonly [ head : string , tail : string ] ;
2223 /** Never empty. */
2324 readonly lines : ReadonlyArray < Line > ;
2425 readonly row : number ;
@@ -31,7 +32,7 @@ export const EMPTY_TERMINAL_OUTPUT: TerminalOutputState = {
3132 text : "" ,
3233 truncated : false ,
3334 pending : "" ,
34- screen : { settled : "" , lines : [ [ ] ] , row : 0 , col : 0 , pen : "" , saved : null } ,
35+ screen : { settled : [ "" , "" ] , lines : [ [ ] ] , row : 0 , col : 0 , pen : "" , saved : null } ,
3536} ;
3637
3738/** Default command tail, in UTF-16 code units. */
@@ -289,7 +290,8 @@ function csiParams(params: string): Array<number> {
289290}
290291
291292class Terminal {
292- settled : string ;
293+ settledHead : string ;
294+ settledTail : string ;
293295 lines : Array < Line > ;
294296 row : number ;
295297 col : number ;
@@ -303,7 +305,7 @@ class Terminal {
303305
304306 constructor ( screen : TerminalScreen , maxChars : number ) {
305307 this . maxChars = maxChars ;
306- this . settled = screen . settled ;
308+ [ this . settledHead , this . settledTail ] = screen . settled ;
307309 this . lines = screen . lines . slice ( ) ;
308310 this . row = screen . row ;
309311 this . col = screen . col ;
@@ -374,9 +376,12 @@ class Terminal {
374376 }
375377
376378 settle ( line : Line ) : void {
377- this . settled += `${ serializeLine ( line ) } \n` ;
378- if ( this . settled . length > 2 * this . maxChars ) {
379- this . settled = terminalOutputTail ( this . settled , this . maxChars ) . text ;
379+ this . settledTail += `${ serializeLine ( line ) } \n` ;
380+ if ( this . settledHead . length + this . settledTail . length > 2 * this . maxChars ) {
381+ [ this . settledHead , this . settledTail = "" ] = terminalOutputTailParts (
382+ [ this . settledHead , this . settledTail ] ,
383+ this . maxChars ,
384+ ) . parts ;
380385 this . dropped = true ;
381386 }
382387 }
@@ -435,8 +440,9 @@ class Terminal {
435440 for ( let row = 0 ; row < this . row ; row += 1 ) this . lines [ row ] = [ ] ;
436441 this . lines [ this . row ] = eraseLine ( this . lines [ this . row ] ! , this . col , 1 ) ;
437442 } else if ( mode === 3 ) {
438- if ( this . settled !== "" ) this . dropped = true ;
439- this . settled = "" ;
443+ if ( this . settledHead !== "" || this . settledTail !== "" ) this . dropped = true ;
444+ this . settledHead = "" ;
445+ this . settledTail = "" ;
440446 } else {
441447 this . lines = [ [ ] ] ;
442448 this . row = 0 ;
@@ -541,6 +547,33 @@ export function terminalOutputTail(
541547 return { text : style . prefix + text . slice ( start ) , truncated : true } ;
542548}
543549
550+ /**
551+ * terminalOutputTail of the parts' concatenation, kept as parts. V8 copies a concatenated string
552+ * the first time it is searched or sliced, so a newline cut slices only the part it lands in.
553+ */
554+ function terminalOutputTailParts (
555+ parts : readonly [ string , ...Array < string > ] ,
556+ maxChars : number ,
557+ ) : { readonly parts : readonly [ string , ...Array < string > ] ; readonly truncated : boolean } {
558+ let total = 0 ;
559+ for ( const part of parts ) total += part . length ;
560+ if ( total <= maxChars ) return { parts, truncated : false } ;
561+ const start = total - maxChars ;
562+ let offset = 0 ;
563+ for ( let index = 0 ; index < parts . length ; index += 1 ) {
564+ const part = parts [ index ] ! ;
565+ if ( offset + part . length > start ) {
566+ const newline = part . indexOf ( "\n" , Math . max ( 0 , start - offset ) ) ;
567+ if ( newline !== - 1 ) {
568+ if ( offset + newline - start >= maxChars / 4 ) break ;
569+ return { parts : [ part . slice ( newline + 1 ) , ...parts . slice ( index + 1 ) ] , truncated : true } ;
570+ }
571+ }
572+ offset += part . length ;
573+ }
574+ return { parts : [ terminalOutputTail ( parts . join ( "" ) , maxChars ) . text ] , truncated : true } ;
575+ }
576+
544577export function appendTerminalOutput (
545578 state : TerminalOutputState ,
546579 chunk : string ,
@@ -575,15 +608,15 @@ export function appendTerminalOutput(
575608 }
576609 }
577610 if ( terminal . dropped ) truncated = true ;
578- const settled = terminalOutputTail ( terminal . settled , maxChars ) ;
611+ const settled = terminalOutputTailParts ( [ terminal . settledHead , terminal . settledTail ] , maxChars ) ;
579612 const window = terminal . lines . map ( serializeLine ) . join ( "\n" ) ;
580- const tail = terminalOutputTail ( settled . text + window , maxChars ) ;
613+ const tail = terminalOutputTailParts ( [ ... settled . parts , window ] , maxChars ) ;
581614 return {
582- text : tail . text ,
615+ text : tail . parts . reduce ( ( text , part ) => text + part , "" ) ,
583616 truncated : truncated || settled . truncated || tail . truncated ,
584617 pending,
585618 screen : {
586- settled : settled . text ,
619+ settled : [ settled . parts [ 0 ] , settled . parts [ 1 ] ?? "" ] ,
587620 lines : terminal . lines ,
588621 row : terminal . row ,
589622 col : terminal . col ,
0 commit comments