question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Discussion of adding TCO when closure is detected

See original GitHub issue

When closure is detected what about pre-processing using a combination of copying params/var decelerations to a state object (when those refs are used via closure) in the function and update all refs to point to this state.

And converting all declared functions in the closure to automatically invoked curried functions that apply the state ref via closure.

I believe this would fix any issues related to function scoping rules including referencing of primitive datatypes since the automatically invoked curried functions would be applied with object refs and would keep that reference even if the parent scope re-assigned a new object ref to the state object variable

Basically a statement like this

function someMethod(min, b) {
  const val = b.find(s => s.min < min)
   ...
  return someCondition ? someMethod(val.min, someStatementOf(val)) : val.min
}

would convert to something like this

function someMethod(min, b) {
  var _b, _min
  ...
  while(true){
   // at start of each iteration assign _state to a new object with updated values
   // this allows for automatically invoked curried functions that were applied in a previous iteration to 
   // keep ref to that object while the next iteration scope can work cleanly on the new object with 
   // updated values 
   const _state = {
     min,
   }
   const  val = b.find((state => s => s.min < state['min'])(_state))
   if(someCondition){
        _min = val.min
      _b = someStatementOf(val)
      b = _b
      min = _min
      continue;
    }
    else {
      return val.min
    }
  }
}

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:1
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
dakotamurphyucfcommented, Feb 12, 2019

@xtuc yea I completely overlooked that _state was being declared in the while loop and so you are right the automatically invoked curried function is not needed

I know safari figured out a way to implement TCO to spec so it definitely must be doable

Thinking more about it, I agree it is probably not a safe assumption.

We should be able to detect a closure in a closure and throw, I guess shadowing and scoping will become complicated. Just to avoid breaking the state for outer functions.

Could you elaborate a little more? Sorry if I am completely missing something I am pretty new to this subject 😆

I should have a pull-request ready by the end of the week to with a P.O.C.

0reactions
xtuccommented, Feb 18, 2019

We should be able to detect a closure in a closure and throw, I guess shadowing and scoping will become complicated. Just to avoid breaking the state for outer functions.

I was wondering about:

function someMethod(min, b) {
  function a() {
   var min;
   min = 5;
   function b() {
      var min;
      min = 9;
   }

  // maybe b();
 }

   a();
   ///...
  return someCondition ? someMethod(val.min, someStatementOf(val)) : val.min
}

The two min declarations should end up overwritting themself in the state.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Uncover All Hidden Lifecycle Ownership Costs. Find TCO in 6 ...
1. Describe the Acquisition or investment and its role in the business, Define TCO Lifespan in years, as either ownership life, economic life,...
Read more >
Tail call optimiztion (TCO) in ES6 & Node.js · Issue #3 - GitHub
There is some concern about TCO and how it plays out in VMs and it'd be worth us being aware of this so...
Read more >
Understanding Total Cost of Ownership of Serverless and ...
Last week I published an article discussing when serverless is more expensive than containers. In that post, I did a simple comparison ...
Read more >
What Does TCO Really Mean? - Zimbra : Blog
But, not understanding the nuances of total cost of ownership (TCO) can add up over time, both in CapEx and OpEx.
Read more >
Three-Way Handshake - an overview | ScienceDirect Topics
2 TCP Connection Establishment. As discussed above, a connection is established using a three-way handshake procedure. The flow of data in each direction...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found