TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Stack safety for free?

48 点作者 tekkertje超过 3 年前

10 条评论

gopiandcode超过 3 年前
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 未加载
amalcon超过 3 年前
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.
lmm超过 3 年前
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.
tobz1000超过 3 年前
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.
wahern超过 3 年前
&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.
rincewind超过 3 年前
I want this guy to duke it out with the developers of chicken scheme
Gadiguibou超过 3 年前
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_rationalist超过 3 年前
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>
servytor超过 3 年前
Could this trick be used by ABCL (a Common Lisp implementation on the JVM) to allow for tail-call optimization?
评论 #29461558 未加载
auggierose超过 3 年前
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 未加载