Why do your `optional` and `expected` allocate on the heap?!
Ok, look, The decision to allocate most objects on the heap rather than the stack in Nodepp is driven by two primary reasons, both crucial for handling asynchronous operations and efficient resource management in Nodepp.
1. Managing Asynchronous Context and Object Lifecycles
In asynchronous programming, where operations don't complete immediately and functions might execute at a later time (like a network request or a timer), it's very common to lose the context of objects if they were allocated on the stack.
Let me illustrate with an example:
#include <nodepp/nodepp.h>
#include <nodepp/timer.h>
using namespace nodepp;
void onMain() {
uint number = 100; // 'number' is on the stack
timer::timeout([&](){ // Capture 'number' by reference
console::log( number );
// This will lead to a segmentation fault!
// By the time this lambda executes, 'number' has
// likely gone out of scope and its memory reused.
}, 1000 ); // 1 second
}
In this scenario, number is a local variable on the stack of onMain. When onMain finishes executing (which happens immediately), number goes out of scope. When the timer::timeout lambda tries to access number a second later, it's trying to access invalid memory, leading to a crash.
One way to seemingly solve this is by copying the object into the lambda's closure:
#include <nodepp/nodepp.h>
#include <nodepp/timer.h>
using namespace nodepp;
void onMain() {
uint number = 100;
timer::timeout([=](){ // Capture 'number' by value (copy)
console::log( number ); // This will now work
}, 1000 );
}
While this works for reading a value, it introduces two problems:
- Copying large objects: If number were a complex object (like an HTTP request context), copying it would be inefficient and time-consuming.
- Immutability: What if you need to modify the object and have those changes reflected elsewhere?
#include <nodepp/nodepp.h>
#include <nodepp/timer.h>
using namespace nodepp;
void onMain() {
uint number = 100;
timer::timeout([=](){ // Captures a copy of 'number'
number += 100; // Modifies the *copy*, not the original 'number'
}, 1000 );
timer::timeout([=](){ // Captures another copy of the *original* 'number' (which is still 100)
console::log( number ); // This will still print 100
}, 2000 );
}
As you can see, each lambda gets its own copy, so modifications aren't shared. This is where smart pointers come in. By allocating objects on the heap and managing them with smart pointers, Nodepp can ensure objects persist as long as they are needed and can be safely shared and modified across different asynchronous operations.
#include <nodepp/nodepp.h>
#include <nodepp/timer.h>
using namespace nodepp;
void onMain() {
// 'number' is now on the heap, managed by a smart pointer
ptr_t<uint> number ( new uint(100) ); // OR
// ptr_t<uint> number = type::bind( new uint(100) );
timer::timeout([=](){ // Captures the smart pointer by value (a cheap copy of the pointer)
*number += 100; // Modifies the object on the heap
}, 1000 );
timer::timeout([=](){ // Captures another copy of the smart pointer
console::log( *number ); // Accesses the modified object on the heap, prints 200
}, 2000 );
}
The smart pointer ptr_t manages the lifetime of the uint on the heap. When the last smart pointer referencing that uint goes out of scope, the memory is automatically deallocated. This completely resolves the context loss and modification issues inherent with stack allocation in asynchronous environments.
2. Efficiency with Large and Shared Objects
The second reason is about efficiency when dealing with large or frequently shared objects. Imagine you have a complex HTTP context object. If you were to copy this entire object every time it needed to be passed to an asynchronous callback or shared between different parts of your application, it would be incredibly inefficient in terms of both memory and CPU time.
By using smart pointers to manage objects on the heap, Nodepp can simply pass around a small pointer instead of making expensive copies of the entire object. Multiple parts of your application can then share and interact with the same instance of that large object, and then, Nodepp automatically handles the deletion of the object (e.g., closing a socket and freeing memory for that HTTP object) once it's no longer referenced by any other async task.