Skip to content

Commit

Permalink
Implement safety check
Browse files Browse the repository at this point in the history
lein test sci.performance
Testing reusable function result.
Evaluation count : 19170 in 6 samples of 3195 calls.
             Execution time mean : 32.441596 µs
    Execution time std-deviation : 636.477386 ns
   Execution time lower quantile : 31.584483 µs ( 2.5%)
   Execution time upper quantile : 33.182918 µs (97.5%)
                   Overhead used : 1.858977 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
  • Loading branch information
jeroenvandijk committed Jun 11, 2020
1 parent 8312d90 commit 3c01531
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
29 changes: 25 additions & 4 deletions src/sci/impl/fns.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,31 @@
fn-name macro? with-meta?]
(let [min-var-args-arity (when var-arg-name fixed-arity)

interpret-body (if (= 1 (count body))
(let [body-part1 (first body)]
(fn [ctx] (interpret ctx body-part1)))
(fn [ctx] (eval-do* ctx body)))
interpret-body
(let [iterate-counter (get ctx :iterate-max-counter)]
(if iterate-counter
(let [iteration-checker (fn []
(when (zero? (swap! iterate-counter dec))
(throw (ex-info "Maximum number of iterations reached"
{:type :sci.error/iterated-beyond-max
:iterate-max (:iterate-max ctx)
:last-expression (:expression ctx)}))))]
(if (= 1 (count body))
(let [body-part1 (first body)]
(fn [ctx]
(iteration-checker)
(interpret ctx body-part1)))

(fn [ctx]
(iteration-checker)
(eval-do* ctx body))))


(if (= 1 (count body))
(let [body-part1 (first body)]
(fn [ctx] (interpret ctx body-part1)))

(fn [ctx] (eval-do* ctx body)))))

f (fn run-fn [& args]
(let [;; tried making bindings a transient, but saw no perf improvement (see #246)
Expand Down
7 changes: 6 additions & 1 deletion src/sci/impl/opts.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
[{:keys [:bindings :env
:allow :deny
:realize-max
:iterate-max
:preset ;; used by malli
:aliases
:namespaces
Expand All @@ -100,14 +101,18 @@
imports (merge default-imports imports)
bindings bindings
_ (init-env! env bindings aliases namespaces imports load-fn)
iterate-max (or iterate-max (:iterate-max preset))
ctx (merge {:env env
:bindings {}
:allow (process-permissions (:allow preset) allow)
:deny (process-permissions (:deny preset) deny)
:realize-max (or realize-max (:realize-max preset))
:iterate-max iterate-max
:features features
:dry-run dry-run
:readers readers
::ctx true}
(normalize-classes (merge default-classes classes)))]
(normalize-classes (merge default-classes classes))
(when iterate-max
{:iterate-max-counter (atom iterate-max)}))]
ctx))
1 change: 1 addition & 0 deletions src/sci/impl/utils.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#?(:clj (set! *warn-on-reflection* true))

(derive :sci.error/realized-beyond-max :sci/error)
(derive :sci.error/iterated-beyond-max :sci/error)

(defn constant? [x]
(or (number? x) (string? x) (keyword? x)))
Expand Down

0 comments on commit 3c01531

Please sign in to comment.