Skip to content

Commit ca0d50d

Browse files
Optimize boolean fields in JSFunctionDef, JSParseState and JSToken (#1232)
Reordered and changed multiple boolean fields in JSFunctionDef, JSParseState and JSToken structures from `bool` to `bool flag : 1` to optimize memory usage while maintaining readability.
1 parent 79f83f1 commit ca0d50d

File tree

1 file changed

+25
-21
lines changed

1 file changed

+25
-21
lines changed

quickjs.c

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20017,29 +20017,36 @@ typedef struct JSFunctionDef {
2001720017
struct list_head child_list; /* list of JSFunctionDef.link */
2001820018
struct list_head link;
2001920019

20020-
bool is_eval; /* true if eval code */
2002120020
int eval_type; /* only valid if is_eval = true */
20022-
bool is_global_var; /* true if variables are not defined locally:
20021+
20022+
/* Pack all boolean flags together as 1-bit fields to reduce struct size
20023+
while avoiding padding and compiler deoptimization. */
20024+
bool is_eval : 1; /* true if eval code */
20025+
bool is_global_var : 1; /* true if variables are not defined locally:
2002320026
eval global, eval module or non strict eval */
20024-
bool is_func_expr; /* true if function expression */
20025-
bool has_home_object; /* true if the home object is available */
20026-
bool has_prototype; /* true if a prototype field is necessary */
20027-
bool has_simple_parameter_list;
20028-
bool has_parameter_expressions; /* if true, an argument scope is created */
20029-
bool has_use_strict; /* to reject directive in special cases */
20030-
bool has_eval_call; /* true if the function contains a call to eval() */
20031-
bool has_arguments_binding; /* true if the 'arguments' binding is
20027+
bool is_func_expr : 1; /* true if function expression */
20028+
bool has_home_object : 1; /* true if the home object is available */
20029+
bool has_prototype : 1; /* true if a prototype field is necessary */
20030+
bool has_simple_parameter_list : 1;
20031+
bool has_parameter_expressions : 1; /* if true, an argument scope is created */
20032+
bool has_use_strict : 1; /* to reject directive in special cases */
20033+
bool has_eval_call : 1; /* true if the function contains a call to eval() */
20034+
bool has_arguments_binding : 1; /* true if the 'arguments' binding is
2003220035
available in the function */
20033-
bool has_this_binding; /* true if the 'this' and new.target binding are
20036+
bool has_this_binding : 1; /* true if the 'this' and new.target binding are
2003420037
available in the function */
20035-
bool new_target_allowed; /* true if the 'new.target' does not
20038+
bool new_target_allowed : 1; /* true if the 'new.target' does not
2003620039
throw a syntax error */
20037-
bool super_call_allowed; /* true if super() is allowed */
20038-
bool super_allowed; /* true if super. or super[] is allowed */
20039-
bool arguments_allowed; /* true if the 'arguments' identifier is allowed */
20040-
bool is_derived_class_constructor;
20041-
bool in_function_body;
20042-
bool backtrace_barrier;
20040+
bool super_call_allowed : 1; /* true if super() is allowed */
20041+
bool super_allowed : 1; /* true if super. or super[] is allowed */
20042+
bool arguments_allowed : 1; /* true if the 'arguments' identifier is allowed */
20043+
bool is_derived_class_constructor : 1;
20044+
bool in_function_body : 1;
20045+
bool backtrace_barrier : 1;
20046+
bool need_home_object : 1;
20047+
bool use_short_opcodes : 1; /* true if short opcodes are used in byte_code */
20048+
bool has_await : 1; /* true if await is used (used in module eval) */
20049+
2004320050
JSFunctionKindEnum func_kind : 8;
2004420051
JSParseFunctionEnum func_type : 7;
2004520052
uint8_t is_strict_mode : 1;
@@ -20065,7 +20072,6 @@ typedef struct JSFunctionDef {
2006520072
int new_target_var_idx; /* variable containg the 'new.target' value, -1 if none */
2006620073
int this_active_func_var_idx; /* variable containg the 'this.active_func' value, -1 if none */
2006720074
int home_object_var_idx;
20068-
bool need_home_object;
2006920075

2007020076
int scope_level; /* index into fd->scopes if the current lexical scope */
2007120077
int scope_first; /* index into vd->vars of first lexically scoped variable */
@@ -20081,7 +20087,6 @@ typedef struct JSFunctionDef {
2008120087

2008220088
DynBuf byte_code;
2008320089
int last_opcode_pos; /* -1 if no last opcode */
20084-
bool use_short_opcodes; /* true if short opcodes are used in byte_code */
2008520090

2008620091
LabelSlot *label_slots;
2008720092
int label_size; /* allocated size for label_slots[] */
@@ -20119,7 +20124,6 @@ typedef struct JSFunctionDef {
2011920124
int source_len;
2012020125

2012120126
JSModuleDef *module; /* != NULL when parsing a module */
20122-
bool has_await; /* true if await is used (used in module eval) */
2012320127
} JSFunctionDef;
2012420128

2012520129
typedef struct JSToken {

0 commit comments

Comments
 (0)