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

Script performance improvements #1487

Open
wants to merge 27 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
dbab337
Reduce name lookups in script evaluation by reusing already resolved …
SanderMertens Dec 23, 2024
fee983c
Speed up casts between number types in script expressions
SanderMertens Dec 24, 2024
22283a2
Change script variable lookups to use frame offsets instead of name
SanderMertens Dec 24, 2024
13fe47c
Don't push template variables with name to improve performance
SanderMertens Dec 24, 2024
102abd5
Don't declare const variables in template scope with a name
SanderMertens Dec 24, 2024
33d417e
Bulk add components in entity script scope
SanderMertens Dec 24, 2024
fe13079
Fix misaligned memory access to template instance data
SanderMertens Dec 24, 2024
8626d5c
Allow script variables to be provided in arbitrary order
SanderMertens Dec 25, 2024
2ee680f
Fix issue with initialization of random number generator
SanderMertens Dec 25, 2024
23703e9
Fix issue with looking up entity in template scope with anonymous child
SanderMertens Dec 25, 2024
5732075
Remove empty table propagation from table cache
SanderMertens Dec 26, 2024
3ea8c87
Fix change detection for pipeline rebuilds
SanderMertens Dec 26, 2024
d49297b
Fix issue where assigning id value in script would not take into acco…
SanderMertens Dec 27, 2024
affe11a
Implement else if
SanderMertens Dec 27, 2024
a5539a6
Change meaning of standalone script scope to anonymous entity
SanderMertens Dec 27, 2024
9332fe2
Fix issue with parsing empty initializers
SanderMertens Dec 27, 2024
37c374b
Implement syntax for match expressions
SanderMertens Dec 27, 2024
a87c556
Implement basic evaluation of match expressions
SanderMertens Dec 27, 2024
2bd34e7
Add match test cases, fix bugs
SanderMertens Dec 28, 2024
80da4de
Fix more issues with empty initializers
SanderMertens Dec 28, 2024
f5ccc56
Implement match any case
SanderMertens Dec 28, 2024
5c28309
Add tests for assigning match expressions to components
SanderMertens Dec 28, 2024
6132d91
Remove need to use $ to refer to script variables
SanderMertens Dec 28, 2024
7687b5a
Document match expressions
SanderMertens Dec 28, 2024
b0ce3f0
Implement add assignment and multiply assignment operators
SanderMertens Dec 28, 2024
4422b08
Allow props to have implicit types
SanderMertens Dec 28, 2024
a400e83
Deprecate const var = ... notation in favor of const var: ...
SanderMertens Dec 28, 2024
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
2,901 changes: 1,971 additions & 930 deletions distr/flecs.c

Large diffs are not rendered by default.

