Add ArenaStlAllocator, and use arena throughout CommitRequest

This commit is contained in:
2025-08-14 13:05:05 -04:00
parent 61ae8420a8
commit 2c247fa75e
2 changed files with 87 additions and 16 deletions

View File

@@ -2,6 +2,7 @@
#include "arena_allocator.hpp"
#include <optional>
#include <span>
#include <stack>
#include <string>
#include <string_view>
@@ -61,10 +62,16 @@ public:
};
struct ParserContext {
std::stack<ParseState> state_stack;
std::string current_key;
std::string current_string;
std::string current_number;
using ArenaString = std::basic_string<char, std::char_traits<char>,
ArenaStlAllocator<char>>;
using ArenaStateStack =
std::stack<ParseState,
std::vector<ParseState, ArenaStlAllocator<ParseState>>>;
ArenaStateStack state_stack;
ArenaString current_key;
ArenaString current_string;
ArenaString current_number;
bool in_key = false;
bool parse_error = false;
bool parse_complete = false;
@@ -74,8 +81,18 @@ public:
Operation current_operation{};
// Parsing state for nested structures
std::string precondition_type;
std::string operation_type;
ArenaString precondition_type;
ArenaString operation_type;
// Constructor to initialize arena-allocated containers
explicit ParserContext(ArenaAllocator *arena)
: state_stack(ArenaStateStack::container_type(
ArenaStlAllocator<ParseState>(arena))),
current_key(ArenaStlAllocator<char>(arena)),
current_string(ArenaStlAllocator<char>(arena)),
current_number(ArenaStlAllocator<char>(arena)),
precondition_type(ArenaStlAllocator<char>(arena)),
operation_type(ArenaStlAllocator<char>(arena)) {}
};
private:
@@ -83,8 +100,8 @@ private:
std::optional<std::string_view> request_id_;
std::string_view leader_id_;
uint64_t read_version_ = 0;
std::vector<Precondition> preconditions_;
std::vector<Operation> operations_;
std::vector<Precondition, ArenaStlAllocator<Precondition>> preconditions_;
std::vector<Operation, ArenaStlAllocator<Operation>> operations_;
ParserContext parser_context_;
WeaselJsonParser *json_parser_ = nullptr;
@@ -93,7 +110,11 @@ public:
* @brief Construct a new CommitRequest with the given initial arena size.
* @param arena_size Initial size for the arena allocator
*/
explicit CommitRequest(size_t arena_size = 4096) : arena_(arena_size) {}
explicit CommitRequest(size_t arena_size = 4096)
: arena_(arena_size),
preconditions_(ArenaStlAllocator<Precondition>(&arena_)),
operations_(ArenaStlAllocator<Operation>(&arena_)),
parser_context_(&arena_) {}
/**
* @brief Destructor - cleans up any active parser.
@@ -203,17 +224,15 @@ public:
/**
* @brief Get the preconditions.
* @return Vector of preconditions
* @return span of preconditions
*/
const std::vector<Precondition> &preconditions() const {
return preconditions_;
}
std::span<const Precondition> preconditions() const { return preconditions_; }
/**
* @brief Get the operations.
* @return Vector of operations
* @return span of operations
*/
const std::vector<Operation> &operations() const { return operations_; }
std::span<const Operation> operations() const { return operations_; }
/**
* @brief Get the total allocated bytes in the arena.
@@ -243,7 +262,7 @@ public:
WeaselJsonParser_destroy(json_parser_);
json_parser_ = nullptr;
}
parser_context_ = ParserContext{};
parser_context_ = ParserContext(&arena_);
parser_context_.state_stack.push(ParseState::Root);
}