Serverless, ReactPHP, and Expanding Frontiers, May 2019

(singke) #1
http://www.phparch.com \ May 2019 \ 39

Memory Abstractions


Internal Apparatus


Heap Space
As we just saw, PHP does let us see the call stack. The other
area that’s just as important is invisible. It’s called the heap.
When we create objects or PHP creates arrays, variables,
strings, or pretty much anything else, those items take up heap
space. As your program takes up more and more memory, it’s
heap space (or possibly buffer space) that expands.
In earlier articles, we saw how PHP arrays get allocated.
Where they get allocated, is in the heap. Generally speaking,
the stack and heap grow towards each other. When they meet,
we’re out of memory.

Registers
The hardware register is simply another memory abstrac-
tion, but we need some hardware perspective to explain its
existence. A register normally contains one computer word’s
worth of data. On 32-bit hardware, registers are usually 32
bits, with 64-bit registers existing on 64-bit hardware, and so
on.
Think of a register, for a moment, as something like a server.
You (as the running computer program) need a piece of infor-
mation—a datum. You send off a request for that information,
and sit idle, waiting, for that information to be returned. Once
received you can continue the program, but not until then.
When the server takes a long time to respond, your program
is going to be running very, very slowly. It’s spending nearly
all its time sitting, waiting for the next datum to arrive so that
it can proceed. It’s starved for input.
This is the situation inside a modern computer. The CPU
runs at full speed, L1 and L2 cache are on the order of 10X
slower, and main memory (RAM) is something on the order
of 50X slower. The CPU cannot run full speed unless it has
some way to hold on to its variables at full speed.
That something is the set of registers. Registers are simply
variables implemented in the hardware. Registers are
generally as fast as the CPU, but there are very few of them,


sometimes only one. I’m being vague on details here because
different CPU chipsets have different characteristics.
Why do we care? We care because the PHP compiler,
written in C, includes compiler instructions to “use hardware
registers when available.” Microsoft, for example, formal-
ized this as the __fastcall calling convention^14. With this
convention (which the PHP compiler does use), the first two
function arguments are passed via hardware registers rather
than being placed in the stack frame.
Thus registers are an abstraction related to performance.
Register access is faster than the L1 cache, which is faster
than the L2 cache, which is faster than RAM, which is faster
than the spinning hard drive, and so on. However, with
performance comes the tradeoff of time (speed) versus space.
Registers provide faster access than hardware cache but with
less capacity; hardware cache provides faster access than the
hard drive but the hard drive has far greater storage capacity.

Summary and Looking Ahead
Abstractions are tricky. That’s why they exist—to hide the
details that would distract from the task at hand. This month
we introduced abstractions related to memory.
We noted that memory implementations include these
three concepts: scope, persistence, and performance. Each
has numerous options and tradeoffs.
We introduced a project that I hope becomes an engaging
learning tool: implement a computer console as the sending
and receiving of text messages. We’ll use the tool next month
for investigating memory-management concepts in PHP.

Ed Barnard had a front-row seat when
the Morris Worm took down the Internet,
November 1988. He was teaching CRAY-1
supercomputer operating system internals to
analysts as they were being directly hit by the
Worm. It was a busy week! Ed continues to
indulge his interests in computer security
and teaching software concepts to others.
@ewbarnard

14 __fastcall calling convention: https://phpa.me/ms-fastcall-2019

Figure 4. First Six Triangular Numbers

Related Reading


Free download pdf