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

Force creation of a function in a loop? (unwanted optimiziation) #1079

Open
determin1st opened this issue Nov 26, 2018 · 9 comments
Open

Force creation of a function in a loop? (unwanted optimiziation) #1079

determin1st opened this issue Nov 26, 2018 · 9 comments
Labels

Comments

@determin1st
Copy link

Hi there,
I was assembling constructor functions in a map with similar code:

# create constructors map
map = new !->
  for a of type
    # create constructor
    @[a] = b = !->
      @prop1 = false
      @prop2 = false
    # set its prototype (methods)
    b.prototype = type[a]

the output:

map = new function(){
  var a, b;
  for (a in type) {
    this[a] = b = fn$;
    b.prototype = type[a];
  }
  function fn$(){
    this.prop1 = false;
    this.prop2 = false;
  }
};

Is there a way to force creation (not definition) logic?

@rhendric
Copy link
Collaborator

Huh. That looks like a bug to me, or at the very least a completely undocumented gotcha.

I can think of two workarounds, at least: change for a to for let a, or change b = !-> to b = do -> !->. Both add some overhead at run time, which is unfortunate, but it might be mostly optimized out by a JS engine.

@rhendric rhendric added the bug label Nov 26, 2018
@determin1st
Copy link
Author

determin1st commented Nov 27, 2018

ye, okay, i've created some factory function outside of the loop for now..
the construction overhead doesn't matter much in my case (it's a singleton object),
it just doesn't look "good enough".

@determin1st
Copy link
Author

determin1st commented Dec 22, 2018

well, i've got some answer from the JIT-man:

"Different constructor functions will have different hidden classes"

So my intent to make some set of "look-alike" constructors with the same hidden class won't work with this pattern. Maybe, it's not a bug, i see no reason to write such code constructs now..

@ceremcem
Copy link

Looks like very same issue: https://stackoverflow.com/a/41587540/1952991

@determin1st
Copy link
Author

@ceremcem yes yes, it's the same.. almost.

@ceremcem
Copy link

Any update on this issue?

@rhendric
Copy link
Collaborator

Nobody's working on LiveScript in secret. What you see is what you get.

@ceremcem
Copy link

Would you accept a patch that entirely removes that optimization?

@rhendric
Copy link
Collaborator

Short answer, I don't know if that's a good idea. It's been a few years since I've dug into LiveScript. If you've done the research and can make a logical case that the optimization doesn't help, I'd look at it. Given LiveScript's status as a legacy language I don't want to remove anything that some project might be relying on.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants