Skip to content

Commit 07cb480

Browse files
committed
go back to the old implementation of matching backwards, but keep the cache
as this seems to account for the entire performance difference in modern Racket. That is, the old version, without the cache, when indenting drracket/private/unit.rkt (that's already indented) takes about 7% more time to complete than the newer version. Adding the cache into the older version (as in this commit) recovers that 7%. (And the older version is simpler.)
1 parent 1935dff commit 07cb480

File tree

1 file changed

+27
-23
lines changed

1 file changed

+27
-23
lines changed

syntax-color-lib/syntax-color/paren-tree.rkt

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@
178178
;; starting and stoping positions for error highlighting.
179179
;; If all three return #f, then there was no tree to search, or
180180
;; the position did not immediately follow a close.
181-
(define/public (match-backward pos)
181+
#;(define/public (match-backward pos)
182182
(define (not-found)
183183
(send tree search! pos)
184184
(values (- pos (paren-length (send tree get-root-data))) pos #t))
@@ -225,29 +225,33 @@
225225
[else
226226
(values #f #f #f)]))]))
227227

228-
#;(define/public (match-backward pos)
229-
(send tree search! (if (> pos 0) (sub1 pos) pos))
228+
(define/public (match-backward pos)
230229
(cond
231-
((and (not (send tree is-empty?))
232-
(is-close? (paren-type (send tree get-root-data)))
233-
(= (+ (paren-length (send tree get-root-data))
234-
(send tree get-root-start-position))
235-
pos))
236-
(let ((end
237-
(let/ec ret
238-
(do-match-backward (node-left (send tree get-root))
239-
0
240-
(list (paren-type (send tree get-root-data)))
241-
ret)
242-
#f)))
243-
(cond
244-
(end
245-
(values end pos #f))
246-
(else
247-
(send tree search! pos)
248-
(values (- pos (paren-length (send tree get-root-data))) pos #t)))))
249-
(else
250-
(values #f #f #f))))
230+
[(hash-ref back-cache pos #f) => (λ (res) (values res pos #f))]
231+
[else
232+
(send tree search! (if (> pos 0) (sub1 pos) pos))
233+
(cond
234+
((and (not (send tree is-empty?))
235+
(is-close? (paren-type (send tree get-root-data)))
236+
(= (+ (paren-length (send tree get-root-data))
237+
(send tree get-root-start-position))
238+
pos))
239+
(let ((end
240+
(let/ec ret
241+
(do-match-backward (node-left (send tree get-root))
242+
0
243+
(list (paren-type (send tree get-root-data)))
244+
ret)
245+
#f)))
246+
(cond
247+
(end
248+
(hash-set! back-cache pos end)
249+
(values end pos #f))
250+
(else
251+
(send tree search! pos)
252+
(values (- pos (paren-length (send tree get-root-data))) pos #t)))))
253+
(else
254+
(values #f #f #f)))]))
251255

252256
;; is-open-pos?: natural-number -> (union #f symbol)
253257
;; if the position starts an open, return the corresponding close,

0 commit comments

Comments
 (0)