Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve assert performance #1061

Merged
merged 2 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions include/flecs/addons/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,7 @@ void ecs_abort_(
...);

FLECS_API
bool ecs_assert_(
bool condition,
void ecs_assert_log_(
int32_t error_code,
const char *condition_str,
const char *file,
Expand Down Expand Up @@ -350,7 +349,8 @@ void ecs_parser_errorv_(
#define ecs_assert(condition, error_code, ...)
#else
#define ecs_assert(condition, error_code, ...)\
if (!ecs_assert_(condition, error_code, #condition, __FILE__, __LINE__, __VA_ARGS__)) {\
if (!(condition)) {\
ecs_assert_log_(error_code, #condition, __FILE__, __LINE__, __VA_ARGS__);\
ecs_os_abort();\
}\
assert(condition) /* satisfy compiler/static analyzers */
Expand Down Expand Up @@ -390,7 +390,8 @@ void ecs_parser_errorv_(
#else
#ifdef FLECS_SOFT_ASSERT
#define ecs_check(condition, error_code, ...)\
if (!ecs_assert_(condition, error_code, #condition, __FILE__, __LINE__, __VA_ARGS__)) {\
if (!(condition)) {\
ecs_assert_log_(error_code, #condition, __FILE__, __LINE__, __VA_ARGS__);\
goto error;\
}
#else // FLECS_SOFT_ASSERT
Expand Down
36 changes: 14 additions & 22 deletions src/addons/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -326,32 +326,27 @@ void ecs_abort_(
ecs_os_api.log_last_error_ = err;
}

bool ecs_assert_(
bool condition,
void ecs_assert_log_(
int32_t err,
const char *cond_str,
const char *file,
int32_t line,
const char *fmt,
...)
{
if (!condition) {
if (fmt) {
va_list args;
va_start(args, fmt);
char *msg = ecs_vasprintf(fmt, args);
va_end(args);
ecs_fatal_(file, line, "assert: %s %s (%s)",
cond_str, msg, ecs_strerror(err));
ecs_os_free(msg);
} else {
ecs_fatal_(file, line, "assert: %s %s",
cond_str, ecs_strerror(err));
}
ecs_os_api.log_last_error_ = err;
if (fmt) {
va_list args;
va_start(args, fmt);
char *msg = ecs_vasprintf(fmt, args);
va_end(args);
ecs_fatal_(file, line, "assert: %s %s (%s)",
cond_str, msg, ecs_strerror(err));
ecs_os_free(msg);
} else {
ecs_fatal_(file, line, "assert: %s %s",
cond_str, ecs_strerror(err));
}

return condition;
ecs_os_api.log_last_error_ = err;
}

void ecs_deprecated_(
Expand Down Expand Up @@ -484,22 +479,19 @@ void ecs_abort_(
(void)fmt;
}

bool ecs_assert_(
bool condition,
void ecs_assert_log_(
int32_t error_code,
const char *condition_str,
const char *file,
int32_t line,
const char *fmt,
...)
{
(void)condition;
(void)error_code;
(void)condition_str;
(void)file;
(void)line;
(void)fmt;
return true;
}

#endif
Expand Down
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
21 changes: 0 additions & 21 deletions src/poly.c
Original file line number Diff line number Diff line change
Expand Up @@ -187,27 +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);\
assert(cond)

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