sanctuary-js / sanctuary-def

Run-time type system for JavaScript
MIT License
294 stars 23 forks source link

dramatically improve performance of UnaryType and BinaryType #244

Closed davidchambers closed 5 years ago

davidchambers commented 5 years ago

Commit message:

The predicate and extractor functions provided to the UnaryType and BinaryType type constructors play an important role in type checking. Each predicate function may be applied hundreds of times when a small program is run. This is not a problem, in and of itself.

If these oft-applied functions are themselves type-checked, though, run-time type checking is significantly more expensive as a result.

This commit disables type checking of predicate and extractor functions.

On my computer, the following program takes about half a second to run on this branch compared with about five seconds on master:

const $ = require ('.');

const a = $.TypeVariable ('a');

const xs = []; for (let x = 0; x < 1000; x += 1) xs.push ([[[[[[[[[x]]]]]]]]]);

$.create
  ({checkTypes: true, env: [$.Array ($.Unknown), $.Number]})
  ('I')
  ({})
  ([a, a])
  (x => x)
  (xs);

Note that providing $.env as the environment does not demonstrate the problem because the version of $.Array included in $.env is not defined via def.