LiveScript icon indicating copy to clipboard operation
LiveScript copied to clipboard

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

Open determin1st opened this issue 5 years ago • 9 comments

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?

determin1st avatar Nov 26 '18 19:11 determin1st

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 avatar Nov 26 '18 20:11 rhendric

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 avatar Nov 27 '18 09:11 determin1st

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..

determin1st avatar Dec 22 '18 19:12 determin1st

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

ceremcem avatar Mar 17 '19 09:03 ceremcem

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

determin1st avatar May 26 '19 18:05 determin1st

Any update on this issue?

ceremcem avatar Apr 11 '22 10:04 ceremcem

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

rhendric avatar Apr 11 '22 18:04 rhendric

Would you accept a patch that entirely removes that optimization?

ceremcem avatar Apr 12 '22 11:04 ceremcem

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.

rhendric avatar Apr 12 '22 23:04 rhendric