Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initialize stateless context #369

Closed
borkdude opened this issue Jul 20, 2020 · 3 comments
Closed

Initialize stateless context #369

borkdude opened this issue Jul 20, 2020 · 3 comments

Comments

@borkdude
Copy link
Collaborator

borkdude commented Jul 20, 2020

Sometimes it's desirable to re-use the same set of options in eval-string for performance reasons, like you can do with eval-string* but without the statefulness. It might be nice to offer an API around this.

This can be achieved right now with:

(def mutable-ctx (sci/init {:preset :termination-safe}))
(def immutable-ctx (update mutable-ctx :env deref))
(defn init-ctx [immutable-ctx] (update immutable-ctx :env atom))
(sci/eval-string* (init-ctx proto-ctx) "(+ 1 2 3)" )
;;=> 6
(sci/eval-string* (init-ctx proto-ctx) "(def x 1) x")
1
(sci/eval-string* (init-ctx proto-ctx) "x")
Execution error (ExceptionInfo) at sci.impl.utils/throw-error-with-location (utils.cljc:54).
Could not resolve symbol: x [at line 1, column 1]
@borkdude borkdude changed the title Initialize unstateful context Initialize stateless context Jul 20, 2020
@ikitommi
Copy link
Contributor

did a quick test using criterium:

(ns user
  (:require [criterium.core :as cc]
            [sci.core :as sci]
            [sci.impl.interpreter :as scii]
            [sci.impl.opts :as scio]))

;; 25µs
(cc/quick-bench (sci/eval-string "(+ 1 1)" nil))

(def ctx (update (scio/init nil) :env deref))

;; 13µs
(cc/quick-bench (scii/eval-string* (update ctx :env atom) "(+ 1 1)"))

@borkdude
Copy link
Collaborator Author

@ikitommi Note that eval-string* and init are part of the public API in sci.core.

@borkdude
Copy link
Collaborator Author

Implemented as follows:

In a multi-user environment it can be useful to give each user their own
context. This can already be achieved with eval-string, but for performance
reasons it may be desirable to initialize a shared context. This shared context
can then be forked for each user so that changes in one user's context
aren't visible for other users:

(def forked (sci/fork sci-ctx))
(sci/eval-string* forked "(def forked 1)")
(sci/eval-string* forked "forked") ;;=> 1
(sci/eval-string* sci-ctx "forked") ;;=> Could not resolved symbol: forked

bbss pushed a commit to bbss/sci that referenced this issue Aug 18, 2020
bbss pushed a commit to bbss/sci that referenced this issue Aug 18, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants