zeek/src/CompHash.h
Max Kellermann 0db61f3094 include cleanup
The Zeek code base has very inconsistent #includes.  Many sources
included a few headers, and those headers included other headers, and
in the end, nearly everything is included everywhere, so missing
#includes were never noticed.  Another side effect was a lot of header
bloat which slows down the build.

First step to fix it: in each source file, its own header should be
included first to verify that each header's includes are correct, and
none is missing.

After adding the missing #includes, I replaced lots of #includes
inside headers with class forward declarations.  In most headers,
object pointers are never referenced, so declaring the function
prototypes with forward-declared classes is just fine.

This patch speeds up the build by 19%, because each compilation unit
gets smaller.  Here are the "time" numbers for a fresh build (with a
warm page cache but without ccache):

Before this patch:

 3144.94user 161.63system 3:02.87elapsed 1808%CPU (0avgtext+0avgdata 2168608maxresident)k
 760inputs+12008400outputs (1511major+57747204minor)pagefaults 0swaps

After this patch:

 2565.17user 141.83system 2:25.46elapsed 1860%CPU (0avgtext+0avgdata 1489076maxresident)k
 72576inputs+9130920outputs (1667major+49400430minor)pagefaults 0swaps
2020-02-04 20:51:02 +01:00

90 lines
2.8 KiB
C++

// See the file "COPYING" in the main distribution directory for copyright.
#pragma once
#include "Type.h"
class ListVal;
class HashKey;
class CompositeHash {
public:
explicit CompositeHash(TypeList* composite_type);
~CompositeHash();
// Compute the hash corresponding to the given index val,
// or 0 if it fails to typecheck.
HashKey* ComputeHash(const Val* v, int type_check) const;
// Given a hash key, recover the values used to create it.
ListVal* RecoverVals(const HashKey* k) const;
unsigned int MemoryAllocation() const { return padded_sizeof(*this) + pad_size(size); }
protected:
HashKey* ComputeSingletonHash(const Val* v, int type_check) const;
// Computes the piece of the hash for Val*, returning the new kp.
// Used as a helper for ComputeHash in the non-singleton case.
char* SingleValHash(int type_check, char* kp, BroType* bt, Val* v,
bool optional) const;
// Recovers just one Val of possibly many; called from RecoverVals.
// Upon return, pval will point to the recovered Val of type t.
// Returns and updated kp for the next Val. Calls reporter->InternalError()
// upon errors, so there is no return value for invalid input.
const char* RecoverOneVal(const HashKey* k,
const char* kp, const char* const k_end,
BroType* t, Val*& pval, bool optional) const;
// Rounds the given pointer up to the nearest multiple of the
// given size, if not already a multiple.
const void* Align(const char* ptr, unsigned int size) const;
// Rounds the given pointer up to the nearest multiple of the
// given size, padding the skipped region with 0 bytes.
void* AlignAndPad(char* ptr, unsigned int size) const;
// Returns offset+size rounded up so it can correctly align data
// of the given size.
int SizeAlign(int offset, unsigned int size) const;
template<class T>
T* AlignAndPadType(char* ptr) const
{
return reinterpret_cast<T*>(AlignAndPad(ptr, sizeof(T)));
}
template<class T>
const T* AlignType(const char* ptr) const
{
return reinterpret_cast<const T*>(Align(ptr, sizeof(T)));
}
template<class T>
int SizeAlignType(int offset) const
{
return SizeAlign(offset, sizeof(T));
}
// Compute the size of the composite key. If v is non-nil then
// the value is computed for the particular list of values.
// Returns 0 if the key has an indeterminant size (if v not given),
// or if v doesn't match the index type (if given).
int ComputeKeySize(const Val* v, int type_check,
bool calc_static_size) const;
int SingleTypeKeySize(BroType*, const Val*,
int type_check, int sz, bool optional,
bool calc_static_size) const;
TypeList* type;
char* key; // space for composite key
int size;
int is_singleton; // if just one type in index
// If one type, but not normal "singleton", e.g. record.
int is_complex_type;
InternalTypeTag singleton_tag;
};