When implementing algorithms that depend on “current state” (client context, cached slot/client, etc.) or on custom data structures (stack/heap modes), make the invariant explicit and ensure all computations are derived from the specific operand/object being processed—not from incidental outer execution context.
Practical rules:
Example (stack-embedded “small vector” idea):
// SmallVector-like: small fixed buffer embedded in the DS.
typedef struct vec {
size_t size;
size_t cap;
void **data;
void *small[VEC_DEFAULT_INITCAP];
} vec;
void vecInit(vec *v, size_t hint) {
v->size = 0;
if (hint <= VEC_DEFAULT_INITCAP) {
v->data = v->small;
v->cap = VEC_DEFAULT_INITCAP;
} else {
v->data = zmalloc(sizeof(void*) * hint);
v->cap = hint;
}
}
void vecDestroy(vec *v) {
if (v->data != v->small) zfree(v->data);
}
Apply this mindset to both algorithmic routing decisions and DS implementations: correctness comes from invariants tied to the actual data being acted upon, and API/structure choices that prevent accidental reliance on stale execution-context or ambiguous ownership.
Enter the URL of a public GitHub repository