streamline module caching for eval

pull/9/head
Matthew Butterick 11 years ago
parent 9d5a231f5e
commit a68459c37d

@ -209,68 +209,57 @@
(> (len (get-output-string port-for-catching-file-info)) 0)) (> (len (get-output-string port-for-catching-file-info)) 0))
;; cache some modules to speed up eval. ;; set up namespace for module caching
;; Do it in separate module so as not to pollute this one. (module caching-module racket/base
;; todo: macrofy these lists of modules (define-namespace-anchor caching-module-nsa)
;; Don't, however, put the project-requires here, (provide caching-module-nsa))
;; because they will no longer be subject to rerequire. (require 'caching-module)
(module my-module-cache racket/base
(require web-server/templates ;; (car (current-eval-namespace-cache)) = namespace containing cached modules
xml ;; (cdr (current-eval-namespace-cache)) = list of cached modules
racket/port (define current-eval-namespace-cache (make-parameter (cons (namespace-anchor->namespace caching-module-nsa) '())))
racket/file
racket/rerequire (define/contract+provide (add-module-to-current-eval-cache module-name)
racket/contract (symbol? . -> . void?)
racket/list (define cache-ns (car (current-eval-namespace-cache)))
racket/match (define cached-modules (cdr (current-eval-namespace-cache)))
racket/syntax (when (not (member module-name cached-modules))
pollen/cache (eval `(require ,module-name) cache-ns)
pollen/debug (current-eval-namespace-cache (cons cache-ns (cons module-name cached-modules)))))
pollen/decode
pollen/file (define initial-modules-to-cache '(web-server/templates
pollen/main xml
pollen/reader-base racket/port
pollen/pagetree racket/file
pollen/tag racket/rerequire
pollen/template racket/contract
pollen/world racket/list
pollen/project racket/match
sugar racket/syntax
txexpr) pollen/cache
(define-namespace-anchor my-module-cache-ns-anchor) pollen/debug
(provide my-module-cache-ns-anchor)) pollen/decode
pollen/file
pollen/main
(require 'my-module-cache) pollen/reader-base
(define cache-ns (namespace-anchor->namespace my-module-cache-ns-anchor)) pollen/pagetree
pollen/tag
pollen/template
pollen/world
pollen/project
sugar
txexpr))
(for-each add-module-to-current-eval-cache initial-modules-to-cache)
(define/contract (render-through-eval expr-to-eval) (define/contract (render-through-eval expr-to-eval)
(list? . -> . bytes?) (list? . -> . bytes?)
(define cache-ns (car (current-eval-namespace-cache)))
(define cached-modules (cdr (current-eval-namespace-cache)))
(parameterize ([current-namespace (make-base-namespace)] (parameterize ([current-namespace (make-base-namespace)]
[current-output-port (current-error-port)] [current-output-port (current-error-port)]
[current-pagetree (make-project-pagetree (world:current-project-root))]) [current-pagetree (make-project-pagetree (world:current-project-root))])
(for-each (λ(mod-name) (namespace-attach-module cache-ns mod-name)) (for-each (λ(mod-name) (namespace-attach-module cache-ns mod-name)) cached-modules)
`(web-server/templates
xml
racket/port
racket/file
racket/rerequire
racket/contract
racket/list
racket/match
racket/syntax
pollen/debug
pollen/cache
pollen/decode
pollen/file
pollen/main
pollen/reader-base
pollen/pagetree
pollen/project
pollen/tag
pollen/template
pollen/world
sugar
txexpr))
(string->bytes/utf-8 (eval expr-to-eval (current-namespace))))) (string->bytes/utf-8 (eval expr-to-eval (current-namespace)))))
Loading…
Cancel
Save