Skip to content

Commit

Permalink
improve struct alignment
Browse files Browse the repository at this point in the history
reduce memory usage by aligning structs
  • Loading branch information
sni committed Mar 23, 2024
1 parent b924a27 commit 83b79c3
Show file tree
Hide file tree
Showing 18 changed files with 217 additions and 230 deletions.
2 changes: 1 addition & 1 deletion pkg/lmd/a_helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,8 +441,8 @@ func httpMockHandler(ctx context.Context, t *testing.T, wrt io.Writer, rdr *http
// Credential string // unused
Options struct {
// Action string // unused
Args []string `json:"args"`
Sub string `json:"sub"`
Args []string `json:"args"`
} `json:"options"`
}
err := json.Unmarshal([]byte(rdr.PostFormValue("data")), &data)
Expand Down
10 changes: 5 additions & 5 deletions pkg/lmd/client_con.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@ const (
// ClientConnection handles a single client connection.
type ClientConnection struct {
noCopy noCopy
lmd *Daemon
connection net.Conn
lmd *Daemon
keepAliveTimer *time.Timer
queryStats *QueryStats
curRequest *Request
localAddr string
remoteAddr string
keepAlive bool
keepAliveTimer *time.Timer
listenTimeout int
logSlowQueryThreshold int
logHugeQueryThreshold int
queryStats *QueryStats
curRequest *Request
keepAlive bool
}

// NewClientConnection creates a new client connection object.
Expand Down
14 changes: 7 additions & 7 deletions pkg/lmd/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ type VirtualColumnResolveFunc func(d *DataRow, col *Column) interface{}
// VirtualColumnMapEntry is used to define the virtual key mapping in the VirtualColumnMap.
type VirtualColumnMapEntry struct {
noCopy noCopy
ResolveFunc VirtualColumnResolveFunc
Name string
StatusKey PeerStatusKey
ResolveFunc VirtualColumnResolveFunc
}

// VirtualColumnList maps the virtual columns with the peer status map entry.
Expand Down Expand Up @@ -191,7 +191,7 @@ const (
)

// OptionalFlagsStrings maps available backend flags to their string value.
var OptionalFlagsStrings = []struct {
var OptionalFlagsStrings = []struct { //nolint:govet // no need to align this struct, use only once
flag OptionalFlags
name string
}{
Expand Down Expand Up @@ -277,17 +277,17 @@ func (f *OptionalFlags) Clear() {
// Column is the definition of a single column within a DataRow.
type Column struct {
noCopy noCopy
RefCol *Column // reference to column in other table, ex.: host_alias
Table *Table // // reference to the table holding this column
VirtualMap *VirtualColumnMapEntry // reference to resolver for virtual columns
Name string // name and primary key
Description string // human description
Index int // position in datastore
RefColTableName TableName // shortcut to Column.RefCol.Table.Name
Optional OptionalFlags // flags if this column is used for certain backends only
DataType DataType // Type of this column
FetchType FetchType // flag wether this columns needs to be updated
StorageType StorageType // flag how this column is stored
Optional OptionalFlags // flags if this column is used for certain backends only
RefCol *Column // reference to column in other table, ex.: host_alias
RefColTableName TableName // shortcut to Column.RefCol.Table.Name
Table *Table // reference to the table holding this column
VirtualMap *VirtualColumnMapEntry // reference to resolver for virtual columns
}

// NewColumn adds a column object.
Expand Down
72 changes: 36 additions & 36 deletions pkg/lmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,20 @@ import (

// Connection defines a single connection configuration.
type Connection struct {
Name string
ID string
Source []string
Auth string
RemoteName string `toml:"remote_name"`
Section string
TLSCertificate string
TLSKey string
TLSCA string
TLSSkipVerify int
TLSServerName string
Proxy string
Flags []string
NoConfigTool int `toml:"noconfigtool"` // skip adding config tool to sites query
TLSCertificate string `toml:"tlscertificate"`
Name string `toml:"name"`
Proxy string `toml:"proxy"`
Auth string `toml:"auth"`
RemoteName string `toml:"remote_name"`
Section string `toml:"section"`
ID string `toml:"id"`
TLSKey string `toml:"tlskey"`
TLSServerName string `toml:"tlsservername"`
TLSCA string `toml:"tlsca"`
Source []string `toml:"source"`
Flags []string `toml:"flags"`
TLSSkipVerify int `toml:"tlsskipverify"`
NoConfigTool int `toml:"noconfigtool"` // skip adding config tool to sites query
}

// Equals checks if two connection objects are identical.
Expand Down Expand Up @@ -71,39 +71,39 @@ func (c *configFiles) Set(value string) (err error) {

// Config defines the available configuration options from supplied config files.
type Config struct {
Listen []string `toml:"Listen"`
Nodes []string `toml:"Nodes"`
GroupAuthorization string `toml:"GroupAuthorization"`
LogFile string `toml:"LogFile"`
TLSCertificate string `toml:"TLSCertificate"`
TLSMinVersion string `toml:"TLSMinVersion"`
ServiceAuthorization string `toml:"ServiceAuthorization"`
TLSKey string `toml:"TLSKey"`
TLSClientPems []string `toml:"TLSClientPems"`
UpdateInterval int64 `toml:"Updateinterval"`
FullUpdateInterval int64 `toml:"FullUpdateInterval"`
Connections []Connection `toml:"Connections"`
LogFile string `toml:"LogFile"`
LogLevel string `toml:"LogLevel"`
LogSlowQueryThreshold int `toml:"LogSlowQueryThreshold"`
ListenPrometheus string `toml:"ListenPrometheus"`
Connections []Connection `toml:"Connections"`
Nodes []string `toml:"Nodes"`
Listen []string `toml:"Listen"`
TLSClientPems []string `toml:"TLSClientPems"`
StaleBackendTimeout int `toml:"StaleBackendTimeout"`
LogHugeQueryThreshold int `toml:"LogHugeQueryThreshold"`
LogQueryStats bool `toml:"LogQueryStats"`
ConnectTimeout int `toml:"ConnectTimeout"`
NetTimeout int `toml:"NetTimeout"`
ListenTimeout int `toml:"ListenTimeout"`
SaveTempRequests bool `toml:"SaveTempRequests"`
ListenPrometheus string `toml:"ListenPrometheus"`
SkipSSLCheck int `toml:"SkipSSLCheck"`
UpdateInterval int64 `toml:"Updateinterval"`
MaxQueryFilter int `toml:"MaxQueryFilter"`
ConnectTimeout int `toml:"ConnectTimeout"`
IdleTimeout int64 `toml:"IdleTimeout"`
IdleInterval int64 `toml:"IdleInterval"`
StaleBackendTimeout int `toml:"StaleBackendTimeout"`
BackendKeepAlive bool `toml:"BackendKeepAlive"`
ServiceAuthorization string `toml:"ServiceAuthorization"`
GroupAuthorization string `toml:"GroupAuthorization"`
SyncIsExecuting bool `toml:"SyncIsExecuting"`
FullUpdateInterval int64 `toml:"FullUpdateInterval"`
MaxParallelPeerConnections int `toml:"MaxParallelPeerConnections"`
SkipSSLCheck int `toml:"SkipSSLCheck"`
LogSlowQueryThreshold int `toml:"LogSlowQueryThreshold"`
UpdateOffset int64 `toml:"UpdateOffset"`
CompressionMinimumSize int `toml:"CompressionMinimumSize"`
CompressionLevel int `toml:"CompressionLevel"`
MaxClockDelta float64 `toml:"MaxClockDelta"`
UpdateOffset int64 `toml:"UpdateOffset"`
TLSMinVersion string `toml:"TLSMinVersion"`
MaxParallelPeerConnections int `toml:"MaxParallelPeerConnections"`
MaxQueryFilter int `toml:"MaxQueryFilter"`
SyncIsExecuting bool `toml:"SyncIsExecuting"`
SaveTempRequests bool `toml:"SaveTempRequests"`
BackendKeepAlive bool `toml:"BackendKeepAlive"`
LogQueryStats bool `toml:"LogQueryStats"`
}

// NewConfig reads all config files.
Expand Down
11 changes: 5 additions & 6 deletions pkg/lmd/datarow.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@ const ListSepChar1 = "\x00"

// DataRow represents a single entry in a DataTable.
type DataRow struct {
noCopy noCopy // we don't want to make copies, use references
noCopy noCopy
DataStore *DataStore // reference to the datastore itself
Refs map[TableName]*DataRow // contains references to other objects, ex.: hosts from the services table
LastUpdate float64 // timestamp when this row has been updated
dataInt64List [][]int64 // stores lists of integers
dataString []string // stores string data
dataInt []int // stores integers
dataInt64 []int64 // stores large integers
dataFloat []float64 // stores floats
dataStringList [][]string // stores stringlists
dataInt64List [][]int64 // stores lists of integers
dataServiceMemberList [][]ServiceMember // stores list of servicemembers
dataStringLarge []StringContainer // stores large strings
dataInterfaceList [][]interface{}
dataInterfaceList [][]interface{} // stores anything else
LastUpdate float64 // timestamp of last update
}

// NewDataRow creates a new DataRow.
Expand Down Expand Up @@ -1660,8 +1660,7 @@ func (d *DataRow) checkAuth(authUser string) (canView bool) {
}

func (d *DataRow) CountStats(stats, result []*Filter) {
for idx, stat := range stats {
resultPos := idx
for resultPos, stat := range stats {
if stat.StatsPos > 0 {
resultPos = stat.StatsPos
}
Expand Down
20 changes: 10 additions & 10 deletions pkg/lmd/datastore.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@ import (
// DataStore contains the actual data rows with a reference to the table and peer.
type DataStore struct {
noCopy noCopy
DynamicColumnCache ColumnList // contains list of columns used to run periodic update
DynamicColumnNamesCache []string // contains list of keys used to run periodic update
Peer *Peer // reference to our peer
PeerName string // cached peer name
PeerKey string // cached peer key
PeerLockMode PeerLockMode // flag wether datarow have to set PeerLock when accessing status
DataSet *DataStoreSet // reference to parent DataSet
Data []*DataRow // the actual data rows
Index map[string]*DataRow // access data rows from primary key, ex.: hostname or comment id
Index2 map[string]map[string]*DataRow // access data rows from 2 primary keys, ex.: host and service
Peer *Peer // reference to our peer
LowerCaseColumns map[int]int // list of string column indexes with their coresponding lower case index
dupStringList map[[32]byte][]string // lookup pointer to other stringlists during initialization
Table *Table // reference to table definition
DataSet *DataStoreSet // reference to parent DataSet
PeerKey string // cached peer key
PeerName string // cached peer name
DynamicColumnCache ColumnList // contains list of keys used to run periodic update
Data []*DataRow // the actual data rows
Columns ColumnList // reference to the used columns
dupStringList map[[32]byte][]string // lookup pointer to other stringlists during initialization
LowerCaseColumns map[int]int // list of string column indexes with their coresponding lower case index
DynamicColumnNamesCache []string // contains list of keys used to run periodic update
PeerLockMode PeerLockMode // flag wether datarow have to set PeerLock when accessing status
}

// NewDataStore creates a new datastore with columns based on given flags.
Expand Down
1 change: 0 additions & 1 deletion pkg/lmd/datastoreset.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ func (ds *DataStoreSet) CreateObjectByType(ctx context.Context, table *Table) (s
// SetReferences creates reference entries for all tables.
func (ds *DataStoreSet) SetReferences() (err error) {
for _, t := range ds.tables {
t := t
err = t.SetReferences()
if err != nil {
logWith(ds).Debugf("setting references on table %s failed: %s", t.Table.Name.String(), err.Error())
Expand Down
46 changes: 20 additions & 26 deletions pkg/lmd/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,33 +47,27 @@ func (op *StatsType) String() string {
}

// Filter defines a single filter object.
// filter can either be a single filter
// or a group of filters (GroupOperator).
type Filter struct {
noCopy noCopy
// filter can either be a single filter
Column *Column
Operator Operator
StrValue string
FloatValue float64
IntValue int
Regexp *regexp.Regexp
CustomTag string
IsEmpty bool
Negate bool

// or a group of filters
Filter []*Filter
GroupOperator GroupOperator

// stats query
Stats float64
StatsCount int
StatsType StatsType
StatsPos int // position in stats result array

// copy of Column.Optional
ColumnOptional OptionalFlags
// copy of Column.Index if Column is of type LocalStore
ColumnIndex int
noCopy noCopy
Regexp *regexp.Regexp
Column *Column // filter can either be a single filter
StrValue string
CustomTag string
Filter []*Filter // or a group of filters
Stats float64 // stats query
IntValue int
FloatValue float64
StatsCount int
StatsPos int // position in stats result array
ColumnIndex int // copy of Column.Index if Column is of type LocalStore
ColumnOptional OptionalFlags // copy of Column.Optional
IsEmpty bool
Negate bool
GroupOperator GroupOperator
Operator Operator
StatsType StatsType
}

// Operator defines a filter operator.
Expand Down
6 changes: 3 additions & 3 deletions pkg/lmd/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ const (
// Listener is the object which handles incoming connections.
type Listener struct {
noCopy noCopy
Connection net.Listener
Lock *deadlock.RWMutex // must be used for when changing config
lmd *Daemon
connectionString string
Connection net.Listener
waitGroupDone *sync.WaitGroup
waitGroupInit *sync.WaitGroup
openConnections int64
queryStats *QueryStats
cleanup func()
connectionString string
openConnections int64
}

// NewListener creates a new Listener object.
Expand Down
Loading

0 comments on commit 83b79c3

Please sign in to comment.