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

Type checking #420

Open
hemml opened this issue Feb 3, 2022 · 7 comments
Open

Type checking #420

hemml opened this issue Feb 3, 2022 · 7 comments

Comments

@hemml
Copy link
Contributor

hemml commented Feb 3, 2022

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?

@nagy
Copy link
Contributor

nagy commented Feb 3, 2022

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

@davazp
Copy link
Member

davazp commented Feb 3, 2022

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.

@hemml
Copy link
Contributor Author

hemml commented Feb 3, 2022

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

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

@vlad-km
Copy link
Member

vlad-km commented Sep 7, 2022

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?

@davazp
Copy link
Member

davazp commented Sep 7, 2022

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.

@vlad-km
Copy link
Member

vlad-km commented Sep 7, 2022

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.

@vlad-km
Copy link
Member

vlad-km commented Sep 7, 2022

safety 0

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

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

4 participants