TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Stack safety for free?

48 pointsby tekkertjeover 3 years ago

10 comments

gopiandcodeover 3 years ago
I would highly recommend that the author look into continuation-passing-style - the general construction they discover in their blog just seems to be a reinvention of the pretty old observation that we can make any program tail-call recursive if we write it in a CPS form. In fact, in languages like OCaml, it's fairly common to do this transformation to avoid stack overflow (typically paired with a Monad), especially when compiling to JS. The requirement of generators isn't actually needed - you just need first class functions - although, generators and coroutines do allow expressing the code in a slightly more concise way.
评论 #29466233 未加载
amalconover 3 years ago
I&#x27;m sort of hesitant to be &quot;that guy&quot;, but I&#x27;ve seen this kind of thing be a bit of a trap before. The technique of simulating a stack on the heap is useful in some situations, but it&#x27;s very misleading to call it a form of safety. It&#x27;s important to be clear about exactly what this buys you, and what it doesn&#x27;t buy you.<p>The benefit in a case like this is that you use memory a little more efficiently (since your stack frames contain exactly what you put in them) and you have a larger maximum size (since the heap tends to have more addressable space than the stack). This buys you more recursive depth (and therefore a larger input) before crashing. You can still run out of memory and crash. Arguably on systems like 64-bit Linux this is more dangerous, since the OOM killer is less predictable than stack overflow handling.<p>Of course it is often worth it to be able to handle the input you happen to have on hand, but that&#x27;s a functional improvement, not a safety improvement. It&#x27;s no substitute for an algorithm that just uses less memory.
lmmover 3 years ago
Yes, this is a standard technique. It&#x27;s more or less recovering the trampoline monad via continuations as a universal monad. You can use the same technique to implement other effects as well, if you&#x27;re working in the kind of crazy language that has continuations but no monads.
tobz1000over 3 years ago
This seems like a universal-ish mechanism for queueing your recursive function&#x27;s calls onto a dynamically resizeable stack (placed in the process&#x27;s heap). This is pretty cool, but I wouldn&#x27;t say it&#x27;s &quot;for free&quot;, performance-wise, as demonstrated by the performance figures in the article. Although it certainly makes the transformation cheap, implementation-wise.
wahernover 3 years ago
&gt; I demonstrate how to (ab)use generators&#x2F;coroutines to transform any recursive function into an iterative function with nearly zero code changes.<p>That&#x27;s not abuse. Reversing consumer&#x2F;producer calling protocols (i.e. reversing caller&#x2F;callee relationship) is exactly the fundamental purpose of generators and coroutines. It&#x27;s a useful insight, nonetheless, for those exploring the fundamental problem and solution space regarding concurrency and, more generally, the reification of execution flow.
rincewindover 3 years ago
I want this guy to duke it out with the developers of chicken scheme
Gadiguibouover 3 years ago
This seems like such an obvious abstraction, that it&#x27;s hard to believe noone came up with a macro to do this a long time ago.<p>However, that seems like a sign of a good or well explained idea.<p>While, I&#x27;ve never seen something similar being used before, I wouldn&#x27;t call it revolutionary since it&#x27;s not really making the function iterative, it&#x27;s just moving the call stack to the heap.<p>It would still be a very nice convenience for functions that are more elegantly expressed recursively.
The_rationalistover 3 years ago
Kotlin were the first to introduce the idea in my knowledge <a href="https:&#x2F;&#x2F;elizarov.medium.com&#x2F;deep-recursion-with-coroutines-7c53e15993e3" rel="nofollow">https:&#x2F;&#x2F;elizarov.medium.com&#x2F;deep-recursion-with-coroutines-7...</a>
servytorover 3 years ago
Could this trick be used by ABCL (a Common Lisp implementation on the JVM) to allow for tail-call optimization?
评论 #29461558 未加载
auggieroseover 3 years ago
I would like to have the option to just mark some functions to run on the heap instead of the stack. Without that option, recursive functions are really useless and cannot be used in the real world.
评论 #29461505 未加载
评论 #29463544 未加载
评论 #29462017 未加载
评论 #29461744 未加载