Skip to content

Commit

Permalink
#60 preallocate right columns when using set_w_data from system
Browse files Browse the repository at this point in the history
  • Loading branch information
SanderMertens committed Sep 5, 2019
1 parent a2bb17b commit 7bcf93f
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/entity.c
Original file line number Diff line number Diff line change
Expand Up @@ -1329,7 +1329,7 @@ ecs_entity_t set_w_data_intern(
* row_count number of rows, which will give a perf boost the first time
* the entities are inserted. */
if (!entities) {
ecs_table_dim(table, count);
ecs_table_dim(table, columns, count);
entities = ecs_vector_first(columns[0].data);
ecs_assert(entities != NULL, ECS_INTERNAL_ERROR, NULL);
}
Expand Down
1 change: 1 addition & 0 deletions src/flecs_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ uint32_t ecs_table_grow(
/* Dimension array to have n rows (doesn't add entities) */
int16_t ecs_table_dim(
ecs_table_t *table,
ecs_table_column_t *columns,
uint32_t count);

/* Return number of entities in table */
Expand Down
6 changes: 5 additions & 1 deletion src/table.c
Original file line number Diff line number Diff line change
Expand Up @@ -310,9 +310,13 @@ uint32_t ecs_table_grow(

int16_t ecs_table_dim(
ecs_table_t *table,
ecs_table_column_t *columns,
uint32_t count)
{
ecs_table_column_t *columns = table->columns;
if (!columns) {
columns = table->columns;
}

uint32_t column_count = ecs_vector_count(table->type);

uint32_t size = ecs_vector_set_size(
Expand Down
2 changes: 1 addition & 1 deletion src/world.c
Original file line number Diff line number Diff line change
Expand Up @@ -869,7 +869,7 @@ void _ecs_dim_type(
if (type) {
ecs_table_t *table = ecs_world_get_table(world, &world->main_stage, type);
if (table) {
ecs_table_dim(table, entity_count);
ecs_table_dim(table, NULL, entity_count);
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion test/api/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@
"2_columns_3_rows_1_nested_prefab",
"2_columns_3_rows_component_order",
"overwrite_w_unset_column",
"overwrite_from_other_type_w_unset_column"
"overwrite_from_other_type_w_unset_column",
"staged_1_column_3_rows",
"staged_1_column_3_rows_w_entities",
"staged_1_column_3_rows_w_entities_w_base"
]
}, {
"id": "Add",
Expand Down
129 changes: 129 additions & 0 deletions test/api/src/Set_w_data.c
Original file line number Diff line number Diff line change
Expand Up @@ -878,3 +878,132 @@ void Set_w_data_overwrite_from_other_type_w_unset_column() {

ecs_fini(world);
}

static ecs_entity_t e_result;

static
void Set_w_data(ecs_rows_t *rows) {
ecs_table_data_t *data = ecs_get_context(rows->world);
ecs_entity_t e = ecs_set_w_data(rows->world, data);
test_assert(e != 0);

e_result = e;
}

void Set_w_data_staged_1_column_3_rows() {
ecs_world_t *world = ecs_init();

ECS_COMPONENT(world, Position);
ECS_SYSTEM(world, Set_w_data, EcsOnUpdate, .Position);

ecs_table_data_t data = {
.column_count = 1,
.row_count = 3,
.entities = NULL,
.components = (ecs_entity_t[]){ecs_entity(Position)},
.columns = (ecs_table_columns_t[]){
(Position[]) {
{10, 20},
{11, 21},
{12, 22}
}
}
};

ecs_set_context(world, &data);

ecs_progress(world, 1);
test_int(ecs_count(world, Position), 3);

ecs_entity_t e = e_result;

int i;
for (i = 0; i < 3; i ++) {
test_assert(ecs_has(world, e + i, Position));

Position *p = ecs_get_ptr(world, e + i, Position);
test_assert(p != NULL);
test_int(p->x, 10 + i);
test_int(p->y, 20 + i);
}

ecs_fini(world);
}

void Set_w_data_staged_1_column_3_rows_w_entities() {
ecs_world_t *world = ecs_init();

ECS_COMPONENT(world, Position);
ECS_SYSTEM(world, Set_w_data, EcsOnUpdate, .Position);

ecs_table_data_t data = {
.column_count = 1,
.row_count = 3,
.entities = (ecs_entity_t[]){5000, 5001, 5002},
.components = (ecs_entity_t[]){ecs_entity(Position)},
.columns = (ecs_table_columns_t[]){
(Position[]) {
{10, 20},
{11, 21},
{12, 22}
}
}
};

ecs_set_context(world, &data);

ecs_progress(world, 1);
test_int(ecs_count(world, Position), 3);

int i;
for (i = 0; i < 3; i ++) {
test_assert(ecs_has(world, 5000 + i, Position));

Position *p = ecs_get_ptr(world, 5000 + i, Position);
test_assert(p != NULL);
test_int(p->x, 10 + i);
test_int(p->y, 20 + i);
}

ecs_fini(world);
}

void Set_w_data_staged_1_column_3_rows_w_entities_w_base() {
ecs_world_t *world = ecs_init();

ECS_COMPONENT(world, Position);
ECS_SYSTEM(world, Set_w_data, EcsOnUpdate, .Position);
ECS_ENTITY(world, Base, 0);

ecs_table_data_t data = {
.column_count = 2,
.row_count = 3,
.entities = (ecs_entity_t[]){5000, 5001, 5002},
.components = (ecs_entity_t[]){ecs_entity(Position), ECS_INSTANCEOF | Base},
.columns = (ecs_table_columns_t[]){
(Position[]) {
{10, 20},
{11, 21},
{12, 22}
},
NULL
}
};

ecs_set_context(world, &data);

ecs_progress(world, 1);
test_int(ecs_count(world, Position), 3);

int i;
for (i = 0; i < 3; i ++) {
test_assert(ecs_has(world, 5000 + i, Position));

Position *p = ecs_get_ptr(world, 5000 + i, Position);
test_assert(p != NULL);
test_int(p->x, 10 + i);
test_int(p->y, 20 + i);
}

ecs_fini(world);
}
17 changes: 16 additions & 1 deletion test/api/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ void Set_w_data_2_columns_3_rows_1_nested_prefab(void);
void Set_w_data_2_columns_3_rows_component_order(void);
void Set_w_data_overwrite_w_unset_column(void);
void Set_w_data_overwrite_from_other_type_w_unset_column(void);
void Set_w_data_staged_1_column_3_rows(void);
void Set_w_data_staged_1_column_3_rows_w_entities(void);
void Set_w_data_staged_1_column_3_rows_w_entities_w_base(void);

// Testsuite 'Add'
void Add_zero(void);
Expand Down Expand Up @@ -882,7 +885,7 @@ static bake_test_suite suites[] = {
},
{
.id = "Set_w_data",
.testcase_count = 17,
.testcase_count = 20,
.testcases = (bake_test_case[]){
{
.id = "1_column_3_rows",
Expand Down Expand Up @@ -951,6 +954,18 @@ static bake_test_suite suites[] = {
{
.id = "overwrite_from_other_type_w_unset_column",
.function = Set_w_data_overwrite_from_other_type_w_unset_column
},
{
.id = "staged_1_column_3_rows",
.function = Set_w_data_staged_1_column_3_rows
},
{
.id = "staged_1_column_3_rows_w_entities",
.function = Set_w_data_staged_1_column_3_rows_w_entities
},
{
.id = "staged_1_column_3_rows_w_entities_w_base",
.function = Set_w_data_staged_1_column_3_rows_w_entities_w_base
}
}
},
Expand Down

0 comments on commit 7bcf93f

Please sign in to comment.