Skip to content

Commit

Permalink
Reorder fields on OperandStack
Browse files Browse the repository at this point in the history
  • Loading branch information
chfast committed Sep 16, 2020
1 parent cfad111 commit 8a4e20d
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions lib/fizzy/stack.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,21 @@ class OperandStack
/// The size of the pre-allocated internal storage: 128 bytes.
static constexpr auto small_storage_size = 128 / sizeof(Value);

/// The pointer to the top item, or below the stack bottom if stack is empty.
/// The pointer to the top item of the operand stack,
/// or below the stack bottom if stack is empty.
///
/// This pointer always alias m_storage, but it is kept as the first field
/// This pointer always alias m_locals, but it is kept as the first field
/// because it is accessed the most. Therefore, it must be initialized
/// in the constructor after the m_storage.
/// in the constructor after the m_locals.
Value* m_top;

Value* m_bottom;

/// The pointer to the beginning of the locals array.
/// This always points to one of the storages.
Value* m_locals;

/// The pointer to the bottom of the operand stack.
Value* m_bottom;

/// The pre-allocated internal storage.
Value m_small_storage[small_storage_size];

Expand All @@ -97,18 +101,16 @@ class OperandStack
const auto num_args = args.size();
const auto storage_size_required = num_args + num_local_variables + max_stack_height;

Value* storage;
if (storage_size_required <= small_storage_size)
{
storage = &m_small_storage[0];
m_locals = &m_small_storage[0];
}
else
{
m_large_storage = std::make_unique<Value[]>(storage_size_required);
storage = &m_large_storage[0];
m_locals = &m_large_storage[0];
}

m_locals = storage;
m_bottom = m_locals + num_args + num_local_variables;
m_top = m_bottom - 1;

Expand Down

0 comments on commit 8a4e20d

Please sign in to comment.