93 changes: 77 additions & 16 deletions distr/flecs.h
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,6 @@ extern "C" {
//// Aperiodic action flags (used by ecs_run_aperiodic)
////////////////////////////////////////////////////////////////////////////////

#define EcsAperiodicEmptyTables (1u << 1u) /* Process pending empty table events */
#define EcsAperiodicComponentMonitors (1u << 2u) /* Process component monitors */
#define EcsAperiodicEmptyQueries (1u << 4u) /* Process empty queries */

Expand Down Expand Up @@ -3208,7 +3207,6 @@ typedef struct ecs_table_cache_hdr_t {
struct ecs_table_cache_t *cache; /**< Table cache of element. Of type ecs_id_record_t* for component index elements. */
ecs_table_t *table; /**< Table associated with element. */
struct ecs_table_cache_hdr_t *prev, *next; /**< Next/previous elements for id in table cache. */
bool empty; /**< Whether element is in empty list. */
} ecs_table_cache_hdr_t;

/** Metadata describing where a component id is stored in a table.
Expand Down Expand Up @@ -3758,7 +3756,8 @@ typedef struct ecs_worker_iter_t {
/* Convenience struct to iterate table array for id */
typedef struct ecs_table_cache_iter_t {
struct ecs_table_cache_hdr_t *cur, *next;
struct ecs_table_cache_hdr_t *next_list;
bool iter_fill;
bool iter_empty;
} ecs_table_cache_iter_t;

/** Each iterator */
Expand Down Expand Up @@ -4674,7 +4673,6 @@ typedef struct ecs_world_info_t {
int32_t pair_id_count; /**< Number of pair ids in the world */

int32_t table_count; /**< Number of tables */
int32_t empty_table_count; /**< Number of tables without entities */

/* -- Command counts -- */
struct {
Expand Down Expand Up @@ -5051,12 +5049,6 @@ FLECS_API extern const ecs_entity_t EcsOnTableCreate;
/** Event that triggers when a table is deleted. */
FLECS_API extern const ecs_entity_t EcsOnTableDelete;

/** Event that triggers when a table becomes empty (doesn't emit on creation). */
FLECS_API extern const ecs_entity_t EcsOnTableEmpty;

/** Event that triggers when a table becomes non-empty. */
FLECS_API extern const ecs_entity_t EcsOnTableFill;

/** Relationship used for specifying cleanup behavior. */
FLECS_API extern const ecs_entity_t EcsOnDelete;

Expand Down Expand Up @@ -10735,6 +10727,22 @@ void ecs_parser_errorv_(
const char *fmt,
va_list args);

FLECS_API
void ecs_parser_warning_(
const char *name,
const char *expr,
int64_t column,
const char *fmt,
...);

FLECS_API
void ecs_parser_warningv_(
const char *name,
const char *expr,
int64_t column,
const char *fmt,
va_list args);


////////////////////////////////////////////////////////////////////////////////
//// Logging macros
Expand Down Expand Up @@ -10980,6 +10988,12 @@ void ecs_parser_errorv_(
#define ecs_parser_errorv(name, expr, column, fmt, args)\
ecs_parser_errorv_(name, expr, column, fmt, args)

#define ecs_parser_warning(name, expr, column, ...)\
ecs_parser_warning_(name, expr, column, __VA_ARGS__)

#define ecs_parser_warningv(name, expr, column, fmt, args)\
ecs_parser_warningv_(name, expr, column, fmt, args)

#endif // FLECS_LEGACY


Expand Down Expand Up @@ -14370,12 +14384,15 @@ typedef struct ecs_script_var_t {
const char *name;
ecs_value_t value;
const ecs_type_info_t *type_info;
int32_t sp;
bool is_const;
} ecs_script_var_t;

/** Script variable scope. */
typedef struct ecs_script_vars_t {
struct ecs_script_vars_t *parent;
int32_t sp;

ecs_hashmap_t var_index;
ecs_vec_t vars;

Expand Down Expand Up @@ -14766,6 +14783,45 @@ ecs_script_var_t* ecs_script_vars_lookup(
const ecs_script_vars_t *vars,
const char *name);

/** Lookup a variable by stack pointer.
* This operation provides a faster way to lookup variables that are always
* declared in the same order in a ecs_script_vars_t scope.
*
* The stack pointer of a variable can be obtained from the ecs_script_var_t
* type. The provided frame offset must be valid for the provided variable
* stack. If the frame offset is not valid, this operation will panic.
*
* @param vars The variable scope.
* @param sp The stack pointer to the variable.
* @return The variable.
*/
FLECS_API
ecs_script_var_t* ecs_script_vars_from_sp(
const ecs_script_vars_t *vars,
int32_t sp);

/** Print variables.
* This operation prints all variables in the vars scope and parent scopes.asm
*
* @param vars The variable scope.
*/
FLECS_API
void ecs_script_vars_print(
const ecs_script_vars_t *vars);

/** Preallocate space for variables.
* This operation preallocates space for the specified number of variables. This
* is a performance optimization only, and is not necessary before declaring
* variables in a scope.
*
* @param vars The variable scope.
* @param count The number of variables to preallocate space for.
*/
FLECS_API
void ecs_script_vars_set_size(
ecs_script_vars_t *vars,
int32_t count);

/** Convert iterator to vars
* This operation converts an iterator to a variable array. This allows for
* using iterator results in expressions. The operation only converts a
Expand Down Expand Up @@ -14811,18 +14867,23 @@ void ecs_script_vars_from_iter(
typedef struct ecs_expr_eval_desc_t {
const char *name; /**< Script name */
const char *expr; /**< Full expression string */
const ecs_script_vars_t *vars; /**< Variables accessible in expression */
ecs_entity_t type; /**< Type of parsed value (optional) */
ecs_entity_t (*lookup_action)( /**< Function for resolving entity identifiers */
const ecs_world_t*,
const char *value,
void *ctx);
void *lookup_ctx; /**< Context passed to lookup function */
const ecs_script_vars_t *vars; /**< Variables accessible in expression */
ecs_entity_t type; /**< Type of parsed value (optional) */

/* Disable constant folding (slower evaluation, faster parsing) */

/** Disable constant folding (slower evaluation, faster parsing) */
bool disable_folding;

/* Allow for unresolved identifiers when parsing. Useful when entities can

/** This option instructs the expression runtime to lookup variables by
* stack pointer instead of by name, which improves performance. Only enable
* when provided variables are always declared in the same order. */
bool disable_dynamic_variable_binding;

/** Allow for unresolved identifiers when parsing. Useful when entities can
* be created in between parsing & evaluating. */
bool allow_unresolved_identifiers;

Expand Down
Loading
Loading