Better pack fields in Variable

Used a BitField to for Variable fields instead of relying on the compiler, saving some memory probably.
This reduces sizeof(Variable) from 64 to 40 on x64

BUG=v8:5209

Review-Url: https://codereview.chromium.org/2257493002
Cr-Commit-Position: refs/heads/master@{#38891}
This commit is contained in:
heimbuef 2016-08-25 01:46:08 -07:00 committed by Commit bot
parent 3c1d076a85
commit 955606506c
3 changed files with 83 additions and 64 deletions

View File

@ -33,16 +33,15 @@ Variable::Variable(Scope* scope, const AstRawString* name, VariableMode mode,
MaybeAssignedFlag maybe_assigned_flag)
: scope_(scope),
name_(name),
mode_(mode),
kind_(kind),
location_(VariableLocation::UNALLOCATED),
local_if_not_shadowed_(nullptr),
index_(-1),
initializer_position_(kNoSourcePosition),
local_if_not_shadowed_(NULL),
force_context_allocation_(false),
is_used_(false),
initialization_flag_(initialization_flag),
maybe_assigned_(maybe_assigned_flag) {
bit_field_(MaybeAssignedFlagField::encode(maybe_assigned_flag) |
InitializationFlagField::encode(initialization_flag) |
VariableModeField::encode(mode) | IsUsedField::encode(false) |
ForceContextAllocationField::encode(false) |
LocationField::encode(VariableLocation::UNALLOCATED) |
KindField::encode(kind)) {
// Var declared variables never need initialization.
DCHECK(!(mode == VAR && initialization_flag == kNeedsInitialization));
}
@ -51,8 +50,8 @@ Variable::Variable(Scope* scope, const AstRawString* name, VariableMode mode,
bool Variable::IsGlobalObjectProperty() const {
// Temporaries are never global, they must always be allocated in the
// activation frame.
return (IsDynamicVariableMode(mode_) ||
(IsDeclaredVariableMode(mode_) && !IsLexicalVariableMode(mode_))) &&
return (IsDynamicVariableMode(mode()) ||
(IsDeclaredVariableMode(mode()) && !IsLexicalVariableMode(mode()))) &&
scope_ != NULL && scope_->is_script_scope();
}
@ -60,7 +59,7 @@ bool Variable::IsGlobalObjectProperty() const {
bool Variable::IsStaticGlobalObjectProperty() const {
// Temporaries are never global, they must always be allocated in the
// activation frame.
return (IsDeclaredVariableMode(mode_) && !IsLexicalVariableMode(mode_)) &&
return (IsDeclaredVariableMode(mode()) && !IsLexicalVariableMode(mode())) &&
scope_ != NULL && scope_->is_script_scope();
}

View File

@ -17,7 +17,13 @@ namespace internal {
// after binding and variable allocation.
class Variable final : public ZoneObject {
public:
enum Kind { NORMAL, FUNCTION, THIS, ARGUMENTS };
enum Kind : uint8_t {
NORMAL,
FUNCTION,
THIS,
ARGUMENTS,
kLastKind = ARGUMENTS
};
Variable(Scope* scope, const AstRawString* name, VariableMode mode, Kind kind,
InitializationFlag initialization_flag,
@ -38,51 +44,54 @@ class Variable final : public ZoneObject {
Handle<String> name() const { return name_->string(); }
const AstRawString* raw_name() const { return name_; }
VariableMode mode() const { return mode_; }
VariableMode mode() const { return VariableModeField::decode(bit_field_); }
bool has_forced_context_allocation() const {
return force_context_allocation_;
return ForceContextAllocationField::decode(bit_field_);
}
void ForceContextAllocation() {
DCHECK(IsUnallocated() || IsContextSlot());
force_context_allocation_ = true;
bit_field_ = ForceContextAllocationField::update(bit_field_, true);
}
bool is_used() { return IsUsedField::decode(bit_field_); }
void set_is_used() { bit_field_ = IsUsedField::update(bit_field_, true); }
MaybeAssignedFlag maybe_assigned() const {
return MaybeAssignedFlagField::decode(bit_field_);
}
void set_maybe_assigned() {
bit_field_ = MaybeAssignedFlagField::update(bit_field_, kMaybeAssigned);
}
bool is_used() { return is_used_; }
void set_is_used() { is_used_ = true; }
MaybeAssignedFlag maybe_assigned() const { return maybe_assigned_; }
void set_maybe_assigned() { maybe_assigned_ = kMaybeAssigned; }
int initializer_position() { return initializer_position_; }
void set_initializer_position(int pos) { initializer_position_ = pos; }
bool IsUnallocated() const {
return location_ == VariableLocation::UNALLOCATED;
return location() == VariableLocation::UNALLOCATED;
}
bool IsParameter() const { return location_ == VariableLocation::PARAMETER; }
bool IsStackLocal() const { return location_ == VariableLocation::LOCAL; }
bool IsParameter() const { return location() == VariableLocation::PARAMETER; }
bool IsStackLocal() const { return location() == VariableLocation::LOCAL; }
bool IsStackAllocated() const { return IsParameter() || IsStackLocal(); }
bool IsContextSlot() const { return location_ == VariableLocation::CONTEXT; }
bool IsGlobalSlot() const { return location_ == VariableLocation::GLOBAL; }
bool IsContextSlot() const { return location() == VariableLocation::CONTEXT; }
bool IsGlobalSlot() const { return location() == VariableLocation::GLOBAL; }
bool IsUnallocatedOrGlobalSlot() const {
return IsUnallocated() || IsGlobalSlot();
}
bool IsLookupSlot() const { return location_ == VariableLocation::LOOKUP; }
bool IsLookupSlot() const { return location() == VariableLocation::LOOKUP; }
bool IsGlobalObjectProperty() const;
bool IsStaticGlobalObjectProperty() const;
bool is_dynamic() const { return IsDynamicVariableMode(mode_); }
bool is_const_mode() const { return IsImmutableVariableMode(mode_); }
bool is_dynamic() const { return IsDynamicVariableMode(mode()); }
bool is_const_mode() const { return IsImmutableVariableMode(mode()); }
bool binding_needs_init() const {
DCHECK(initialization_flag_ != kNeedsInitialization ||
IsLexicalVariableMode(mode_));
return initialization_flag_ == kNeedsInitialization;
DCHECK(initialization_flag() != kNeedsInitialization ||
IsLexicalVariableMode(mode()));
return initialization_flag() == kNeedsInitialization;
}
bool is_function() const { return kind_ == FUNCTION; }
bool is_this() const { return kind_ == THIS; }
bool is_arguments() const { return kind_ == ARGUMENTS; }
bool is_function() const { return kind() == FUNCTION; }
bool is_this() const { return kind() == THIS; }
bool is_arguments() const { return kind() == ARGUMENTS; }
Variable* local_if_not_shadowed() const {
DCHECK(mode_ == DYNAMIC_LOCAL && local_if_not_shadowed_ != NULL);
DCHECK(mode() == DYNAMIC_LOCAL && local_if_not_shadowed_ != NULL);
return local_if_not_shadowed_;
}
@ -90,15 +99,21 @@ class Variable final : public ZoneObject {
local_if_not_shadowed_ = local;
}
VariableLocation location() const { return location_; }
int index() const { return index_; }
VariableLocation location() const {
return LocationField::decode(bit_field_);
}
Kind kind() const { return KindField::decode(bit_field_); }
InitializationFlag initialization_flag() const {
return initialization_flag_;
return InitializationFlagField::decode(bit_field_);
}
int index() const { return index_; }
void AllocateTo(VariableLocation location, int index) {
DCHECK(IsUnallocated() || (location_ == location && index_ == index));
location_ = location;
DCHECK(IsUnallocated() ||
(this->location() == location && this->index() == index));
bit_field_ = LocationField::update(bit_field_, location);
DCHECK_EQ(location, this->location());
index_ = index;
}
@ -107,23 +122,29 @@ class Variable final : public ZoneObject {
private:
Scope* scope_;
const AstRawString* name_;
VariableMode mode_;
Kind kind_;
VariableLocation location_;
int index_;
int initializer_position_;
// If this field is set, this variable references the stored locally bound
// variable, but it might be shadowed by variable bindings introduced by
// sloppy 'eval' calls between the reference scope (inclusive) and the
// binding scope (exclusive).
Variable* local_if_not_shadowed_;
int index_;
int initializer_position_;
uint16_t bit_field_;
// Usage info.
bool force_context_allocation_; // set by variable resolver
bool is_used_;
InitializationFlag initialization_flag_;
MaybeAssignedFlag maybe_assigned_;
class VariableModeField : public BitField16<VariableMode, 0, 3> {};
class KindField : public BitField16<Kind, VariableModeField::kNext, 2> {};
class LocationField
: public BitField16<VariableLocation, KindField::kNext, 3> {};
class ForceContextAllocationField
: public BitField16<bool, LocationField::kNext, 1> {};
class IsUsedField
: public BitField16<bool, ForceContextAllocationField::kNext, 1> {};
class InitializationFlagField
: public BitField16<InitializationFlag, IsUsedField::kNext, 2> {};
class MaybeAssignedFlagField
: public BitField16<MaybeAssignedFlag, InitializationFlagField::kNext,
2> {};
};
} // namespace internal
} // namespace v8

View File

@ -877,7 +877,7 @@ const double kMaxSafeInteger = 9007199254740991.0; // 2^53-1
// The order of this enum has to be kept in sync with the predicates below.
enum VariableMode {
enum VariableMode : uint8_t {
// User declared variables:
VAR, // declared via 'var', and 'function' declarations
@ -898,10 +898,12 @@ enum VariableMode {
// variable is global unless it has been shadowed
// by an eval-introduced variable
DYNAMIC_LOCAL // requires dynamic lookup, but we know that the
// variable is local and where it is unless it
// has been shadowed by an eval-introduced
// variable
DYNAMIC_LOCAL, // requires dynamic lookup, but we know that the
// variable is local and where it is unless it
// has been shadowed by an eval-introduced
// variable
kLastVariableMode = DYNAMIC_LOCAL
};
inline bool IsDynamicVariableMode(VariableMode mode) {
@ -923,7 +925,7 @@ inline bool IsImmutableVariableMode(VariableMode mode) {
return mode == CONST || mode == CONST_LEGACY;
}
enum class VariableLocation {
enum VariableLocation : uint8_t {
// Before and during variable allocation, a variable whose location is
// not yet determined. After allocation, a variable looked up as a
// property on the global object (and possibly absent). name() is the
@ -956,7 +958,9 @@ enum class VariableLocation {
LOOKUP,
// A named slot in a module's export table.
MODULE
MODULE,
kLastVariableLocation = MODULE
};
// ES6 Draft Rev3 10.2 specifies declarative environment records with mutable
@ -990,14 +994,9 @@ enum class VariableLocation {
// The following enum specifies a flag that indicates if the binding needs a
// distinct initialization step (kNeedsInitialization) or if the binding is
// immediately initialized upon creation (kCreatedInitialized).
enum InitializationFlag {
kNeedsInitialization,
kCreatedInitialized
};
enum MaybeAssignedFlag { kNotAssigned, kMaybeAssigned };
enum InitializationFlag : uint8_t { kNeedsInitialization, kCreatedInitialized };
enum MaybeAssignedFlag : uint8_t { kNotAssigned, kMaybeAssigned };
// Serialized in PreparseData, so numeric values should not be changed.
enum ParseErrorType { kSyntaxError = 0, kReferenceError = 1 };