From 149f2cc389b54af621f6a40dcb206dbac6b6965a Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Wed, 6 May 2020 18:30:41 -0700 Subject: [PATCH] skip unnecessary steps for non-interactive sessions --- pollen/private/cache-utils.rkt | 68 ++++++++++++++++++---------------- pollen/private/command.rkt | 3 +- pollen/private/ts.rktd | 2 +- pollen/setup.rkt | 5 +++ pollen/test/test-rerequire.rkt | 3 +- 5 files changed, 47 insertions(+), 34 deletions(-) diff --git a/pollen/private/cache-utils.rkt b/pollen/private/cache-utils.rkt index 66b48df..c250419 100644 --- a/pollen/private/cache-utils.rkt +++ b/pollen/private/cache-utils.rkt @@ -61,38 +61,44 @@ (define-namespace-anchor cache-utils-module-ns) -(define my-caching-compile-proc (make-caching-managed-compile-zo)) -;; check 'gc not 'vm because 'gc is supported before 6.7 -(define running-on-cs? (eq? (system-type 'gc) 'cs)) +;; faster than the usual `managed-compile-zo` +(define caching-zo-compiler (make-caching-managed-compile-zo)) (define (path->hash path) - (for ([p (in-list (or (get-directory-require-files path) null))]) - (my-caching-compile-proc p)) - (when running-on-cs? - ;; this makes builds faster, - ;; but a bytecode-caching bug in Racket BC - ;; restricts it to CS for now - (my-caching-compile-proc path)) - (apply hasheq - (let ([doc-key (setup:main-export)] [meta-key (setup:meta-export)]) - (unless (and (symbol? doc-key) (symbol? meta-key)) - (raise-argument-error 'path->hash "symbols for doc and meta key" (cons doc-key meta-key))) - ;; I monkeyed around with using the metas submodule to pull out the metas (for speed) - ;; but in practice most files get their doc requested too. - ;; so it's just simpler to get both at once and be done with it. - ;; the savings of avoiding two cache fetches at the outset outweighs - ;; the benefit of not reloading doc when you just need metas. - ;; new namespace forces `dynamic-require` to re-instantiate `path` - ;; otherwise it gets cached in current namespace. - (parameterize ([current-namespace (make-base-namespace)] - [current-directory (dirname path)]) - ;; brings in currently instantiated params (unlike namespace-require) - (define outer-ns (namespace-anchor->namespace cache-utils-module-ns)) - (namespace-attach-module outer-ns 'pollen/setup) - (define doc-missing-thunk (λ () "")) - (define metas-missing-thunk (λ () (hasheq))) - (list doc-key (dynamic-require path doc-key doc-missing-thunk) - meta-key (dynamic-require path meta-key metas-missing-thunk)))))) + (define compilation-namespace + (cond + [(current-session-interactive?) + ;; in interactive mode, we need a fresh namespace every time + ;; and can't use bytecode, because it's possible that path + ;; or any dependency (say, "pollen.rkt") has changed + (define bns (make-base-namespace)) + (define outer-ns (namespace-anchor->namespace cache-utils-module-ns)) + ;; bring in currently instantiated params (unlike namespace-require) + (namespace-attach-module outer-ns 'pollen/setup bns) + bns] + [else + ;; make bytecode, because we know that in a non-interactive sesssion + ;; the sources won't change in the midst + (for-each caching-zo-compiler (cons path (or (get-directory-require-files path) null))) + ; recycle namespace + (current-namespace)])) + (define doc-key (setup:main-export)) + (define meta-key (setup:meta-export)) + (unless (and (symbol? doc-key) (symbol? meta-key)) + (raise-argument-error 'path->hash "symbols for doc and meta key" (cons doc-key meta-key))) + ;; I monkeyed around with using the metas submodule to pull out the metas (for speed) + ;; but in practice most files get their doc requested too. + ;; so it's just simpler to get both at once and be done with it. + ;; the savings of avoiding two cache fetches at the outset outweighs + ;; the benefit of not reloading doc when you just need metas. + ;; new namespace forces `dynamic-require` to re-instantiate `path` + ;; otherwise it gets cached in current namespace. + (define doc-missing-thunk (λ () "")) + (define metas-missing-thunk (λ () (hasheq))) + (parameterize ([current-namespace compilation-namespace] + [current-directory (dirname path)]) + (hasheq doc-key (dynamic-require path doc-key doc-missing-thunk) + meta-key (dynamic-require path meta-key metas-missing-thunk)))) (define (my-make-directory* dir) (define base (dirname dir)) @@ -151,5 +157,5 @@ "cache attempt failed: could not acquire shared lock") (void)] [_ (log-pollen-error str)]))) #;(with-input-from-file dest-file - (λ () (fasl->s-exp (port->bytes)))) + (λ () (fasl->s-exp (port->bytes)))) (deserialize (file->value dest-file))) diff --git a/pollen/private/command.rkt b/pollen/private/command.rkt index 1dd70e1..53182e6 100644 --- a/pollen/private/command.rkt +++ b/pollen/private/command.rkt @@ -192,7 +192,8 @@ version print the version" (current-server-port) (make-publish-di (error (format "~a is not a valid port number" http-port))) (parameterize ([current-project-root dir] [current-server-port (or http-port (setup:project-server-port))] - [current-server-listen-ip (and localhost-wanted "127.0.0.1")]) + [current-server-listen-ip (and localhost-wanted "127.0.0.1")] + [current-session-interactive? #true]) (message "starting project server ...") ((dynamic-require 'pollen/private/project-server 'start-server) (format "/~a" (setup:main-pagetree dir)) launch-wanted))) diff --git a/pollen/private/ts.rktd b/pollen/private/ts.rktd index d6bc4c6..0ff1e99 100644 --- a/pollen/private/ts.rktd +++ b/pollen/private/ts.rktd @@ -1 +1 @@ -1588786944 +1588815041 diff --git a/pollen/setup.rkt b/pollen/setup.rkt index a459a8e..fc76f78 100644 --- a/pollen/setup.rkt +++ b/pollen/setup.rkt @@ -106,6 +106,11 @@ (define+provide current-render-source (make-parameter #f)) +;; used to distinguish one-shot rendering (e.g., using `raco pollen setup` or `render` +;; from an interactive session with the project server (using `raco pollen start`) +;; in one-shot mode, certain features needed for dynamic recompilation are disabled for speed. +(define+provide current-session-interactive? (make-parameter #false)) + (define-settable dashboard-css "poldash.css") (define-runtime-path server-extras-dir "private/server-extras") diff --git a/pollen/test/test-rerequire.rkt b/pollen/test/test-rerequire.rkt index 7d91e08..2f84ed4 100644 --- a/pollen/test/test-rerequire.rkt +++ b/pollen/test/test-rerequire.rkt @@ -16,7 +16,8 @@ ;; test makes sure that file render changes after pollen.rkt changes (parameterize ([current-output-port (open-output-string)] [current-directory rerequire-dir] - [current-project-root rerequire-dir]) + [current-project-root rerequire-dir] + [current-session-interactive? #true]) (display-to-file @string-append{#lang racket/base (provide id)