How Recursion Got into Programming (2014)
vanemden.wordpress.com
vanemden.wordpress.com
Ironic that almost 20 years later the idea of dynamic activation had been so well absorbed that it was possible to write an influential paper all about how procedure call (and lambda) could be considered a goto (via tail recursion)!
+-----------------+
| return address |
+-----------------+
| executable code |
+-----------------+
The way you call a function is to store your desired return address in the first slot, and jump to the code in the second slot. The function jumps to the saved return address when it's done.We did it this way for years.
Now given that mindset, how could you even contemplate recursion? Where would you put all the return addresses?
static char *err = NULL;
But that specifically does not allocate it on the stack. Both heap allocation and stack allocation are dynamic, not static.There are things that are a bit more of a gray area in C and Unix and Win32/64 — are "statically allocated" variables of a shared library really statically allocated if they aren't there when the program starts up and they may be at some unexpected address? — but this is not one of them.
On the plus side, languages at the time (FORTRAN, COBOL) also required array sizes to be known at compile time, and memory allocators didn’t exist, so programs never ran out of memory (but would abort if an input file was larger than expected, even if the machine had plenty of memory available)
Consequently, CPU support for stacks was weak, because languages had no good use for them.
In theory, optimizers could do away with (part of) the activation record in cases where a function was called from only one place, or where arguments always were the same. I don’t know whether compilers that did that existed.
The aspect of this that bothers me is that automatically-managed stacks are not the only way to call procedures. Continuations for example are another way; actors yet another. But because automatically-managed stacks are present in virtually every CPU at the ISA level, many programmers never venture outside that model and have trouble understanding that others even exist, much less what advantages they might bring.
https://dspace.mit.edu/bitstream/handle/1721.1/5753/AIM-443....
that series of papers was released adjacent to the development of the scheme language which will generally optimize out the need for a call stack and replace it with essentially goto in certain cases.
as for 'changing the way you call a function' this subtopic is primarily of importance for language/compiler implementers; while you don't "need" to change the way you call a function, often doing so can speed up the program hugely, or allow things that were not otherwise possible (e.g. infinite recursion without stack overflow)
I'm not sure how actors or continuations could solve the problem of creating a distributed database of clients, or even of computing histograms on data in parallel, significantly more easily than more traditional programming paradigms.
The actor that owns the data reads its mailbox of requests and processes them without worrying about locks and things, because it has exclusive access.
The actor that needs to access the data doesn't worry about locks and things because it's not directly accessing it, it's just sending a message.
The system worries about locks and things, because the message mailboxes are shared state to some extent. If you can cleanly separate ownership of different pieces of data into different actors, you get parallelism. If there's too much dependency between the pieces of data, so that separating ownership makes things more complex, maybe that becomes more visible.
For gmail users, you could hash on the (normalized) username, and all users with hash between A and B would go to one actor, etc. You don't need (or want) parallelism for signup on a single user id, but you could hash or otherwise divide that work to that level.
You can also do a partitioned work queue with a (smallish) worker pool per partition; and have the queue keep track of any keys with a current worker, to assign further work to that worker (to avoiding parallel work on the same key and the locking that requires)
I have no doubt that the actor model or continuations can be used to solve any problem. I do however doubt that it would turn out to be simpler than using shared state and explicit locking; or using ledger-like approaches. I also don't think it would be more complex!
I believe that problems of distributed computing are inherently hard enough that the incidental complexity from your chosen modeling approach is not likely to be a significant portion of the overall complexity.
There may well be emergent complexity in how the simple parts go together, but that's going to happen anyway. Might as well make the parts simple where you can.
Incidentally, I think email is a great example of something with clear divisions of work --- an actor could easily be responsible for a single user's mailbox, and there should be no overlap in responsibility. Possibly, you could have one actor per folder, but gmail doesn't really have folders, so...
The pieces are simpler, but you might pay a greater cost in system-level design. In shared state models, you could have a more dynamic approach with a number of worker threads scaling with the number of requests (emails), with the only problem being contention when accessing the same piece of the shared storage, but at least no massive overhead from idle workers. The shared-state layer would be responsible for the locking strategy, and could even potentially optimize the required locking without necessarily changing the worker code, depending on the level of abstraction it offers workers to begin with.
> you would have hundreds of millions or even billions of actors being idle all day long
> but at least no massive overhead from idle workers.
You could certainly work to activate the actor for a user only when it's needed (while the user is online, or while an email is delivered); but the level of overhead for an actor is quite low --- it would be fine to have the actor simply always running, and doing nothing when there's nothing to do. You can run a million Erlang processes on a server, and if they're mostly idle, there's no big deal; if you need to run a billion processes, you would need a thousand servers; but my wild guess is storage is going to be a bigger factor than CPU, assuming that the gmail style large quotas do get significantly used, I could easily be wrong about storage requirements though.
I didn't know that Erlang can go up to such large numbers of processes, I knew they were very lightweight, but would have guessed maybe at most thousands to tens of thousands. Very nice that they can do that!
Even the first machine I ever programmed, the KA-10, had stack manipulation instructions though any location in memory could be a stack (which came with base and bounds for free) so threading was easy to write in assembly code.
There was no difference between a call stack and a stack data structure — we called them all “push down list”
One camp (against recursion) was trying to design a language that would be productive for programming in the (then) here and now, with the machines they had at the time; in other words, the language is molded after the machine. Since efficiently implementing recursion wasn't well understood at the time, adding it to the language was a no-go for them.
The Dijkstra side of the argument was that the language should be designed to be as elegant as possible, without much concern for the limitations of the machines of the day. The idea was that, once a broadly accepted programming language was designed, computer designs would follow to support it.
I think both are valid viewpoints (especially in context), although with perfect hindsight Dijkstra et al seem to have been the more prescient side.
https://vanemden.wordpress.com/2014/06/18/how-recursion-got-...