Skip to content

Optimization: share Schema objects between property registry and body #1294

Description

@frenchy64

In [:schema {:registry {::alias :string}} ::alias] two :string Schema instances are constructed.

One is from constructing the property registry, and the other from constructing the schema body.

Since they are in the same scope, they can share the same Schema instance.

Here are some test cases demonstrating the problem.

(defn counting-registry []
  (let [count-into-schemas (atom 0)
        reg (mr/simple-registry (assoc (m/default-schemas)
                                       ::counting (m/-proxy-schema {:type ::counting
                                                                    :fn (fn [p c o]
                                                                          (assert (empty? c))
                                                                          (swap! count-into-schemas inc)
                                                                          [[] [] (m/schema :int o)])})))]
    {:reg reg :counter count-into-schemas}))

(defn is-counting-times [?schema i]
  (let [{:keys [reg counter]} (counting-registry)
        s (m/schema ?schema {:registry reg})]
    (is (= @counter i))))

(deftest share-property-registry-with-body-test
  (is-counting-times [:schema {:registry {::BAR ::counting}} :int] 1)
  (is-counting-times [:schema {:registry {::BAR ::counting}} ::BAR] 2) ;; should be 1
  (is-counting-times [:schema {:registry {::BAR ::counting}} [:tuple ::BAR ::BAR]] 3)  ;; should be 1
  (is-counting-times [:schema {:registry {::BAR ::counting}} [:tuple ::BAR ::BAR ::BAR]] 4))  ;; should be 1

This issue was one several root causes for exponential memory blowup when creating certain schemas. A solution is prototyped in #1284

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions