From bfb9974aa9806d69555fea63c9acc157b78499c2 Mon Sep 17 00:00:00 2001 From: Lassi Kortela Date: Wed, 1 Sep 2021 15:31:49 +0300 Subject: [PATCH] Use CJSON to read JSON Profiling shows that most time is spent in: - medea#read-json - chicken.port#with-input-from-string Use the cjson egg to read JSON to go a lot faster. Note that we still write JSON using medea; that's not slow. Thanks to wasamasa and sjamaan on #chicken for profiling tips. --- pandoc.egg | 2 +- pandoc.server.chicken.scm | 15 ++++++++++++--- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/pandoc.egg b/pandoc.egg index 1283752..d35fad4 100644 --- a/pandoc.egg +++ b/pandoc.egg @@ -8,7 +8,7 @@ (license "MIT") (author "Lassi Kortela") (maintainer "Lassi Kortela") - (dependencies http-client medea scsh-process) + (dependencies cjson http-client medea scsh-process) (test-dependencies) (distribution-files "pandoc.server.chicken.scm" diff --git a/pandoc.server.chicken.scm b/pandoc.server.chicken.scm index eee6db5..f15e9d7 100644 --- a/pandoc.server.chicken.scm +++ b/pandoc.server.chicken.scm @@ -8,6 +8,7 @@ (import (scheme) (chicken base) + (cjson) (only (chicken port) with-input-from-string) (only (chicken io) read-string) (only (http-client) with-input-from-request) @@ -37,9 +38,17 @@ (cons 'text input-string))) input-strings))))) (lambda () - (map (lambda (string) - (with-input-from-string string read-json)) - (vector->list (read-json)))))) + (let ((array (string->cjson (read-string)))) + (unless (eq? cjson/array (cjson-type array)) + (error "Got unexpected JSON from pandoc-server")) + (let loop ((i (- (cjson-array-size array) 1)) (results '())) + (if (< i 0) results + (loop (- i 1) + (cons (cjson-schemify + (string->cjson + (cjson-schemify + (cjson-array-ref array i)))) + results)))))))) (define (pandoc-server-files->json input-format input-filenames) (pandoc-server-strings->json