Use State Pooling for high performance
mainFor scenarios requiring frequent Lua script execution, use state pooling to avoid the overhead of repeated memory allocation and library loading. RunPooled() automatically acquires a state from the pool and returns it when finished.
Predefined Pool Colors
| Color | Libraries | Use Case |
|---|---|---|
"default" | All standard | General scripting |
"sandboxed" | base, math, string, table | Safe execution |
"minimal" | base only | Maximum performance |
"io" | base, io, os | File operations |
Custom Pools
You can define your own pool configuration using PoolConfig:
PoolConfig config;
config.libraries = {"base", "math"};
config.maxSize = 3;
ctx.createPool("math_only", config);
ctx.RunPooled("calculation", "math_only");#include <LuaCpp.hpp>
#include <iostream>
using namespace LuaCpp;
int main(int argc, char **argv) {
LuaContext ctx;
// Compile a script
ctx.CompileString("hello", "print('Hello from pooled state!')");
// Execute using a pooled state
try {
ctx.RunPooled("hello");
}
catch (std::runtime_error& e) {
std::cout << e.what() << '\n';
}
}