(zilch core magic): use bytevectors to calculate rewrites, use mapping

This massively improves performance. Turns out strings are slow!

Change-Id: I6a6a6964c48e10fe046298eaf29c4e86f7d30cf1
This commit is contained in:
puck 2025-06-18 17:07:16 +00:00
parent 9382082702
commit 9b61f4df0a

View file

@ -278,43 +278,26 @@
is-ca)
(define (rewrite-string str with-rewrites)
(define parts '())
(define (find-part-at i last-i)
(define next-slash (string-contains str "/" i))
(if (or (not next-slash) (>= next-slash (- (string-length str) 53)))
(if (= last-i 0)
(set! parts #f)
(set! parts (cons (string-copy str last-i) parts)))
(let* ((actual-string (string-copy str next-slash (+ next-slash 53)))
(mapping-pair (assoc actual-string with-rewrites string=?)))
; If we have a mapping for this string, replace it and continue.
(if mapping-pair
(begin
(set! parts (cons (cdr mapping-pair) (cons (string-copy str last-i next-slash) parts)))
(find-part-at (+ next-slash 53) (+ next-slash 53)))
(find-part-at (+ next-slash 1) last-i)))))
(find-part-at 0 0)
(if (pair? parts)
(string-concatenate-reverse parts)
str))
(define bv (string->utf8 str))
(define out (rewrite-bytevector bv with-rewrites))
(if (eq? bv out) str (utf8->string out)))
(define (rewrite-bytevector str with-rewrites)
(define parts '())
(define (find-part-at i last-i)
(define next-slash (bytestring-index str (lambda (c) (= c #x2F)) i))
(let find-part-at ((i 0) (last-i 0))
(let ((next-slash (bytestring-index str (lambda (c) (= c #x2F)) i)))
(if (or (not next-slash) (>= next-slash (- (bytevector-length str) 53)))
(if (= last-i 0)
(set! parts #f)
(set! parts (cons (bytevector-copy str last-i) parts)))
(let* ((actual-string (utf8->string (bytevector-copy str next-slash (+ next-slash 53))))
(mapping-pair (assoc actual-string with-rewrites string=?)))
(mapping-pair (mapping-ref/default with-rewrites actual-string #f)))
; If we have a mapping for this string, replace it and continue.
(if mapping-pair
(begin
(set! parts (cons (string->utf8 (cdr mapping-pair)) (cons (bytevector-copy str last-i next-slash) parts)))
(set! parts (cons (string->utf8 mapping-pair) (cons (bytevector-copy str last-i next-slash) parts)))
(find-part-at (+ next-slash 53) (+ next-slash 53)))
(find-part-at (+ next-slash 1) last-i)))))
(find-part-at 0 0)
(find-part-at (+ next-slash 1) last-i))))))
(if (pair? parts)
(apply bytevector-append (reverse parts))
str))
@ -416,7 +399,7 @@
; Rewrite CA drv to IA drv using the known inputs
(define new-drvs (list))
(define new-srcs (derivation-input-src (pending-item-ca-drv item)))
(define rewrites (list))
(define rewrites (mapping (make-default-comparator)))
(for-each
(lambda (drv-and-outputs)
(mutex-lock! pending-mutex)
@ -429,7 +412,7 @@
(define new-path (cdr (assoc output (pending-item-resolved-paths dep-pend))))
(set! new-srcs (cons new-path new-srcs))
(define old-output (cdr (assoc output (derivation-outputs (car drv-and-outputs)))))
(set! rewrites (cons (cons (derivation-output-path old-output) new-path) rewrites)))
(set! rewrites (mapping-set! rewrites (derivation-output-path old-output) new-path)))
(cdr drv-and-outputs))
; Not a CA drv, add it back to the drvs list