Skip to content

Commit

Permalink
Improve poly assert performance
Browse files Browse the repository at this point in the history
  • Loading branch information
jbarthelmes committed Oct 12, 2023
1 parent e4f2ecc commit ab4a143
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 39 deletions.
11 changes: 6 additions & 5 deletions src/addons/pipeline/pipeline.c
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,8 @@ bool flecs_pipeline_build(

int32_t i;
for (i = 0; i < it.count; i ++) {
ecs_system_t *sys = ecs_poly(poly[i].poly, ecs_system_t);
ecs_poly_assert(poly[i].poly, ecs_system_t);
ecs_system_t *sys = (ecs_system_t*)poly[i].poly;
ecs_query_t *q = sys->query;

bool needs_merge = false;
Expand Down Expand Up @@ -391,8 +392,8 @@ bool flecs_pipeline_build(
for (i = 0; i < count; i ++) {
ecs_entity_t system = systems[i];
const EcsPoly *poly = ecs_get_pair(world, system, EcsPoly, EcsSystem);
ecs_assert(poly != NULL, ECS_INTERNAL_ERROR, NULL);
ecs_system_t *sys = ecs_poly(poly->poly, ecs_system_t);
ecs_poly_assert(poly->poly, ecs_system_t);
ecs_system_t *sys = (ecs_system_t*)poly->poly;

#ifdef FLECS_LOG_1
char *path = ecs_get_fullpath(world, system);
Expand Down Expand Up @@ -550,8 +551,8 @@ int32_t flecs_run_pipeline_ops(
for (; i < count; i++) {
ecs_entity_t system = systems[i];
const EcsPoly* poly = ecs_get_pair(world, system, EcsPoly, EcsSystem);
ecs_assert(poly != NULL, ECS_INTERNAL_ERROR, NULL);
ecs_system_t* sys = ecs_poly(poly->poly, ecs_system_t);
ecs_poly_assert(poly->poly, ecs_system_t);
ecs_system_t* sys = (ecs_system_t*)poly->poly;

/* Keep track of the last frame for which the system has ran, so we
* know from where to resume the schedule in case the schedule
Expand Down
3 changes: 2 additions & 1 deletion src/addons/system/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,8 @@ ecs_entity_t ecs_system_init(

ecs_defer_end(world);
} else {
ecs_system_t *system = ecs_poly(poly->poly, ecs_system_t);
ecs_poly_assert(poly->poly, ecs_system_t);
ecs_system_t *system = (ecs_system_t*)poly->poly;

if (desc->run) {
system->run = desc->run;
Expand Down
3 changes: 2 additions & 1 deletion src/observer.c
Original file line number Diff line number Diff line change
Expand Up @@ -928,7 +928,8 @@ ecs_entity_t ecs_observer_init(
ecs_get_name(world, entity));
}
} else {
ecs_observer_t *observer = ecs_poly(poly->poly, ecs_observer_t);
ecs_poly_assert(poly->poly, ecs_observer_t);
ecs_observer_t *observer = (ecs_observer_t*)poly->poly;

if (desc->run) {
observer->run = desc->run;
Expand Down
20 changes: 0 additions & 20 deletions src/poly.c
Original file line number Diff line number Diff line change
Expand Up @@ -187,26 +187,6 @@ ecs_poly_t* ecs_poly_get_(
return NULL;
}

#ifndef FLECS_NDEBUG
#define assert_object(cond, file, line, type_name)\
ecs_assert((cond), ECS_INVALID_PARAMETER, #cond, file, line, type_name)

void* ecs_poly_assert_(
const ecs_poly_t *poly,
int32_t type,
const char *file,
int32_t line)
{
assert_object(poly != NULL, file, line, 0);

const ecs_header_t *hdr = poly;
const char *type_name = hdr->mixins->type_name;
assert_object(hdr->magic == ECS_OBJECT_MAGIC, file, line, type_name);
assert_object(hdr->type == type, file, line, type_name);
return ECS_CONST_CAST(ecs_poly_t*, poly);
}
#endif

bool ecs_poly_is_(
const ecs_poly_t *poly,
int32_t type)
Expand Down
21 changes: 9 additions & 12 deletions src/poly.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,16 @@ ecs_poly_t* ecs_poly_get_(

/* Utilities for testing/asserting an object type */
#ifndef FLECS_NDEBUG
void* ecs_poly_assert_(
const ecs_poly_t *object,
int32_t type,
const char *file,
int32_t line);

#define ecs_poly_assert(object, type)\
ecs_poly_assert_(object, type##_magic, __FILE__, __LINE__)

#define ecs_poly(object, T) ((T*)ecs_poly_assert(object, T))
#define ecs_poly_assert(object, ty)\
do {\
ecs_assert(object != NULL, ECS_INVALID_PARAMETER, NULL);\
const ecs_header_t *hdr = (const ecs_header_t *)object;\
const char *type_name = hdr->mixins->type_name;\
ecs_assert(hdr->magic == ECS_OBJECT_MAGIC, ECS_INVALID_PARAMETER, type_name);\
ecs_assert(hdr->type == ty##_magic, ECS_INVALID_PARAMETER, type_name);\
} while (0)
#else
#define ecs_poly_assert(object, type)
#define ecs_poly(object, T) ((T*)object)
#define ecs_poly_assert(object, ty)
#endif

/* Utility functions for getting a mixin from an object */
Expand Down

0 comments on commit ab4a143

Please sign in to comment.