noprompt / meander

Tools for transparent data transformation
MIT License
918 stars 55 forks source link

add `:meander.epsilon/fast` flag #193

Closed ribelo closed 2 years ago

ribelo commented 3 years ago

Disable type checks and bound checks and use fastest possible functions for lookup

noprompt commented 3 years ago

Based on some discussion in the Slack channel, I think we should build on this a little more.

First, I think there are roughly 4 levels of safety (see the table below) that we can derive by considering the combinations of enabling/disabling type checking/native method calling: benign, safe, unsafe, and dangerous. I think what we should do is the following.

+-------------+----------------+----------------------------+-----------+
| type-check? | native-method? | Result                     | Name      |
+-------------+----------------+----------------------------+-----------+
| true        | false          | (f object ,,,)             | Slow      |
+-------------+----------------+----------------------------+-----------+
| true        | true           | (.method ^Type object ,,,) | Safe      |
+-------------+----------------+----------------------------+-----------+
| false       | false          | (f object ,,,)             | Unsafe    |
+-------------+----------------+----------------------------+-----------+
| false       | true           | (.method ^Type object ,,,) | Dangerous |
+-------------+----------------+----------------------------+-----------+
noprompt commented 3 years ago

@ribelo By desugar I meant adding the following functions to the meander.environment.epsilon namespace.

(defn desguar-unsafe [env]
  (if (:meander.epsilon/unsafe env)
    (assoc (dissoc :meander.epsilon/unsafe)
           :meander.epsilon/no-type-check true
           :meander.epsilon/use-native-methods false)
    env))

(defn desguar-dangerous [env]
  (if (:meander.epsilon/dangerous env)
    (assoc (dissoc :meander.epsilon/unsafe)
           :meander.epsilon/no-type-check true
           :meander.epsilon/use-native-methods true)
    env))

(defn desugar [env]
  (-> env
      desugar-unsafe
      desugar-dangerous))

Then use the desugar function in m.match/analyze-match-args and similar functions where we initialize the environment such as here.

Additionally, I think we should add these predicates to meander.environment.epsilon.

(defn unsafe? [env]
  (and (true? (:meander.epsilon/no-type-check env))
       (false? (:meander.epsilon/use-native-methods env))))

(defn dangerous? [env]
  (and (true? (:meander.epsilon/no-type-check env))
       (true? (:meander.epsilon/use-native-methods env))))

We update the predicates in meander.match.ir.epsilon to use these.

ribelo commented 3 years ago

@noprompt

I removed unsafe? and dangerous? and added native-method? It seems to me that because we have desugar, the above have no function. I also used desugar in a different place than you suggested, because I wanted to use it as early as possible so that all functions that use env already have updated variables.

I also noticed that neither find nor search use r.environment/default, so I took the liberty of adding that.

EDIT: Fix markdown.

noprompt commented 3 years ago

I also noticed that neither find nor search use r.environment/default, so I took the liberty of adding that.

noprompt commented 2 years ago

@ribelo From what I can tell this seems good to me. The only thing left would be to delete those fields and the fast? predicate and you can hit the merge button. :smile: