7 comments

  • gpderetta8 hours ago
    &gt; Your CPU doesn’t know or care what functions are [...]<p>Well, most architectures do not use plain jumps with implicit arguments for function calls, but have explicit call&#x2F;ret or branch-with-link instructions. Even those that used jumps had branch hints. The reason is that the microarchitecture must know about call&#x2F;ret pairs to be able to predict the return path as the generic dynamic predictor is just not good enough for such a common path.<p>Reduced prediction performance compared normal calls is actually a concern for some coroutine and async code.<p>&gt; in C we don’t have any dynamic lookup inside functions—every dynamic jump comes from an explicit conditional statement<p>function pointers.
    • adamnew1234567 hours ago
      &gt; Well, most architectures do not use plain jumps with implicit arguments for function calls, but have explicit call&#x2F;ret or branch-with-link instructions.<p>Anyone who wants to go deeper on this would be well served doing some reading on &quot;return oriented programming&quot; and shadow stacks &#x2F; control flow integrity. I got turned onto this by a few LWN articles that talk about the subject (<a href="https:&#x2F;&#x2F;lwn.net&#x2F;Articles&#x2F;940403&#x2F;" rel="nofollow">https:&#x2F;&#x2F;lwn.net&#x2F;Articles&#x2F;940403&#x2F;</a> among others).<p>It turns out that &quot;jump to this pointer&quot; is actually a dangerous construct, and it&#x27;s not something you get much high-level exposure to since functions as an abstraction are so unleaky.
  • jcranmer6 hours ago
    &gt; Your CPU doesn’t know or care what functions are<p>This has already been commented on by a couple of people, but yes, your CPU absolutely does care a lot about functions. At the very least, call&#x2F;ret matching is important for branch prediction, but the big arches nowadays have shadow stacks and CFI checks that require you to use call&#x2F;rets as regular functions. x86 has a more thoroughly built-in notion of functions, since they have a (since mostly-defunct) infrastructure for doing task switching via regular-ish call instructions.<p>&gt; The toString method that gets called depends on the type of the receiver object. This isn’t determined at compile time, but instead a lookup that happens at runtime. The compiler effectively generates a switch statement that looks at the result of getClass and then calls the right method. It’s smarter than that for performance I’m sure, but conceptually that’s what it’s doing.<p>No, it&#x27;s conceptually doing the exact opposite. Class objects have a vtable pointer, a pointer to a list of functions, and the compiler is reading the vtable and calling the n&#x27;th function via function pointer. The difference is quite important: vtables are an inherently open system (anyone can define their own vtable, if they&#x27;re sufficiently crazy), but switches are inherently closed (the complete set of possible targets has to be known at compile-time). Not that I&#x27;ve written it up anywhere, but I&#x27;ve come to think of the closed nature of switch statements as fundamentally anathema to the ideals of object-oriented programming.
    • fanf25 hours ago
      The vtable vs switch dichotomy was called the “expression problem” by Philip Wadler <a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Expression_problem" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Expression_problem</a>
  • rdevilla8 hours ago
    &gt; To start with, you need to remember that functions don’t exist. They’re made up. They’re a social construct.<p><a href="https:&#x2F;&#x2F;www.felixcloutier.com&#x2F;x86&#x2F;call" rel="nofollow">https:&#x2F;&#x2F;www.felixcloutier.com&#x2F;x86&#x2F;call</a><p>Sufficiently large distances of abstraction from the concrete, underlying mechanics are indistinguishable from religion and superstitious belief. Expect LLMs to widen this gap in understanding.<p>We are not far away from the tech priests of the Adeptus Mechanicus.
    • throwaway274487 hours ago
      &gt; Sufficiently large distances of abstraction from the concrete, underlying mechanics are indistinguishable from religion and superstitious belief.<p>Hell, you can apply this to all culture. Humans are not very rational or observant beings, despite how much some of us insist to the contrary. Basically any abstraction is a) going to be taken at face value, or on an assumption that the world is inherently structural, and b) the assumption is essentially guaranteed to either be inaccurate or tautological.<p>&quot;Religion&quot; was invented about 500 years ago to distinguish from the secularists, but the distance between them these days is mostly one of chosen ritual and the direction in which arrogance is spewed. After all, the church that persecuted Galileo was hardly any more irrational than the states we entrust <i>nuclear weapons</i> to is today.
  • hackingonempty8 hours ago
    &gt; If you want to read more, I’d recommend starting with the Effekt and Koka language tours<p>Instead of exploring a research language that nobody uses you could try a mature effects system for a semi-popular language. I think Zio is great and runs on the JVM and ScalaJS. <a href="https:&#x2F;&#x2F;zio.dev&#x2F;" rel="nofollow">https:&#x2F;&#x2F;zio.dev&#x2F;</a>
    • tie-in6 hours ago
      It&#x27;s also possible to implement a functional effect system in under 30 lines of JavaScript: <a href="https:&#x2F;&#x2F;lackofimagination.org&#x2F;2025&#x2F;11&#x2F;managing-side-effects-a-javascript-effect-system-in-30-lines-or-less&#x2F;" rel="nofollow">https:&#x2F;&#x2F;lackofimagination.org&#x2F;2025&#x2F;11&#x2F;managing-side-effects-...</a>
    • jiehong5 hours ago
      I heard ocaml had one?
  • noelwelsh8 hours ago
    This article would benefit from an introduction that lays out the structure of what is to come. I&#x27;m expecting an article on effect systems, but it jumps straight into a chunky section on the implementation of function calls. I&#x27;m immediately wondering why this is here, and what is has to do with effect systems.<p>Also, this is a very operational description: how it works. It&#x27;s also possible to give a denotational description: what it means. Having both is very useful. I find that people tend to start with the operational and then move to the denotational.
    • willhbr8 hours ago
      That&#x27;s fair feedback, thanks<p>I focussed on how it works since that&#x27;s what I set out to understand myself after seeing effects mentioned a few times
  • jiehong5 hours ago
    &gt; having to add IO effects to all functions<p>Sound like Haskell, and its Monads. I think it does end up being very similar in the end.<p>Would you compare effects and monads?
    • yunnpp5 hours ago
      I came here to ask the same question; did somebody just reinvent monads?<p>Also note that much of Haskell now uses MonadIO, so you are no longer constrained to IO or having to manually lift IO into your monad, so long as your monad instances MonadIO. This makes code reuse across libraries trivial. I say this because their div() example says that it can only be called from an Exception effect context, which is like old-school IO-everywhere Haskell. It seems to me like they&#x27;re going to run into the function colour problem.