speedup (line buffering)
authorClaus Gittinger <cg@exept.de>
Tue, 21 Jul 1998 19:33:37 +0200
changeset 997 c899373aac1d
parent 996 f3674765d8fb
child 998 4915be34a1d7
speedup (line buffering)
TerminalView.st
--- a/TerminalView.st	Tue Jul 21 18:51:34 1998 +0200
+++ b/TerminalView.st	Tue Jul 21 19:33:37 1998 +0200
@@ -2,7 +2,7 @@
 	instanceVariableNames:'inStream outStream readerProcess shellPid kbdSequences
 		escapeSequenceTree currentSequence currentTree kbdMap
 		escapeLeadingChars numberOfColumns numberOfLines
-		shellTerminateAction rangeStartLine rangeEndLine'
+		shellTerminateAction rangeStartLine rangeEndLine state'
 	classVariableNames:''
 	poolDictionaries:''
 	category:'Views-TerminalViews'
@@ -135,13 +135,13 @@
     "start a shell on a pseudo-TTY, open a terminalView on it
      (i.e. this is kind of an xterm)"
 
-    ^ self startShellIn:nil
+    ^ self openShellIn:nil
 
     "
      VT100TerminalView openShell
     "
 
-    "Modified: / 20.7.1998 / 18:28:26 / cg"
+    "Modified: / 21.7.1998 / 18:24:55 / cg"
 !
 
 openShellIn:aDirectory
@@ -549,6 +549,8 @@
 
     readerProcess notNil ifTrue:[
         readerProcess terminate.
+        "/ give it a chance to really terminate
+        Processor yield.
         readerProcess := nil
     ].
     inStream notNil ifTrue:[
@@ -560,7 +562,10 @@
         outStream := nil
     ].
 
-    "Modified: / 10.6.1998 / 17:53:49 / cg"
+    "/ flush any leftover input-processing events
+    self sensor flushEventsFor:self withType:#processInput:n:.
+
+    "Modified: / 21.7.1998 / 19:00:13 / cg"
 !
 
 destroy
@@ -694,13 +699,24 @@
 
                             outStream readWait.
                             (self sensor hasKeyPressEventFor:self) ifTrue:[
-                                Processor yield
+                                (self sensor eventCount > 10) ifTrue:[
+                                    "/ give terminalView a chance to
+                                    "/ send out the character.
+                                    Delay waitForSeconds:0.1.
+                                ]
                             ] ifFalse:[
                                 buffer := String new:1024.
                                 n := outStream nextAvailableBytes:1024 into:buffer startingAt:1.
 
                                 n > 0 ifTrue:[
                                     self pushEvent:#processInput:n: with:buffer with:n.
+                                    (self sensor eventCount > 50) ifTrue:[
+                                        [self sensor eventCount > 10] whileTrue:[
+                                            "/ give terminalView a chance to
+                                            "/ catch up.
+                                            Delay waitForSeconds:0.1.
+                                        ]
+                                    ].
                                 ] ifFalse:[
                                     n == 0 ifTrue:[
                                         outStream atEnd ifTrue:[
@@ -718,14 +734,15 @@
             ] valueOnUnwindDo:[
                 readerProcess := nil    
             ]
-        ] fork.
+        ] fork. "/ forkAt:9.
+        readerProcess name:'pty reader'.
     ]
 
     "
      VT100TerminalView openShell
     "
 
-    "Modified: / 12.6.1998 / 20:59:05 / cg"
+    "Modified: / 21.7.1998 / 19:32:57 / cg"
 ! !
 
 !TerminalView methodsFor:'initialization-shell'!
@@ -909,19 +926,104 @@
 !TerminalView methodsFor:'processing - input'!
 
 processInput:buffer n:count
+    |sensor|
+
     self hideCursor.
-    1 to:count do:[:i |
-        self nextPut:(buffer at:i).
+
+access critical:[
+    |i i2 s crnlFollows|
+
+    i := 1.
+    [i <= count] whileTrue:[
+        (state == 0) ifTrue:[
+            "/ in initial state.
+            "/ quick scan forward for next control character ...
+            i2 := buffer indexOfControlCharacterStartingAt:i.
+            i2 == 0 ifTrue:[
+                "/ no control characters - simply append all
+                "/ to the outstanding lines ...
+                s := buffer copyFrom:i to:count.
+                i := count + 1. "/ leave loop.
+                crnlFollows := false.
+            ] ifFalse:[
+                i2 > i ifTrue:[
+                    s := buffer copyFrom:i to:i2-1.
+                    i := i2. "/ proceed withcontrol character
+                    crnlFollows := false.
+                    i < (count - 1) ifTrue:[
+                        (buffer at:i) == Character return ifTrue:[
+                            (buffer at:i+1) == Character nl ifTrue:[
+                                crnlFollows := true.
+                                i := i + 2.
+                            ]
+                        ]
+                    ].
+                ]
+            ].
+        ].
+
+        s notNil ifTrue:[
+            currentEmphasis notNil ifTrue:[
+                s := s emphasizeAllWith:currentEmphasis
+            ].
+
+            outstandingLine size > 0 ifTrue:[
+                outstandingLine := outstandingLine , s.
+            ] ifFalse:[
+                outstandingLine := s.
+            ].
+            crnlFollows ifTrue:[
+                outstandingLines isNil ifTrue:[
+                    outstandingLines := OrderedCollection with:outstandingLine
+                ] ifFalse:[
+                    outstandingLines add:outstandingLine.
+                ].
+                outstandingLine := ''.
+            ].
+            s := nil.
+
+            collecting ifTrue:[
+                flushPending ifFalse:[
+                    self installDelayedUpdate
+                ] ifTrue:[
+"/                    outstandingLines size > collectSize ifTrue:[
+"/                        self endEntry
+"/                    ]
+                ]
+            ] ifFalse:[
+                self endEntry
+            ].
+        ] ifFalse:[
+            "/ no chunk to append (in an escape sequence)
+            "/ must handle individual characters
+            "/ to update the state machine.
+            self nextPut:(buffer at:i).
+            i := i + 1.
+        ]
     ].
 
-    (self sensor hasEvent:#processInput:n: for:self) ifFalse:[
-        self endEntry.
-        self showCursor.
-        "/ self makeCursorVisible.
+].
+
+    (sensor := self sensor) notNil ifTrue:[
+        (sensor hasEvent:#processInput:n: for:self) ifFalse:[
+            self endEntry.
+            self showCursor.
+            "/ self makeCursorVisible.
+        ] ifTrue:[
+            outstandingLines size > collectSize ifTrue:[ 
+                self endEntry.
+                self showCursor.
+                "/ self makeCursorVisible.
+                "/ redraw
+                windowGroup notNil ifTrue:[
+                    windowGroup processRealExposeEventsFor:self.
+                ]
+            ].
+        ]
     ].
 
     "Created: / 10.6.1998 / 17:26:09 / cg"
-    "Modified: / 20.6.1998 / 19:18:07 / cg"
+    "Modified: / 21.7.1998 / 19:30:18 / cg"
 ! !
 
 !TerminalView methodsFor:'queries'!
@@ -973,5 +1075,5 @@
 !TerminalView class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libwidg2/TerminalView.st,v 1.45 1998-07-20 16:30:39 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libwidg2/TerminalView.st,v 1.46 1998-07-21 17:33:37 cg Exp $'
 ! !