jscl-project / jscl

A Lisp-to-JavaScript compiler bootstrapped from Common Lisp

Home Page:https://jscl-project.github.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Type checking

hemml opened this issue · comments

commented

I'm trying to perform some computations on the browser-side, which is rather slow sometimes.
I see in the JSCL-generated code a lot of type checking like this:

var x2=Math.sqrt(v25);
if (typeof x2!='number') throw 'Not a number!';

It seems strange, because Math.sqrt can return numbers only and, anyway, that checks are slowdowns the code.
Is there a way to disable type checking in generated code?

I cannot answer the code-generation question, but according to MDN, Math.sqrt can return NaN sometimes:

Return value

The square root of the given number. If the number is negative, NaN is returned. 

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/sqrt#return_value

There is no way to disable the checks right now. It could be nice to add support for (declare (optimize speed)) to disable those.

The compiler is rather naive and the type checks are everywhere. It also makes the output huge, because those are inlined. It'd be nice to wrap those type checks in JS functions in prelude, so (+ x y) would compile to just a function call with the typecheck in a single place.

Of course, it would be even better to do some basic analysis in the compiler and remove some unnecessary checks. I started a few attempts to add a smarter compiler, but never got enough time to finish it. But if anyone has time and some will, it is doable.

commented

cannot answer the code-generation question, but according to MDN, Math.sqrt can return NaN sometimes:

Sure, but typeof NaN returns "number" anyway :)

commented

There is no way to disable the checks right now. It could be nice to add support for (declare (optimize speed)) to disable those.

Maybe go this way:

  • Responsibility for verification lies with the user - we have check-type, typecase, and assert. If the user doesnt not check the type, it is done by JS and throws an exception. Compiler messages do less help localize the source of errors.
  • Remove those endless number/string type checks from the compiler.

Maybe it's easier than compiler redesign?
Mmm?

I think the default should be to check the types always.
But we can disable them kind of easily with a declaration without improving the compiler.
Indeed if the user chooses safety 0, then they could add check-type.

commented

I think the default should be to check the types always.

Definitely so. by default, type checking is always enabled.

Introduce the pragma disable-check-type directive.

  1. disables all checks at the top level
    l cl-user> (pragma :disable-check-type t)
  2. locally in the function, only for the function at compile time
        (pragma :disable-check-type t)
        (let ()))

As an option.

commented

safety 0

it should disable all checks: arrays, indices, strings, lists.