From c28a0f45dc285224c5b7a910c5c9df7b45b651f4 Mon Sep 17 00:00:00 2001 From: Matthew Flatt Date: Tue, 12 Feb 2019 09:50:02 -0700 Subject: [PATCH] io: (mostly) keep fast path when line counting is enabled --- racket/src/io/demo.rkt | 5 +++-- racket/src/io/port/bytes-port.rkt | 24 ++++++++--------------- racket/src/io/port/peek-via-read-port.rkt | 17 ++++++++-------- racket/src/io/port/pipe.rkt | 4 ++-- racket/src/io/port/port.rkt | 3 --- racket/src/io/port/read-and-peek.rkt | 4 ++++ 6 files changed, 25 insertions(+), 32 deletions(-) diff --git a/racket/src/io/demo.rkt b/racket/src/io/demo.rkt index 797ea3a02b..e0678b6e11 100644 --- a/racket/src/io/demo.rkt +++ b/racket/src/io/demo.rkt @@ -779,6 +779,7 @@ (loop (sub1 j)))))) (define read-byte-buffer-mode 'block) +(define count-lines? #t) 'read-byte/host (time @@ -787,7 +788,7 @@ (let () (define p (host:open-input-file "compiled/io.rktl")) (host:file-stream-buffer-mode p read-byte-buffer-mode) - (host:port-count-lines! p) + (when count-lines? (host:port-count-lines! p)) (let loop () (unless (eof-object? (host:read-byte p)) (loop))) @@ -801,7 +802,7 @@ (let () (define p (open-input-file "compiled/io.rktl")) (file-stream-buffer-mode p read-byte-buffer-mode) - (port-count-lines! p) + (when count-lines? (port-count-lines! p)) (let loop () (unless (eof-object? (read-byte p)) (loop))) diff --git a/racket/src/io/port/bytes-port.rkt b/racket/src/io/port/bytes-port.rkt index 4f4068d1b4..332c74e030 100644 --- a/racket/src/io/port/bytes-port.rkt +++ b/racket/src/io/port/bytes-port.rkt @@ -73,14 +73,10 @@ [(i . < . len) (define amt (min (- end start) (fx- len i))) (define new-pos (fx+ i amt)) - (cond - [(not count) - ;; Keep/resume fast mode - (set! buffer-pos new-pos) - (set! offset 0) - (set! buffer bstr)] - [else - (set! pos new-pos)]) + ;; Keep/resume fast mode + (set! buffer-pos new-pos) + (set! offset 0) + (set! buffer bstr) (bytes-copy! dest-bstr start bstr i new-pos) (progress!) amt] @@ -128,14 +124,10 @@ (let ([amt (min amt (- len i))]) (define dest-bstr (make-bytes amt)) (bytes-copy! dest-bstr 0 bstr i (+ i amt)) - (cond - [(not count) - ;; Keep/resume fast mode - (set! buffer-pos (fx+ i amt)) - (set! buffer bstr) - (set! offset 0)] - [else - (set! pos (fx+ i amt))]) + ;; Keep/resume fast mode + (set! buffer-pos (fx+ i amt)) + (set! buffer bstr) + (set! offset 0) (progress!) (finish dest-bstr)))))] diff --git a/racket/src/io/port/peek-via-read-port.rkt b/racket/src/io/port/peek-via-read-port.rkt index c7b9d73fd2..4ffa902513 100644 --- a/racket/src/io/port/peek-via-read-port.rkt +++ b/racket/src/io/port/peek-via-read-port.rkt @@ -87,15 +87,14 @@ ;; in atomic mode [fast-mode! - (lambda (amt) ; amt = not yet added to `count` - (unless count - (set! buffer bstr) - (define s pos) - (set! buffer-pos s) - (set! buffer-end end-pos) - (define o offset) - (when o - (set! offset (- (+ o amt) s)))))] + (lambda (amt) ; amt = not yet added to `offset` + (set! buffer bstr) + (define s pos) + (set! buffer-pos s) + (set! buffer-end end-pos) + (define o offset) + (when o + (set! offset (- (+ o amt) s))))] ;; in atomic mode [slow-mode! diff --git a/racket/src/io/port/pipe.rkt b/racket/src/io/port/pipe.rkt index 5e3ef973ee..6c2111b307 100644 --- a/racket/src/io/port/pipe.rkt +++ b/racket/src/io/port/pipe.rkt @@ -127,8 +127,8 @@ (private [fast-mode! - (lambda (amt) ; amt = not yet added to `count` - (unless (or count buffer) + (lambda (amt) ; amt = not yet added to `offset` + (unless buffer (with-object pipe-data d (define s start) (define e end) diff --git a/racket/src/io/port/port.rkt b/racket/src/io/port/port.rkt index 1a19f4e635..12161770a7 100644 --- a/racket/src/io/port/port.rkt +++ b/racket/src/io/port/port.rkt @@ -29,9 +29,6 @@ ;; ;; A non-#f `buffer` further implies that `buffer-pos` should be ;; added to `offset` to get the true offset. - ;; - ;; Shortcut mode must be disabled when line counting is enabled, - ;; since shortcut mode skips explicit count. [buffer #f] [buffer-pos 0] ; if < `buffer-end`, allows direct read/write on `buffer` [buffer-end 0] diff --git a/racket/src/io/port/read-and-peek.rkt b/racket/src/io/port/read-and-peek.rkt index aeccf20290..4da9075bb2 100644 --- a/racket/src/io/port/read-and-peek.rkt +++ b/racket/src/io/port/read-and-peek.rkt @@ -67,6 +67,8 @@ (define new-pos (fx+ buf-pos v)) (bytes-copy! bstr start (core-port-buffer in) buf-pos new-pos) (set-core-port-buffer-pos! in new-pos) + (when (core-port-count in) + (port-count! in v bstr start)) (end-atomic) v] [else @@ -208,6 +210,8 @@ [(pos . fx< . (core-port-buffer-end in)) (define b (bytes-ref (core-port-buffer in) pos)) (set-core-port-buffer-pos! in (fx+ pos 1)) + (when (core-port-count in) + (port-count-byte! in b)) (end-atomic) b] [else