mirror of
https://github.com/zeek/zeek.git
synced 2025-10-01 22:28:20 +00:00
RecordVal: Remove rt member
This doesn't seem to be really needed and costs 8 bytes per record value
This commit is contained in:
parent
f30e0166a7
commit
f17ca010bc
2 changed files with 24 additions and 26 deletions
29
src/Val.cc
29
src/Val.cc
|
@ -2924,13 +2924,12 @@ TableVal::TableRecordDependencies TableVal::parse_time_table_record_dependencies
|
|||
|
||||
RecordVal::RecordTypeValMap RecordVal::parse_time_records;
|
||||
|
||||
RecordVal::RecordVal(RecordTypePtr t, bool init_fields) : Val(t) {
|
||||
rt = std::move(t);
|
||||
|
||||
RecordVal::RecordVal(RecordTypePtr t, bool init_fields) : Val(std::move(t)) {
|
||||
auto* rt = GetRecordType();
|
||||
int n = rt->NumFields();
|
||||
|
||||
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 ) {
|
||||
record_val.resize(n);
|
||||
|
@ -2944,7 +2943,7 @@ RecordVal::RecordVal(RecordTypePtr t, bool init_fields) : Val(t) {
|
|||
record_val[e.first] = e.second->Generate();
|
||||
} catch ( InterpreterException& e ) {
|
||||
if ( run_state::is_parsing )
|
||||
parse_time_records[rt.get()].pop_back();
|
||||
parse_time_records[rt].pop_back();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
@ -2956,15 +2955,13 @@ RecordVal::RecordVal(RecordTypePtr t, bool init_fields) : Val(t) {
|
|||
record_val.reserve(n);
|
||||
}
|
||||
|
||||
RecordVal::RecordVal(RecordTypePtr t, std::vector<std::optional<ZVal>> init_vals) : Val(t) {
|
||||
rt = std::move(t);
|
||||
|
||||
RecordVal::RecordVal(RecordTypePtr t, std::vector<std::optional<ZVal>> init_vals) : Val(std::move(t)) {
|
||||
// TODO: Change so that callers pass init_vals as ZValSlot instead?
|
||||
record_val.reserve(rt->NumFields());
|
||||
size_t n = rt->NumFields();
|
||||
size_t n = GetRecordType()->NumFields();
|
||||
record_val.reserve(n);
|
||||
|
||||
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() )
|
||||
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) {
|
||||
if ( new_val ) {
|
||||
auto t = rt->GetFieldType(field);
|
||||
const auto& t = GetRecordType()->GetFieldType(field);
|
||||
record_val[field] = ZValSlot(new_val, t);
|
||||
Modified();
|
||||
}
|
||||
|
@ -3105,7 +3102,7 @@ void RecordVal::Describe(ODesc* d) const {
|
|||
auto n = record_val.size();
|
||||
|
||||
if ( d->IsBinary() ) {
|
||||
rt->Describe(d);
|
||||
GetRecordType()->Describe(d);
|
||||
d->SP();
|
||||
d->Add(static_cast<uint64_t>(n));
|
||||
d->SP();
|
||||
|
@ -3117,7 +3114,7 @@ void RecordVal::Describe(ODesc* d) const {
|
|||
if ( ! d->IsBinary() && i > 0 )
|
||||
d->Add(", ");
|
||||
|
||||
d->Add(rt->FieldName(i));
|
||||
d->Add(GetRecordType()->FieldName(i));
|
||||
|
||||
if ( ! d->IsBinary() )
|
||||
d->Add("=");
|
||||
|
@ -3166,14 +3163,14 @@ ValPtr RecordVal::DoClone(CloneState* state) {
|
|||
// 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)
|
||||
// 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);
|
||||
|
||||
int n = NumFields();
|
||||
for ( auto i = 0; i < n; ++i ) {
|
||||
auto f_i = GetField(i);
|
||||
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;
|
||||
|
|
21
src/Val.h
21
src/Val.h
|
@ -1329,7 +1329,7 @@ public:
|
|||
*/
|
||||
template<class T>
|
||||
void AssignField(const char* field_name, T&& val) {
|
||||
int idx = rt->FieldOffset(field_name);
|
||||
int idx = GetRecordType()->FieldOffset(field_name);
|
||||
if ( idx < 0 )
|
||||
reporter->InternalError("missing record field: %s", field_name);
|
||||
Assign(idx, std::forward<T>(val));
|
||||
|
@ -1351,7 +1351,7 @@ public:
|
|||
if ( record_val[field] )
|
||||
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.
|
||||
*/
|
||||
bool HasField(const char* field) const {
|
||||
int idx = rt->FieldOffset(field);
|
||||
int idx = GetRecordType()->FieldOffset(field);
|
||||
return (idx != -1) && HasField(idx);
|
||||
}
|
||||
|
||||
|
@ -1373,14 +1373,14 @@ public:
|
|||
ValPtr GetField(int field) const {
|
||||
auto& fv = record_val[field];
|
||||
if ( ! fv ) {
|
||||
const auto& fi = rt->DeferredInits()[field];
|
||||
const auto& fi = GetRecordType()->DeferredInits()[field];
|
||||
if ( ! fi )
|
||||
return nullptr;
|
||||
|
||||
fv = fi->Generate();
|
||||
}
|
||||
|
||||
return fv.ToVal(rt->GetFieldType(field));
|
||||
return fv.ToVal(GetRecordType()->GetFieldType(field));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1510,7 +1510,7 @@ public:
|
|||
|
||||
template<typename T>
|
||||
auto GetFieldAs(const char* field) const {
|
||||
int idx = rt->FieldOffset(field);
|
||||
int idx = GetRecordType()->FieldOffset(field);
|
||||
|
||||
if ( idx < 0 )
|
||||
reporter->InternalError("missing record field: %s", field);
|
||||
|
@ -1591,7 +1591,7 @@ protected:
|
|||
ZValSlot& RawOptField(int field) {
|
||||
auto& f = record_val[field];
|
||||
if ( ! f ) {
|
||||
const auto& fi = rt->DeferredInits()[field];
|
||||
const auto& fi = GetRecordType()->DeferredInits()[field];
|
||||
if ( fi )
|
||||
f = fi->Generate();
|
||||
}
|
||||
|
@ -1621,12 +1621,13 @@ protected:
|
|||
private:
|
||||
// Just for template inferencing.
|
||||
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;
|
||||
|
||||
// Keep this handy for quick access during low-level operations.
|
||||
RecordTypePtr rt;
|
||||
|
||||
// Low-level values of each of the fields.
|
||||
//
|
||||
// Lazily modified during GetField(), so mutable.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue