tools.cli/parse-opts
接受参数、选项规范和附加选项。它调用compile-option-specs
和required-arguments
来构建specs
和req
。然后它使用这些规范和要求对提供的args
(以及选项)进行验证。对于给定应用程序,前两步在整个调用过程中不会发生变化。
我建议一个名为make-parse-opts-fn
的新函数,该函数在前端执行compile-option-specs
和required-arguments
操作,并返回一个依赖于编译后的specs
和req
的函数。它可以这样使用:(def compiled-parser (make-parse-opts-fn cli-options))
。
使用criterium进行的微基准测试显示了超过两倍的速度提升(首先是tools.cli/parse-opts,然后是预编译解析器)
; user=> (def cli-options
[["-h" "--help" "This message"]
[nil "--extra" "Output in extra format"
:default false]
["-q" "--quiet" "Print no suggestions, only return exit code"
:default false]])
#'user/cli-options
; user=> (bench (cli/parse-opts ["--quiet" "src"] cli-options :in-order true))
Evaluation count : 10962 in 6 samples of 1827 calls.
Execution time mean : 65.021329 µs
Execution time std-deviation : 5.276033 µs
Execution time lower quantile : 58.658638 µs ( 2.5%)
Execution time upper quantile : 69.724228 µs (97.5%)
Overhead used : 9.607933 ns
nil
; user=> (bench (compiled-parser ["--quiet" "src"] :in-order true))
Evaluation count : 24660 in 6 samples of 4110 calls.
Execution time mean : 25.090846 µs
Execution time std-deviation : 253.361821 ns
Execution time lower quantile : 24.769079 µs ( 2.5%)
Execution time upper quantile : 25.413286 µs (97.5%)
Overhead used : 9.607933 ns
nil
如果将附加选项也提升到make-parse-opts-fn
中(以速度换取灵活性),差异会更加明显,提供比现有的tools.cli/parse-opts
函数大约10倍的速度提升
; user=> (bench (compiled-parser-2 ["--quiet" "src"]))
Evaluation count : 73116 in 6 samples of 12186 calls.
Execution time mean : 8.349923 µs
Execution time std-deviation : 51.419864 ns
Execution time lower quantile : 8.282266 µs ( 2.5%)
Execution time upper quantile : 8.407998 µs (97.5%)
Overhead used : 9.607933 ns
Found 2 outliers in 6 samples (33.3333 %)
low-severe 1 (16.6667 %)
low-mild 1 (16.6667 %)
Variance from outliers : 13.8889 % Variance is moderately inflated by outliers
nil
如果有兴趣,我可以提供一个补丁。