Skip to content

Commit

Permalink
Reduce branching in command batching code
Browse files Browse the repository at this point in the history
  • Loading branch information
SanderMertens committed Nov 14, 2023
1 parent 5b504ed commit d99efe2
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 244 deletions.
193 changes: 73 additions & 120 deletions flecs.c
Original file line number Diff line number Diff line change
Expand Up @@ -8154,6 +8154,7 @@ bool flecs_defer_end(
ecs_stage_t *dst_stage = flecs_stage_from_world(&world);
ecs_commands_t *commands = stage->cmd;
ecs_vec_t *queue = &commands->queue;

if (ecs_vec_count(queue)) {
ecs_cmd_t *cmds = ecs_vec_first(queue);
int32_t i, count = ecs_vec_count(queue);
Expand Down Expand Up @@ -20479,72 +20480,54 @@ int32_t flecs_relation_depth(


static
ecs_cmd_t* flecs_cmd_alloc(
ecs_cmd_t* flecs_cmd_new(
ecs_stage_t *stage)
{
ecs_cmd_t *cmd = ecs_vec_append_t(&stage->allocator, &stage->cmd->queue,
ecs_cmd_t);
ecs_os_zeromem(cmd);
cmd->is._1.value = NULL;
cmd->next_for_entity = 0;
cmd->entry = NULL;
return cmd;
}

static
ecs_cmd_t* flecs_cmd_new(
ecs_cmd_t* flecs_cmd_new_batched(
ecs_stage_t *stage,
ecs_entity_t e,
bool is_delete,
bool can_batch)
ecs_entity_t e)
{
if (e) {
ecs_vec_t *cmds = &stage->cmd->queue;
ecs_cmd_entry_t *first_entry = NULL;
ecs_cmd_entry_t *entry = flecs_sparse_try_t(
&stage->cmd->entries, ecs_cmd_entry_t, e);
ecs_vec_t *cmds = &stage->cmd->queue;
ecs_cmd_entry_t *entry = flecs_sparse_get_any_t(
&stage->cmd->entries, ecs_cmd_entry_t, e);

int32_t cur = ecs_vec_count(cmds);
if (entry) {
if (entry->first == -1) {
/* Existing but invalidated entry */
entry->first = cur;
first_entry = entry;
} else {
int32_t last = entry->last;
if (entry->last == -1) {
/* Entity was deleted, don't insert command */
return NULL;
}

if (can_batch) {
ecs_cmd_t *arr = ecs_vec_first_t(cmds, ecs_cmd_t);
ecs_assert(arr[last].entity == e, ECS_INTERNAL_ERROR, NULL);
ecs_cmd_t *last_op = &arr[last];
last_op->next_for_entity = cur;
if (last == entry->first) {
/* Flip sign bit so flush logic can tell which command
* is the first for an entity */
last_op->next_for_entity *= -1;
}
}
}
} else if (can_batch || is_delete) {
first_entry = entry = flecs_sparse_ensure_fast_t(
&stage->cmd->entries, ecs_cmd_entry_t, e);
int32_t cur = ecs_vec_count(cmds);
ecs_cmd_t *cmd = flecs_cmd_new(stage);
if (entry) {
if (entry->first == -1) {
/* Existing but invalidated entry */
entry->first = cur;
cmd->entry = entry;
} else {
int32_t last = entry->last;
ecs_cmd_t *arr = ecs_vec_first_t(cmds, ecs_cmd_t);
ecs_assert(arr[last].entity == e, ECS_INTERNAL_ERROR, NULL);
ecs_cmd_t *last_op = &arr[last];
last_op->next_for_entity = cur;
if (last == entry->first) {
/* Flip sign bit so flush logic can tell which command
* is the first for an entity */
last_op->next_for_entity *= -1;
}
}
if (can_batch) {
entry->last = cur;
}
if (is_delete) {
/* Prevent insertion of more commands for entity */
entry->last = -1;
}

ecs_cmd_t *cmd = flecs_cmd_alloc(stage);
cmd->entry = first_entry;
return cmd;
} else {
cmd->entry = entry = flecs_sparse_ensure_fast_t(
&stage->cmd->entries, ecs_cmd_entry_t, e);
entry->first = cur;
}

return flecs_cmd_alloc(stage);
entry->last = cur;

return cmd;
}

static
Expand Down Expand Up @@ -20646,7 +20629,7 @@ bool flecs_defer_modified(
ecs_id_t id)
{
if (flecs_defer_cmd(stage)) {
ecs_cmd_t *cmd = flecs_cmd_new(stage, entity, false, true);
ecs_cmd_t *cmd = flecs_cmd_new_batched(stage, entity);
if (cmd) {
cmd->kind = EcsCmdModified;
cmd->id = id;
Expand All @@ -20664,13 +20647,11 @@ bool flecs_defer_clone(
bool clone_value)
{
if (flecs_defer_cmd(stage)) {
ecs_cmd_t *cmd = flecs_cmd_new(stage, entity, false, false);
if (cmd) {
cmd->kind = EcsCmdClone;
cmd->id = src;
cmd->entity = entity;
cmd->is._1.clone_value = clone_value;
}
ecs_cmd_t *cmd = flecs_cmd_new(stage);
cmd->kind = EcsCmdClone;
cmd->id = src;
cmd->entity = entity;
cmd->is._1.clone_value = clone_value;
return true;
}
return false;
Expand All @@ -20683,13 +20664,11 @@ bool flecs_defer_path(
const char *name)
{
if (stage->defer > 0) {
ecs_cmd_t *cmd = flecs_cmd_new(stage, entity, false, false);
if (cmd) {
cmd->kind = EcsCmdPath;
cmd->entity = entity;
cmd->id = parent;
cmd->is._1.value = ecs_os_strdup(name);
}
ecs_cmd_t *cmd = flecs_cmd_new(stage);
cmd->kind = EcsCmdPath;
cmd->entity = entity;
cmd->id = parent;
cmd->is._1.value = ecs_os_strdup(name);
return true;
}
return false;
Expand All @@ -20700,11 +20679,9 @@ bool flecs_defer_delete(
ecs_entity_t entity)
{
if (flecs_defer_cmd(stage)) {
ecs_cmd_t *cmd = flecs_cmd_new(stage, entity, true, false);
if (cmd) {
cmd->kind = EcsCmdDelete;
cmd->entity = entity;
}
ecs_cmd_t *cmd = flecs_cmd_new(stage);
cmd->kind = EcsCmdDelete;
cmd->entity = entity;
return true;
}
return false;
Expand All @@ -20715,11 +20692,9 @@ bool flecs_defer_clear(
ecs_entity_t entity)
{
if (flecs_defer_cmd(stage)) {
ecs_cmd_t *cmd = flecs_cmd_new(stage, entity, false, true);
if (cmd) {
cmd->kind = EcsCmdClear;
cmd->entity = entity;
}
ecs_cmd_t *cmd = flecs_cmd_new_batched(stage, entity);
cmd->kind = EcsCmdClear;
cmd->entity = entity;
return true;
}
return false;
Expand All @@ -20731,7 +20706,7 @@ bool flecs_defer_on_delete_action(
ecs_entity_t action)
{
if (flecs_defer_cmd(stage)) {
ecs_cmd_t *cmd = flecs_cmd_alloc(stage);
ecs_cmd_t *cmd = flecs_cmd_new(stage);
cmd->kind = EcsCmdOnDeleteAction;
cmd->id = id;
cmd->entity = action;
Expand All @@ -20747,12 +20722,10 @@ bool flecs_defer_enable(
bool enable)
{
if (flecs_defer_cmd(stage)) {
ecs_cmd_t *cmd = flecs_cmd_new(stage, entity, false, false);
if (cmd) {
cmd->kind = enable ? EcsCmdEnable : EcsCmdDisable;
cmd->entity = entity;
cmd->id = id;
}
ecs_cmd_t *cmd = flecs_cmd_new(stage);
cmd->kind = enable ? EcsCmdEnable : EcsCmdDisable;
cmd->entity = entity;
cmd->id = id;
return true;
}
return false;
Expand All @@ -20777,14 +20750,12 @@ bool flecs_defer_bulk_new(
*ids_out = ids;

/* Store data in op */
ecs_cmd_t *cmd = flecs_cmd_alloc(stage);
if (cmd) {
cmd->kind = EcsCmdBulkNew;
cmd->id = id;
cmd->is._n.entities = ids;
cmd->is._n.count = count;
}

ecs_cmd_t *cmd = flecs_cmd_new(stage);
cmd->kind = EcsCmdBulkNew;
cmd->id = id;
cmd->is._n.entities = ids;
cmd->is._n.count = count;
cmd->entity = 0;
return true;
}
return false;
Expand All @@ -20797,12 +20768,10 @@ bool flecs_defer_add(
{
if (flecs_defer_cmd(stage)) {
ecs_assert(id != 0, ECS_INTERNAL_ERROR, NULL);
ecs_cmd_t *cmd = flecs_cmd_new(stage, entity, false, true);
if (cmd) {
cmd->kind = EcsCmdAdd;
cmd->id = id;
cmd->entity = entity;
}
ecs_cmd_t *cmd = flecs_cmd_new_batched(stage, entity);
cmd->kind = EcsCmdAdd;
cmd->id = id;
cmd->entity = entity;
return true;
}
return false;
Expand All @@ -20815,12 +20784,10 @@ bool flecs_defer_remove(
{
if (flecs_defer_cmd(stage)) {
ecs_assert(id != 0, ECS_INTERNAL_ERROR, NULL);
ecs_cmd_t *cmd = flecs_cmd_new(stage, entity, false, true);
if (cmd) {
cmd->kind = EcsCmdRemove;
cmd->id = id;
cmd->entity = entity;
}
ecs_cmd_t *cmd = flecs_cmd_new_batched(stage, entity);
cmd->kind = EcsCmdRemove;
cmd->id = id;
cmd->entity = entity;
return true;
}
return false;
Expand All @@ -20836,17 +20803,7 @@ void* flecs_defer_set(
void *value,
bool need_value)
{
ecs_cmd_t *cmd = flecs_cmd_new(stage, entity, false, true);
if (!cmd) {
if (need_value) {
/* Entity is deleted by a previous command, but we still need to
* return a temporary storage to the application. */
cmd_kind = EcsCmdSkip;
} else {
/* No value needs to be returned, we can drop the command */
return NULL;
}
}
ecs_cmd_t *cmd = flecs_cmd_new_batched(stage, entity);

/* Find type info for id */
const ecs_type_info_t *ti = NULL;
Expand Down Expand Up @@ -20977,7 +20934,7 @@ void* flecs_defer_set(
* already deleted. */
return cmd_value;
}
cmd = flecs_cmd_alloc(stage);
cmd = flecs_cmd_new(stage);
}

if (!existing) {
Expand Down Expand Up @@ -21011,11 +20968,7 @@ void flecs_enqueue(
ecs_stage_t *stage,
ecs_event_desc_t *desc)
{
ecs_cmd_t *cmd = flecs_cmd_new(stage, desc->entity, false, false);
if (!cmd) {
return; /* Entity was deleted */
}

ecs_cmd_t *cmd = flecs_cmd_new(stage);
cmd->kind = EcsCmdEvent;
cmd->entity = desc->entity;

Expand Down
1 change: 1 addition & 0 deletions src/entity.c
Original file line number Diff line number Diff line change
Expand Up @@ -4768,6 +4768,7 @@ bool flecs_defer_end(
ecs_stage_t *dst_stage = flecs_stage_from_world(&world);
ecs_commands_t *commands = stage->cmd;
ecs_vec_t *queue = &commands->queue;

if (ecs_vec_count(queue)) {
ecs_cmd_t *cmds = ecs_vec_first(queue);
int32_t i, count = ecs_vec_count(queue);
Expand Down
Loading

0 comments on commit d99efe2

Please sign in to comment.