Skip to content

Commit bb5d296

Browse files
committed
Add always_inline annotation to performance critical functions
1 parent 5e7ec48 commit bb5d296

File tree

8 files changed

+122
-86
lines changed

8 files changed

+122
-86
lines changed

distr/flecs.c

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2948,6 +2948,82 @@ uint32_t flecs_get_table_version(
29482948

29492949
#endif
29502950

2951+
/**
2952+
* @file addons/journal.h
2953+
* @brief Journaling addon that logs API functions.
2954+
*
2955+
* The journaling addon traces API calls. The trace is formatted as runnable
2956+
* C code, which allows for (partially) reproducing the behavior of an app
2957+
* with the journaling trace.
2958+
*
2959+
* The journaling addon is disabled by default. Enabling it can have a
2960+
* significant impact on performance.
2961+
*/
2962+
2963+
#ifdef FLECS_JOURNAL
2964+
2965+
#ifndef FLECS_LOG
2966+
#define FLECS_LOG
2967+
#endif
2968+
2969+
#ifndef FLECS_JOURNAL_H
2970+
#define FLECS_JOURNAL_H
2971+
2972+
/**
2973+
* @defgroup c_addons_journal Journal
2974+
* @ingroup c_addons
2975+
* Journaling addon (disabled by default).
2976+
*
2977+
*
2978+
* @{
2979+
*/
2980+
2981+
/* Trace when log level is at or higher than level */
2982+
#define FLECS_JOURNAL_LOG_LEVEL (0)
2983+
2984+
#ifdef __cplusplus
2985+
extern "C" {
2986+
#endif
2987+
2988+
/* Journaling API, meant to be used by internals. */
2989+
2990+
typedef enum ecs_journal_kind_t {
2991+
EcsJournalNew,
2992+
EcsJournalMove,
2993+
EcsJournalClear,
2994+
EcsJournalDelete,
2995+
EcsJournalDeleteWith,
2996+
EcsJournalRemoveAll,
2997+
EcsJournalTableEvents
2998+
} ecs_journal_kind_t;
2999+
3000+
FLECS_DBG_API
3001+
void flecs_journal_begin(
3002+
ecs_world_t *world,
3003+
ecs_journal_kind_t kind,
3004+
ecs_entity_t entity,
3005+
ecs_type_t *add,
3006+
ecs_type_t *remove);
3007+
3008+
FLECS_DBG_API
3009+
void flecs_journal_end(void);
3010+
3011+
#define flecs_journal(...)\
3012+
flecs_journal_begin(__VA_ARGS__);\
3013+
flecs_journal_end();
3014+
3015+
#ifdef __cplusplus
3016+
}
3017+
#endif // __cplusplus
3018+
/** @} */
3019+
#endif // FLECS_JOURNAL_H
3020+
#else
3021+
#define flecs_journal_begin(...)
3022+
#define flecs_journal_end(...)
3023+
#define flecs_journal(...)
3024+
3025+
#endif // FLECS_JOURNAL
3026+
29513027
/**
29523028
* @file datastructures/name_index.h
29533029
* @brief Data structure for resolving 64bit keys by string (name).

distr/flecs.h

Lines changed: 22 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,12 @@
173173
*/
174174
// #define FLECS_CPP_NO_ENUM_REFLECTION
175175

176+
/** @def FLECS_NO_ALWAYS_INLINE
177+
* When set, this will prevent functions from being annotated with always_inline
178+
* which can improve performance at the cost of increased binary footprint.
179+
*/
180+
// #define FLECS_NO_ALWAYS_INLINE
181+
176182
/** @def FLECS_CUSTOM_BUILD
177183
* This macro lets you customize which addons to build flecs with.
178184
* Without any addons Flecs is just a minimal ECS storage, but addons add
@@ -883,6 +889,18 @@ typedef struct ecs_allocator_t ecs_allocator_t;
883889
#define ECS_ALIGNOF(T) ((int64_t)&((struct { char c; T d; } *)0)->d)
884890
#endif
885891

892+
#ifndef FLECS_NO_ALWAYS_INLINE
893+
#if defined(ECS_TARGET_CLANG) || defined(ECS_TARGET_GCC)
894+
#define FLECS_ALWAYS_INLINE __attribute__((always_inline))
895+
#elif defined(ECS_TARGET_MSVC)
896+
#define FLECS_ALWAYS_INLINE __forceinline
897+
#else
898+
#define FLECS_ALWAYS_INLINE
899+
#endif
900+
#else
901+
#define FLECS_ALWAYS_INLINE
902+
#endif
903+
886904
#ifndef FLECS_NO_DEPRECATED_WARNINGS
887905
#if defined(ECS_TARGET_GNU)
888906
#define ECS_DEPRECATED(msg) __attribute__((deprecated(msg)))
@@ -6523,7 +6541,7 @@ bool ecs_is_enabled_id(
65236541
* @see ecs_get_mut_id()
65246542
*/
65256543
FLECS_API
6526-
const void* ecs_get_id(
6544+
FLECS_ALWAYS_INLINE const void* ecs_get_id(
65276545
const ecs_world_t *world,
65286546
ecs_entity_t entity,
65296547
ecs_id_t id);
@@ -6540,7 +6558,7 @@ const void* ecs_get_id(
65406558
* @return The component pointer, NULL if the entity does not have the component.
65416559
*/
65426560
FLECS_API
6543-
void* ecs_get_mut_id(
6561+
FLECS_ALWAYS_INLINE void* ecs_get_mut_id(
65446562
const ecs_world_t *world,
65456563
ecs_entity_t entity,
65466564
ecs_id_t id);
@@ -7111,7 +7129,7 @@ char* ecs_entity_str(
71117129
* @see ecs_owns_id()
71127130
*/
71137131
FLECS_API
7114-
bool ecs_has_id(
7132+
FLECS_ALWAYS_INLINE bool ecs_has_id(
71157133
const ecs_world_t *world,
71167134
ecs_entity_t entity,
71177135
ecs_id_t id);
@@ -7127,7 +7145,7 @@ bool ecs_has_id(
71277145
* @return True if the entity has the id, false if not.
71287146
*/
71297147
FLECS_API
7130-
bool ecs_owns_id(
7148+
FLECS_ALWAYS_INLINE bool ecs_owns_id(
71317149
const ecs_world_t *world,
71327150
ecs_entity_t entity,
71337151
ecs_id_t id);
@@ -10665,82 +10683,6 @@ int ecs_value_move_ctor(
1066510683
#endif
1066610684

1066710685
/* Always included, if disabled functions are replaced with dummy macros */
10668-
/**
10669-
* @file addons/journal.h
10670-
* @brief Journaling addon that logs API functions.
10671-
*
10672-
* The journaling addon traces API calls. The trace is formatted as runnable
10673-
* C code, which allows for (partially) reproducing the behavior of an app
10674-
* with the journaling trace.
10675-
*
10676-
* The journaling addon is disabled by default. Enabling it can have a
10677-
* significant impact on performance.
10678-
*/
10679-
10680-
#ifdef FLECS_JOURNAL
10681-
10682-
#ifndef FLECS_LOG
10683-
#define FLECS_LOG
10684-
#endif
10685-
10686-
#ifndef FLECS_JOURNAL_H
10687-
#define FLECS_JOURNAL_H
10688-
10689-
/**
10690-
* @defgroup c_addons_journal Journal
10691-
* @ingroup c_addons
10692-
* Journaling addon (disabled by default).
10693-
*
10694-
*
10695-
* @{
10696-
*/
10697-
10698-
/* Trace when log level is at or higher than level */
10699-
#define FLECS_JOURNAL_LOG_LEVEL (0)
10700-
10701-
#ifdef __cplusplus
10702-
extern "C" {
10703-
#endif
10704-
10705-
/* Journaling API, meant to be used by internals. */
10706-
10707-
typedef enum ecs_journal_kind_t {
10708-
EcsJournalNew,
10709-
EcsJournalMove,
10710-
EcsJournalClear,
10711-
EcsJournalDelete,
10712-
EcsJournalDeleteWith,
10713-
EcsJournalRemoveAll,
10714-
EcsJournalTableEvents
10715-
} ecs_journal_kind_t;
10716-
10717-
FLECS_DBG_API
10718-
void flecs_journal_begin(
10719-
ecs_world_t *world,
10720-
ecs_journal_kind_t kind,
10721-
ecs_entity_t entity,
10722-
ecs_type_t *add,
10723-
ecs_type_t *remove);
10724-
10725-
FLECS_DBG_API
10726-
void flecs_journal_end(void);
10727-
10728-
#define flecs_journal(...)\
10729-
flecs_journal_begin(__VA_ARGS__);\
10730-
flecs_journal_end();
10731-
10732-
#ifdef __cplusplus
10733-
}
10734-
#endif // __cplusplus
10735-
/** @} */
10736-
#endif // FLECS_JOURNAL_H
10737-
#else
10738-
#define flecs_journal_begin(...)
10739-
#define flecs_journal_end(...)
10740-
#define flecs_journal(...)
10741-
10742-
#endif // FLECS_JOURNAL
10743-
1074410686
/**
1074510687
* @file addons/log.h
1074610688
* @brief Logging addon.

include/flecs.h

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,12 @@
171171
*/
172172
// #define FLECS_CPP_NO_ENUM_REFLECTION
173173

174+
/** @def FLECS_NO_ALWAYS_INLINE
175+
* When set, this will prevent functions from being annotated with always_inline
176+
* which can improve performance at the cost of increased binary footprint.
177+
*/
178+
// #define FLECS_NO_ALWAYS_INLINE
179+
174180
/** @def FLECS_CUSTOM_BUILD
175181
* This macro lets you customize which addons to build flecs with.
176182
* Without any addons Flecs is just a minimal ECS storage, but addons add
@@ -3158,7 +3164,7 @@ bool ecs_is_enabled_id(
31583164
* @see ecs_get_mut_id()
31593165
*/
31603166
FLECS_API
3161-
const void* ecs_get_id(
3167+
FLECS_ALWAYS_INLINE const void* ecs_get_id(
31623168
const ecs_world_t *world,
31633169
ecs_entity_t entity,
31643170
ecs_id_t id);
@@ -3175,7 +3181,7 @@ const void* ecs_get_id(
31753181
* @return The component pointer, NULL if the entity does not have the component.
31763182
*/
31773183
FLECS_API
3178-
void* ecs_get_mut_id(
3184+
FLECS_ALWAYS_INLINE void* ecs_get_mut_id(
31793185
const ecs_world_t *world,
31803186
ecs_entity_t entity,
31813187
ecs_id_t id);
@@ -3746,7 +3752,7 @@ char* ecs_entity_str(
37463752
* @see ecs_owns_id()
37473753
*/
37483754
FLECS_API
3749-
bool ecs_has_id(
3755+
FLECS_ALWAYS_INLINE bool ecs_has_id(
37503756
const ecs_world_t *world,
37513757
ecs_entity_t entity,
37523758
ecs_id_t id);
@@ -3762,7 +3768,7 @@ bool ecs_has_id(
37623768
* @return True if the entity has the id, false if not.
37633769
*/
37643770
FLECS_API
3765-
bool ecs_owns_id(
3771+
FLECS_ALWAYS_INLINE bool ecs_owns_id(
37663772
const ecs_world_t *world,
37673773
ecs_entity_t entity,
37683774
ecs_id_t id);

include/flecs/private/addons.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@
7474
#endif
7575

7676
/* Always included, if disabled functions are replaced with dummy macros */
77-
#include "flecs/private/journal.h"
7877
#include "flecs/addons/log.h"
7978

8079
/* Handle addon dependencies that need declarations to be visible in header */
@@ -217,7 +216,7 @@
217216
#ifdef FLECS_NO_OS_API_IMPL
218217
#error "FLECS_NO_OS_API_IMPL failed: OS_API_IMPL is required by other addons"
219218
#endif
220-
#include "../private/os_api_impl.h"
219+
#include "../addons/os_api_impl.h"
221220
#endif
222221

223222
#ifdef FLECS_MODULE

include/flecs/private/api_defines.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,18 @@ typedef struct ecs_allocator_t ecs_allocator_t;
258258
#define ECS_ALIGNOF(T) ((int64_t)&((struct { char c; T d; } *)0)->d)
259259
#endif
260260

261+
#ifndef FLECS_NO_ALWAYS_INLINE
262+
#if defined(ECS_TARGET_CLANG) || defined(ECS_TARGET_GCC)
263+
#define FLECS_ALWAYS_INLINE __attribute__((always_inline))
264+
#elif defined(ECS_TARGET_MSVC)
265+
#define FLECS_ALWAYS_INLINE __forceinline
266+
#else
267+
#define FLECS_ALWAYS_INLINE
268+
#endif
269+
#else
270+
#define FLECS_ALWAYS_INLINE
271+
#endif
272+
261273
#ifndef FLECS_NO_DEPRECATED_WARNINGS
262274
#if defined(ECS_TARGET_GNU)
263275
#define ECS_DEPRECATED(msg) __attribute__((deprecated(msg)))
File renamed without changes.

src/private_api.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "poly.h"
1616
#include "stage.h"
1717
#include "world.h"
18+
#include "journal.h"
1819
#include "datastructures/name_index.h"
1920

2021

0 commit comments

Comments
 (0)