RecordVal: Remove rt member

This doesn't seem to be really needed and costs 8 bytes per record value
This commit is contained in:
Arne Welzel 2025-09-27 17:41:26 +02:00
parent f30e0166a7
commit f17ca010bc
2 changed files with 24 additions and 26 deletions

View file

@ -2924,13 +2924,12 @@ TableVal::TableRecordDependencies TableVal::parse_time_table_record_dependencies
RecordVal::RecordTypeValMap RecordVal::parse_time_records; RecordVal::RecordTypeValMap RecordVal::parse_time_records;
RecordVal::RecordVal(RecordTypePtr t, bool init_fields) : Val(t) { RecordVal::RecordVal(RecordTypePtr t, bool init_fields) : Val(std::move(t)) {
rt = std::move(t); auto* rt = GetRecordType();
int n = rt->NumFields(); int n = rt->NumFields();
if ( run_state::is_parsing ) if ( run_state::is_parsing )
parse_time_records[rt.get()].emplace_back(NewRef{}, this); parse_time_records[rt].emplace_back(NewRef{}, this);
if ( init_fields ) { if ( init_fields ) {
record_val.resize(n); record_val.resize(n);
@ -2944,7 +2943,7 @@ RecordVal::RecordVal(RecordTypePtr t, bool init_fields) : Val(t) {
record_val[e.first] = e.second->Generate(); record_val[e.first] = e.second->Generate();
} catch ( InterpreterException& e ) { } catch ( InterpreterException& e ) {
if ( run_state::is_parsing ) if ( run_state::is_parsing )
parse_time_records[rt.get()].pop_back(); parse_time_records[rt].pop_back();
throw; throw;
} }
} }
@ -2956,15 +2955,13 @@ RecordVal::RecordVal(RecordTypePtr t, bool init_fields) : Val(t) {
record_val.reserve(n); record_val.reserve(n);
} }
RecordVal::RecordVal(RecordTypePtr t, std::vector<std::optional<ZVal>> init_vals) : Val(t) { RecordVal::RecordVal(RecordTypePtr t, std::vector<std::optional<ZVal>> init_vals) : Val(std::move(t)) {
rt = std::move(t);
// TODO: Change so that callers pass init_vals as ZValSlot instead? // TODO: Change so that callers pass init_vals as ZValSlot instead?
record_val.reserve(rt->NumFields()); size_t n = GetRecordType()->NumFields();
size_t n = rt->NumFields(); record_val.reserve(n);
for ( size_t i = 0; i < n; i++ ) { for ( size_t i = 0; i < n; i++ ) {
record_val.emplace_back(ZValSlot(rt->GetFieldType(i))); record_val.emplace_back(ZValSlot(GetRecordType()->GetFieldType(i)));
if ( init_vals[i].has_value() ) if ( init_vals[i].has_value() )
record_val[i] = init_vals[i].value(); record_val[i] = init_vals[i].value();
@ -2977,7 +2974,7 @@ ValPtr RecordVal::SizeVal() const { return val_mgr->Count(GetType()->AsRecordTyp
void RecordVal::Assign(int field, ValPtr new_val) { void RecordVal::Assign(int field, ValPtr new_val) {
if ( new_val ) { if ( new_val ) {
auto t = rt->GetFieldType(field); const auto& t = GetRecordType()->GetFieldType(field);
record_val[field] = ZValSlot(new_val, t); record_val[field] = ZValSlot(new_val, t);
Modified(); Modified();
} }
@ -3105,7 +3102,7 @@ void RecordVal::Describe(ODesc* d) const {
auto n = record_val.size(); auto n = record_val.size();
if ( d->IsBinary() ) { if ( d->IsBinary() ) {
rt->Describe(d); GetRecordType()->Describe(d);
d->SP(); d->SP();
d->Add(static_cast<uint64_t>(n)); d->Add(static_cast<uint64_t>(n));
d->SP(); d->SP();
@ -3117,7 +3114,7 @@ void RecordVal::Describe(ODesc* d) const {
if ( ! d->IsBinary() && i > 0 ) if ( ! d->IsBinary() && i > 0 )
d->Add(", "); d->Add(", ");
d->Add(rt->FieldName(i)); d->Add(GetRecordType()->FieldName(i));
if ( ! d->IsBinary() ) if ( ! d->IsBinary() )
d->Add("="); d->Add("=");
@ -3166,14 +3163,14 @@ ValPtr RecordVal::DoClone(CloneState* state) {
// record. As we cannot guarantee that it will be zeroed out at the // record. As we cannot guarantee that it will be zeroed out at the
// appropriate time (as it seems to be guaranteed for the original record) // appropriate time (as it seems to be guaranteed for the original record)
// we don't touch it. // we don't touch it.
auto rv = make_intrusive<RecordVal>(rt, false); auto rv = make_intrusive<RecordVal>(GetType<zeek::RecordType>(), false);
state->NewClone(this, rv); state->NewClone(this, rv);
int n = NumFields(); int n = NumFields();
for ( auto i = 0; i < n; ++i ) { for ( auto i = 0; i < n; ++i ) {
auto f_i = GetField(i); auto f_i = GetField(i);
auto v = f_i ? f_i->Clone(state) : nullptr; auto v = f_i ? f_i->Clone(state) : nullptr;
rv->AppendField(std::move(v), rt->GetFieldType(i)); rv->AppendField(std::move(v), GetRecordType()->GetFieldType(i));
} }
return rv; return rv;

View file

@ -1329,7 +1329,7 @@ public:
*/ */
template<class T> template<class T>
void AssignField(const char* field_name, T&& val) { void AssignField(const char* field_name, T&& val) {
int idx = rt->FieldOffset(field_name); int idx = GetRecordType()->FieldOffset(field_name);
if ( idx < 0 ) if ( idx < 0 )
reporter->InternalError("missing record field: %s", field_name); reporter->InternalError("missing record field: %s", field_name);
Assign(idx, std::forward<T>(val)); Assign(idx, std::forward<T>(val));
@ -1351,7 +1351,7 @@ public:
if ( record_val[field] ) if ( record_val[field] )
return true; return true;
return rt->DeferredInits()[field] != nullptr; return GetRecordType()->DeferredInits()[field] != nullptr;
} }
/** /**
@ -1361,7 +1361,7 @@ public:
* @return Whether there's a value for the given field name. * @return Whether there's a value for the given field name.
*/ */
bool HasField(const char* field) const { bool HasField(const char* field) const {
int idx = rt->FieldOffset(field); int idx = GetRecordType()->FieldOffset(field);
return (idx != -1) && HasField(idx); return (idx != -1) && HasField(idx);
} }
@ -1373,14 +1373,14 @@ public:
ValPtr GetField(int field) const { ValPtr GetField(int field) const {
auto& fv = record_val[field]; auto& fv = record_val[field];
if ( ! fv ) { if ( ! fv ) {
const auto& fi = rt->DeferredInits()[field]; const auto& fi = GetRecordType()->DeferredInits()[field];
if ( ! fi ) if ( ! fi )
return nullptr; return nullptr;
fv = fi->Generate(); fv = fi->Generate();
} }
return fv.ToVal(rt->GetFieldType(field)); return fv.ToVal(GetRecordType()->GetFieldType(field));
} }
/** /**
@ -1510,7 +1510,7 @@ public:
template<typename T> template<typename T>
auto GetFieldAs(const char* field) const { auto GetFieldAs(const char* field) const {
int idx = rt->FieldOffset(field); int idx = GetRecordType()->FieldOffset(field);
if ( idx < 0 ) if ( idx < 0 )
reporter->InternalError("missing record field: %s", field); reporter->InternalError("missing record field: %s", field);
@ -1591,7 +1591,7 @@ protected:
ZValSlot& RawOptField(int field) { ZValSlot& RawOptField(int field) {
auto& f = record_val[field]; auto& f = record_val[field];
if ( ! f ) { if ( ! f ) {
const auto& fi = rt->DeferredInits()[field]; const auto& fi = GetRecordType()->DeferredInits()[field];
if ( fi ) if ( fi )
f = fi->Generate(); f = fi->Generate();
} }
@ -1621,12 +1621,13 @@ protected:
private: private:
// Just for template inferencing. // Just for template inferencing.
RecordVal* Get() { return this; } RecordVal* Get() { return this; }
const RecordType* GetRecordType() const noexcept {
assert(GetType()->Tag() == TYPE_RECORD);
return static_cast<RecordType*>(GetType().get());
}
unsigned int ComputeFootprint(std::unordered_set<const Val*>* analyzed_vals) const override; unsigned int ComputeFootprint(std::unordered_set<const Val*>* analyzed_vals) const override;
// Keep this handy for quick access during low-level operations.
RecordTypePtr rt;
// Low-level values of each of the fields. // Low-level values of each of the fields.
// //
// Lazily modified during GetField(), so mutable. // Lazily modified during GetField(), so mutable.