Most of the time, for most of the work you do as a programmer, you’ll only make brief departures from the main codebase. You’ll investigate a problem, check out the files you need to fix the problem, test and review your changes, then commit them back into the main branch. You might complete the whole cycle in a day, though that’s pretty quick if there’s any testing to be done; more typically, you’ll spend days with stuff checked out.
Eventually, though, you run into a task where this simple model falls apart. You’re teaming up on something with another programmer, say. When you’re working solo, the work in progress only exists on your machine, but that doesn’t work when you’re partnered with someone. The two of you need to maintain a shared version of the work in progress.
The standard answer to this is to create a new branch in your source code control system for the work the two of you have planned. This branch starts as a copy of the main branch, but quickly diverges from the main branch as you work. You probably look for occasional chances to integrate changes from the main branch into your branch to keep up with the work being done by the rest of the team, resolving any conflicts introduced by changes on the main branch. Eventually your work is complete; you integrate one last time from the main branch, do a final test, review your work, and check in.
Bumps in the Road
This approach works—that’s why it’s the standard answer—but it’s not free of problems.
Integrating changes from the main branch can be hard, for instance. The rest of the team has no visibility into what you’re doing on your branch, so it’s easy for them to break your work in progress. This can be a minor annoyance, as when someone introduces a new call into the system you’re reworking. It can be more troublesome, as when someone fixes a bug in the old system that has to be mirrored in your reworked version. Or it can be truly painful, like someone deciding to reorganize a source file, destroying all the diffs you’ve been relying on.
If you’re reworking the old system in place, which is typically the case, then it’s easy to lose sight of how the old system worked. You started with source code for the old system, which you could consult to understand its function—but every line you change makes it harder to see which behaviors were original and which have been added. The workarounds for this—like keeping a full copy of the original source to consult, or continually referring to diffs from the original source—are painful.
If you’re part of a big team, then just keeping up with the churn from the rest of the team can be a challenge. Typically, the core of the work you’re focused on is confined to a handful of source files, but calls to that core can be scattered across dozens of files. Every change to any of those dozens of files is a potential merge conflict. If you’ve got a big team of people checking in changes to the main branch, then the smaller team working on a branch can get saturated just integrating changes.
It’s easy to get lost in a tangle of source-code control branches. The flexibility branching provides is tempting, and it’s easy to get carried away. The simple branching I described for the standard approach—a single branch leaving the main branch and rejoining it later—is easy enough to track. If it gets more complicated than that—throwaway branches for testing new approaches to the problem, branches for personal backups, multiple main branches to manage release staging—you can get lost pretty quick.
We’ve followed this standard branch-and-change approach for a few big changes at Sucker Punch. The results were painful, so we’ve tried alternatives. We’ve had pretty good luck with a particular approach, a duplicate-and-switch model .
Build a Parallel System Instead
Here’s the idea—instead of changing a system in place, we build a parallel system. The new system is checked in while work is still in progress, but is only enabled (via a runtime switch) for the small team working on it. Most of the team uses the old system, never touching the new code paths. When the new system is ready to go, we use the runtime switch to enable it for everyone. Once everyone is successfully using the new system, we excise the old one from the project.
There’s a great aphorism from Kent Beck that applies:
For each desired change, make the change easy (warning: this may be hard), then make the easy change.
It’s straightforward to apply this aphorism on small projects. The parallel system technique is a way to apply it to big changes on larger projects, where the preparatory work spans many bits of code committed from multiple coders. All the hard work of building the parallel system sets you up for the cut-over point, which is easy in comparison.
A Concrete Example
Let’s look at a real-world example. It’s going to take a few pages to set up the context, but we’ll return to the idea of building a parallel system before too long.
At Sucker Punch we use a stack-based memory allocator in much of our code, instead of relying on the standard heap allocator in all cases. The basic idea of a stack allocator is to simplify allocation by not freeing allocated blocks—at least, not individual blocks. With the standard heap allocator, each allocated block must later be freed. A stack allocator works more like variables on the call stack—any block allocated in a function is automatically freed when the function exits. Stack-based allocation is easier to use because you don’t have to worry about freeing blocks. It’s also wicked fast, which is important in a lot of our game programming scenarios.
Scopes are defined with a “context” object. All stack allocations are associated with the current context. When a context goes out of scope, all blocks associated with that context are freed. The blocks have all been allocated sequentially, so this mass freeing is trivial, just as each allocation was. We’re just pushing pointers around. Here’s the allocator :
class StackAlloc
{
friend class StackContext;
public:
static void * alloc(int byteCount);
template <class T>
static T * alloc(int count)
{ return static_cast<T *>(alloc(sizeof(T) * count)); }
protected:
struct Index
{
int m_chunkIndex;
int m_byteIndex;
};
static Index s_index;
static vector<char *> s_chunks;
};
StackAlloc::Index StackAlloc::s_index;
vector<char *> StackAlloc::s_chunks;
const int c_chunkSize = 1024 * 1024;
void * StackAlloc::alloc(int byteCount)
{
assert(byteCount <= c_chunkSize);
while (true)
{
int chunkIndex = s_index.m_chunkIndex;
int byteIndex = s_index.m_byteIndex;
if (chunkIndex >= s_chunks.size())
{
s_chunks.push_back(new char[c_chunkSize]);
}
if (s_index.m_byteIndex + byteCount <= c_chunkSize)
{
s_index.m_byteIndex += byteCount;
return &s_chunks[chunkIndex][byteIndex];
}
s_index = { chunkIndex + 1, 0 };
}
}
The stack allocator tracks a list of chunks of memory, where blocks may have been allocated from the chunks at the start of the list. If a requested allocation fits in the last chunk with allocated blocks, we add the new block to that chunk, right after the last allocated block in that chunk. If it doesn’t, we add it at the beginning of the next chunk, allocating new chunks when necessary.
The context object is even more trivial. It just remembers the next place we’d allocate a block:
class StackContext
{
public:
StackContext()
: m_index(StackAlloc::s_index)
{ ; }
~StackContext()
{ StackAlloc::s_index = m_index; }
protected:
StackAlloc::Index m_index;
};
This allocation model has a few advantages. It’s much faster than a general heap allocator, since allocations are simple pointer math and releasing a context is almost free. 1 More importantly, it has great locality, since consecutively allocated blocks are right next to each other in memory. And since the blocks are freed automatically, there’s no risk that you’ll forget to free one.
There are plenty of disadvantages, too, 2 but there are two main use cases where stack-based allocation is a good fit. First, you often need to allocate some scratch space for a function’s internal logic, and stack-based allocation is perfect for that. Second, if you’re returning variable-sized data, then allocating space for that returned data via StackAlloc works really well .
Stack Allocation in Practice
The original Sucker Punch version of stack allocation looked roughly like the code in the previous section. Over time, however, we’ve mostly migrated to using stack-based vectors instead—a quick search of the codebase turns up a few hundred calls to raw stack-based allocation, but five thousand uses of stack-based vectors.
Here’s a simplified version of the stack-based vector class, with method names chosen to match the standard C++ vector:
template <class ELEMENT>
class StackVector
{
public:
StackVector();
~StackVector();
void reserve(int capacity);
int size() const;
void push_back(const ELEMENT & element);
void pop_back();
ELEMENT & back();
ELEMENT & operator [](int index);
protected:
int m_count;
int m_capacity;
ELEMENT * m_elements;
};
Creating the vector is trivial, since it starts with no elements. Destroying it is almost as simple. There’s no memory to free, so all that’s needed is to call the destructor for each of the elements in the vector:
template <class ELEMENT>
StackVector<ELEMENT>::StackVector() :
m_count(0),
m_capacity(0),
m_elements(nullptr)
{
}
template <class ELEMENT>
StackVector<ELEMENT>::~StackVector()
{
for (int index = 0; index < m_count; ++index)
{
m_elements[index].~ELEMENT();
}
}
The basic vector operations are straightforward. Note that if the vector needs to be resized, the old memory doesn’t need to be freed. The elements need copying to the new storage, but that’s it:
template <class ELEMENT>
void StackVector<ELEMENT>::reserve(int capacity)
{
if (capacity <= m_capacity)
return;
ELEMENT * newElements = StackAlloc::alloc<ELEMENT>(capacity);
for (int index = 0; index < m_count; ++index)
{
newElements[index] = move(m_elements[index]);
}
m_capacity = capacity;
m_elements = newElements;
}
template <class ELEMENT>
int StackVector<ELEMENT>::size() const
{
return m_count;
}
template <class ELEMENT>
void StackVector<ELEMENT>::push_back(const ELEMENT & element)
{
if (m_count >= m_capacity)
{
reserve(max(8, m_capacity * 2));
}
new (&m_elements[m_count++]) ELEMENT(element);
}
template <class ELEMENT>
void StackVector<ELEMENT>::pop_back()
{
m_elements[--m_count].~ELEMENT();
}
template <class ELEMENT>
ELEMENT & StackVector<ELEMENT>::back()
{
return m_elements[m_count - 1];
}
template <class ELEMENT>
ELEMENT & StackVector<ELEMENT>::operator [](int index)
{
return m_elements[index];
}
Here’s a simple usage example:
void getPrimeFactors(
int number,
StackVector<int> * factors)
{
for (int factor = 2; factor * factor <= number; )
{
if (number % factor == 0)
{
factors->push_back(factor);
number /= factor;
}
else
{
++factor;
}
}
factors->push_back(number);
}
So far, so good! It’s just a vector with better performance in some well-defined circumstances, which is a pretty simple thing to get your head around. That’s why stack vectors are used so widely in our codebase .
A Cloud on the Horizon
But there’s a nagging problem. There are two main use cases for stack vectors—allocating scratch storage for a routine and returning values from a routine—and they don’t mesh. Let me explain.
Imagine you’d like to write a function for a video game that returns enemies within five meters of the player. Imagine further that you’ve got a good starting point: a function that returns all nearby characters, whatever their emotional relationship to the player, along with their positions, all in a stack vector. It should be possible to call that code to get nearby characters, then filter out everyone except the enemies.
Here’s the code you’d like to write:
void findNearbyEnemies(
float maxDistance,
StackVector<Character *> * enemies)
{
StackContext context;
StackVector<CharacterData> datas;
findNearbyCharacters(maxDistance, &datas);
for (const CharacterData & data : datas)
{
if (data.m_character->isEnemy())
{
enemies->push_back(data.m_character);
}
}
}
But this doesn’t work, at least not with the simple stack allocator defined in the last section.
The problem is that two stack contexts are getting tangled up. You create a StackContext and StackVector for the character data returned by findNearbyCharacters , and this works great. But when you call enemies->push_back in the second half of the function, it will allocate memory from the stack context you created locally, rather than the stack context associated with the enemies array. The enemies array is probably defined in the calling function, inside a different stack context.
That’s bad! You’d get unpredictable results if you used the returned array in the caller. In fact, when the real Sucker Punch stack vector class tries to allocate memory with a mismatched stack context, it asserts to catch exactly this kind of bug. It’s possible to work around the stack context tangle, but I’m not going to show you the code because, frankly, it’s a little embarrassing.
Making Stack Contexts a Little Smarter
There’s a fairly straightforward way to fix this. The stack allocator defined earlier is global, like the standard heap allocator—when you allocate a block, it’s associated with the stack context created most recently. That was the root cause of the tangled stack contexts we’re trying to untangle. If it was possible to associate a block with a particular stack context, we could fix things.
That’s not hard. The easiest way to do this is to move the alloc method to the StackContext object. If you’re allocating from the current context, you’ll allocate bytes from a shared stack. In the uncommon case that you’re allocating from some context that’s not current, you’ll switch to a backup allocation strategy. You can do this without sacrificing the benefits of stack allocation if you’re careful with the implementation.
First, here is the restructured StackContext class:
class StackContext
{
public:
StackContext();
~StackContext();
void * alloc(int byteCount);
template <class T>
T * alloc(int count)
{ return static_cast<T *>(alloc(sizeof(T) * count)); }
static StackContext * currentContext();
protected:
struct Index
{
int m_chunkIndex;
int m_byteIndex;
};
static char * ensureChunk();
static void recoverChunk(char * chunk);
struct Sequence
{
Sequence() :
m_index({ 0, 0 }), m_chunks()
{ ; }
void * alloc(int byteCount);
Index m_index;
vector<char *> m_chunks;
};
Index m_initialIndex;
Sequence m_extraSequence;
static const int c_chunkSize = 1024 * 1024;
static Sequence s_mainSequence;
static vector<char *> s_unusedChunks;
static vector<StackContext *> s_contexts;
};
New functions are created to create big chunks of memory as needed, reusing them when they’re no longer needed:
char * StackContext::ensureChunk()
{
char * chunk = nullptr;
if (!s_unusedChunks.empty())
{
chunk = s_unusedChunks.back();
s_unusedChunks.pop_back();
}
else
{
chunk = new char[c_chunkSize];
}
return chunk;
}
void StackContext::recoverChunk(char * chunk)
{
s_unusedChunks.push_back(chunk);
}
The code to allocate a new block into the last chunk moves into a new Sequence object:
void * StackContext::Sequence::alloc(int byteCount)
{
assert(byteCount <= c_chunkSize);
while (true)
{
int chunkIndex = m_index.m_chunkIndex;
int byteIndex = m_index.m_byteIndex;
if (chunkIndex >= m_chunks.size())
{
m_chunks.push_back(new char[c_chunkSize]);
}
if (m_index.m_byteIndex + byteCount <= c_chunkSize)
{
m_index.m_byteIndex += byteCount;
return &m_chunks[chunkIndex][byteIndex];
}
m_index = { chunkIndex + 1, 0 };
}
}
The stack context methods that are left are simple. Track the current set of nested stack contexts. When an allocation is made from the topmost stack context (the typical case), it comes from a global sequence. When an allocation is made from any other stack context (the exceptional case), a sequence owned by that stack context is used instead:
StackContext::StackContext() :
m_initialIndex(s_mainSequence.m_index),
m_extraSequence()
{
s_contexts.push_back(this);
}
StackContext::~StackContext()
{
assert(s_contexts.back() == this);
for (char * chunk : m_extraSequence.m_chunks)
{
recoverChunk(chunk);
}
s_mainSequence.m_index = m_initialIndex;
s_contexts.pop_back();
}
void * StackContext::alloc(int byteCount)
{
return (s_contexts.back() == this) ?
s_mainSequence.alloc(byteCount) :
m_extraSequence.alloc(byteCount);
}
In normal usage, the backup sequence in the stack context isn’t used, so there’s very little penalty for this new functionality. Allocation is still fast and easy.
The new stack context code forces a few simple changes to the StackVector class, which now needs to specify which stack context to allocate from. Leaving out the stuff that doesn’t change:
template <class ELEMENT>
class StackVector
{
public:
StackVector(StackContext * context);
protected:
StackContext * m_context;
int m_count;
int m_capacity;
ELEMENT * m_elements;
};
template <class ELEMENT>
StackVector<ELEMENT>::StackVector(StackContext * context) :
m_context(context),
m_count(0),
m_capacity(0),
m_elements(nullptr)
{
}
template <class ELEMENT>
void StackVector<ELEMENT>::reserve(int capacity)
{
if (capacity <= m_capacity)
return;
ELEMENT * newElements = m_context->alloc<ELEMENT>(capacity);
for (int index = 0; index < m_count; ++index)
{
newElements[index] = move(m_elements[index]);
}
m_capacity = capacity;
m_elements = newElements;
}
With these new implementations, we’re in good shape. We’ve kept the positive aspects of stack allocations—lightning-fast allocation and free operations, plus good locality—while fixing the annoying inability to mix and match local scratch space and variable-sized return values .
Migrating from Old Stack Contexts to New Ones
Now it’s time to circle back to the main premise of the chapter, which you’ve likely forgotten in this blizzard of source code. You’ll recall that we’ve got an old version of stack allocation and a new one, and they’re not exactly the same.
So how do we get from Point A to Point B? The new stack allocator and stack array are conceptually the same as the old versions, but their interfaces have evolved a bit. The thousands of places in our source code where we’ve used the old model of StackContext and StackVector don’t match the new interface exactly, so we can’t just drop in the new implementation. There’s a lot of existing source code that needs slight changes.
You should be a little nervous about introducing new problems with the switch to the new implementation. There are likely bugs lurking somewhere in those thousands of uses of the old model. Somebody somewhere is relying on the old system’s behavior—allocating memory from the wrong stack context without realizing it, perhaps, and relying on the old behavior to keep their code working. That bit of code will break when we switch to a new model that actually supports out-of-order allocation.
An easy way to address these problems is by building parallel implementations and using a runtime flag to switch between them.
First, give the two classes different names so that they can coexist in the same codebase. In C++, wrap the StackAlloc and StackContext classes with two namespaces—say, OldStack and NewStack —so that the classes have names like NewStack::StackContext . (You could just as easily rename the classes to NewStackAlloc and OldStackAlloc .)
Second , create new StackAlloc and StackContext adapter classes. These adapter classes will delegate to either the old or the new versions of StackAlloc and StackContext , depending on a new global flag. The adapter classes support the union of the slightly different interfaces to the old and new classes.
This is pretty simple:
bool g_useNewStackAlloc = false;
class StackAlloc
{
public:
static void * alloc(int byteCount);
};
void * StackAlloc::alloc(int byteCount)
{
return (g_useNewStackAlloc) ?
NewStack::StackContext::currentContext()->alloc(byteCount) :
OldStack::StackAlloc::alloc(byteCount);
}
The StackAlloc adapter just consults the runtime flag and calls the right allocator; simple. The StackContext adapter can be even simpler—since the old StackContext didn’t have an alloc method, no code has been written to call it. Any new code calling alloc on the StackContext adapter is opting into using the new StackContext :
class StackContext
{
public:
StackContext() :
m_oldContext(),
m_newContext()
{ s_contexts.push_back(this); }
~StackContext()
{ s_contexts.pop_back(); }
void * alloc(int byteCount);
static StackContext * currentContext()
{ return (s_contexts.empty()) ? nullptr : s_contexts.back(); }
protected:
OldStack::StackContext m_oldContext;
NewStack::StackContext m_newContext;
static vector<StackContext *> s_contexts;
};
vector<StackContext *> StackContext::s_contexts;
void * StackContext::alloc(int byteCount)
{
return m_newContext.alloc(byteCount);
}
Your goal at this point is to minimize intervention in the old code path. As long as the global flag is false, the code is running through almost exactly the same logic as before the change. You’ll create old-style StackContext s as before and StackAlloc works as before, so you shouldn’t find any big issues while testing .
At this point , assuming testing works out, you could commit your work. You don’t need to update StackVector first, since it will work as is. It’s allocating stack memory like any other stack memory user, and you can switch between the old and new stack memory allocators using the runtime flag.
The ability to check in partial work to the main branch is a big advantage of the parallel rework technique. It’s not so important here, with this small example—you could easily incorporate the next couple of steps into a single change list and skip the intermediate steps. With a more realistically sized example, though, being able to migrate to a new solution over a series of partial steps makes the process much easier to pull off .
Preparing to Migrate StackVector
The next step is to decide how to migrate the StackVector class. One obvious answer is to follow the model we used for StackContext , where a new shim StackVector class embeds both an old-style and a new-style StackVector , switching between the two of them based on the global flag. That leads to delegation methods like this:
template <class ELEMENT>
size_t StackVector<ELEMENT>::size() const
{
if (g_useNewStackAlloc)
return m_oldArray.size();
else
return m_newArray.size();
}
As a temporary measure, this isn’t too bad. Creating the delegation functions is a little mind-numbing, but at least it’s obvious what’s going on to anyone who stumbles into this code during the migration to the new system.
The other option is to make the switch where the stack allocator is called. That works beautifully in this case—the StackVector class allocates stack memory in exactly one place. If that code can handle both allocating memory from a global stack (as in the original code) and from an explicit stack context (which we’re migrating to) then you’ll be in good shape:
template <class ELEMENT>
class StackVector
{
public:
StackVector();
StackVector(StackContext * context);
void reserve(int capacity);
protected:
bool m_isExplicitContext;
StackContext * m_context;
int m_count;
int m_capacity;
ELEMENT * m_elements;
};
template <class ELEMENT>
StackVector<ELEMENT>::StackVector()
: m_isExplicitContext(false),
m_context(StackContext::currentContext()),
m_count(0),
m_capacity(0),
m_elements(nullptr)
{
}
template <class ELEMENT>
StackVector<ELEMENT>::StackVector(StackContext * context)
: m_isExplicitContext(true),
m_context(context),
m_count(0),
m_capacity(0),
m_elements(nullptr)
{
}
template <class ELEMENT>
void StackVector<ELEMENT>::reserve(int capacity)
{
if (capacity <= m_capacity)
return;
assert(
m_isExplicitContext ||
m_context == StackContext::currentContext());
ELEMENT * newElements = (m_isExplicitContext) ?
m_context->allocNew<ELEMENT>(capacity) :
m_context->alloc<ELEMENT>(capacity);
for (int index = 0; index < m_count; ++index)
{
newElements[index] = move(m_elements[index]);
}
m_elements = newElements;
}
Having two StackVector constructors lets you track which kind of allocation is appropriate. All your existing code will use the first constructor, at least to start with, since it takes the same arguments as the original version of the class. You’ll eventually migrate to using the second constructor, but none of that code has been written yet. If the first constructor is used, then isExplicitContext won’t be set, and reserve will run exactly as before.
Once again, you’re at a safe point to commit changes. All existing uses of StackVector will go through the old stack allocation code path if the global flag is unset. Anyone setting the global flag runs through the new code path, as does any code that creates a stack array with the new explicit-context constructor.
Time to Migrate
Now you’re set up to migrate!
At Sucker Punch, we’d do this in a few steps. First, some small number of first penguins 3 set the global flag that switches to the new stack allocation system. If they don’t discover problems, we then recruit a larger set of people. When everything looks safe, we check in with the global flag set to true so that everyone uses the new system. If at any point during this rollout we detect a problem, it’s simple to switch everyone back to the old system while we diagnose and fix the problem.
Once everyone is safely using the new system, we can start to tear out the adapters. The new StackContext class replaces the old one without trouble. We can tear out the small bit of wiring we added to the StackVector class, too, since everything goes through the new allocator now.
There’s a policy decision to make about whether to require a context to be passed to each StackVector . It’s a trade-off between the convenience of inferring the topmost stack context and the bugs that pop up when a stack context is accidentally deleted or misplaced. If we decide to require a context, then we can do that in parts—we don’t have to update all five thousand (!) places where we create stack arrays at once.
It might seem crazy that we’d consider converting five thousand lines of code when there’s a reasonable defaulting strategy that avoids the need for any conversion...but our focus is on the long term. If requiring the context would make us more efficient by avoiding a whole class of bugs, then it’s probably worth the effort.
It’s not that hard to convert the code. Just write a little bit of Python to find all the StackVector s, figure out what stack context was implied (which isn’t that hard, because it’s nearly always the last StackContext defined), update the constructor, and check out the changed file. The real cost isn’t updating the code—it’s figuring out how to test the change.
The compiler will catch almost all the problems, but for this case I’d also apply this chapter’s strategy recursively. I’d add a special constructor for all the places where I’ve inferred a context, then assert at runtime that the constructor passed is topmost on the context stack. Once I verify that I haven’t changed contexts, I’d switch to the normal constructor. If I’ve got a good code-coverage test, I’m in great shape.
All that’s left at this point is the code that allocates stack memory directly. We’ve got two choices: continue to support a global stack allocation that implicitly uses the current stack context, or convert a few hundred lines of code to call alloc on the StackContext variable directly. In this case, I’d convert everything, gaining the robustness of the new allocation model.
Once the last bits of direct allocation are converted, you’re done. All of the vestiges of the old stack allocation are gone—and you were able to do it bit by bit, in a series of commits, taking small, safe steps. With this approach, if you run into any bumps along the way, you can quickly back up to the old behavior, so the team as a whole isn’t disrupted .
Recognizing When Parallel Rework Is a Good Strategy
This parallel rework strategy is very useful in the right circumstances, but it’s not a panacea. You’ll still run into the occasional need to fix bugs in two places. Coders who aren’t running with the new version of the system will inadvertently break your work. These things will happen less often and be less disruptive than if you’d gone on off onto your own private branch in source control, but they will happen.
Parallel rework imposes some overhead, too. Just managing three separate names for the same concept—original, reworked, and adapter—is a hassle. You’ll write more code overall, since you’re likely to make copies of some of the bits of the original solution.
It may be that your new, reworked system is so fundamentally different from the original version that parallel rework doesn’t make sense. If you can’t define an adapter layer, something that switches dynamically between the old and new versions, then you can’t apply the technique as I’ve described it here .
In a lot of cases, though, parallel rework provides a manageable way to make major changes to your codebase incrementally and safely. At Sucker Punch we don’t use it for all changes, but for big rework it’s our go-to strategy.
1 Yes, the first lesson of optimization is “don’t optimize,” as Rule 5 explains; believe me when I tell you that we have plenty of data about the importance of quick dynamic-memory allocation in our games.
2 Most importantly, the blocks only stay around as long as the context. If you want to hold onto some bit of data for longer than the context’s lifetime, you’re out of luck.
3 In case you’re not familiar with this expression, an explanation. Penguins nest on land, but hunt at sea. This means diving off ice floes into the ocean, not sure whether a hungry leopard seal is lurking underneath the waves. Penguins tend to gather in jostling mobs at the water’s edge, all waiting for one penguin to be brave enough to dive in—or, more likely, to be jostled in; there is no honor among penguins. Anyhow, if that penguin doesn’t get eaten, the rest of the penguins follow. Hence, “first penguin.”