Skip to content

Commit

Permalink
Merge pull request #71 from mariomac/rmalloc
Browse files Browse the repository at this point in the history
Avoid unnecessary memory allocations
  • Loading branch information
lspgn authored Feb 9, 2022
2 parents 8d59905 + 615b9f6 commit 7d4c41a
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 37 deletions.
10 changes: 5 additions & 5 deletions decoders/netflow/netflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type NetFlowTemplateSystem interface {
}

func DecodeNFv9OptionsTemplateSet(payload *bytes.Buffer) ([]NFv9OptionsTemplateRecord, error) {
records := make([]NFv9OptionsTemplateRecord, 0)
var records []NFv9OptionsTemplateRecord
var err error
for payload.Len() >= 4 {
optsTemplateRecord := NFv9OptionsTemplateRecord{}
Expand Down Expand Up @@ -68,7 +68,7 @@ func DecodeField(payload *bytes.Buffer, field *Field, pen bool) error {
}

func DecodeIPFIXOptionsTemplateSet(payload *bytes.Buffer) ([]IPFIXOptionsTemplateRecord, error) {
records := make([]IPFIXOptionsTemplateRecord, 0)
var records []IPFIXOptionsTemplateRecord
var err error
for payload.Len() >= 4 {
optsTemplateRecord := IPFIXOptionsTemplateRecord{}
Expand Down Expand Up @@ -108,7 +108,7 @@ func DecodeIPFIXOptionsTemplateSet(payload *bytes.Buffer) ([]IPFIXOptionsTemplat
}

func DecodeTemplateSet(version uint16, payload *bytes.Buffer) ([]TemplateRecord, error) {
records := make([]TemplateRecord, 0)
var records []TemplateRecord
var err error
for payload.Len() >= 4 {
templateRecord := TemplateRecord{}
Expand Down Expand Up @@ -214,7 +214,7 @@ func (e *ErrorTemplateNotFound) Error() string {
}

func DecodeOptionsDataSet(version uint16, payload *bytes.Buffer, listFieldsScopes, listFieldsOption []Field) ([]OptionsDataRecord, error) {
records := make([]OptionsDataRecord, 0)
var records []OptionsDataRecord

listFieldsScopesSize := GetTemplateSize(version, listFieldsScopes)
listFieldsOptionSize := GetTemplateSize(version, listFieldsOption)
Expand All @@ -234,7 +234,7 @@ func DecodeOptionsDataSet(version uint16, payload *bytes.Buffer, listFieldsScope
}

func DecodeDataSet(version uint16, payload *bytes.Buffer, listFields []Field) ([]DataRecord, error) {
records := make([]DataRecord, 0)
var records []DataRecord

listFieldsSize := GetTemplateSize(version, listFields)
for payload.Len() >= listFieldsSize {
Expand Down
2 changes: 1 addition & 1 deletion decoders/sflow/sflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ func DecodeFlowRecord(header *RecordHeader, payload *bytes.Buffer) (FlowRecord,
if err != nil {
return flowRecord, err
}
asPath := make([]uint32, 0)
var asPath []uint32
if extendedGateway.ASDestinations != 0 {
err := utils.BinaryDecoder(payload, &(extendedGateway.ASPathType), &(extendedGateway.ASPathLength))
if err != nil {
Expand Down
42 changes: 21 additions & 21 deletions producer/producer_nf.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ func ConvertNetFlowDataSet(version uint16, baseTime uint32, uptime uint32, recor
}

func SearchNetFlowDataSetsRecords(version uint16, baseTime uint32, uptime uint32, dataRecords []netflow.DataRecord, mapperNetFlow *NetFlowMapper, mapperSFlow *SFlowMapper) []*flowmessage.FlowMessage {
flowMessageSet := make([]*flowmessage.FlowMessage, 0)
var flowMessageSet []*flowmessage.FlowMessage
for _, record := range dataRecords {
fmsg := ConvertNetFlowDataSet(version, baseTime, uptime, record.Values, mapperNetFlow, mapperSFlow)
if fmsg != nil {
Expand All @@ -411,7 +411,7 @@ func SearchNetFlowDataSetsRecords(version uint16, baseTime uint32, uptime uint32
}

func SearchNetFlowDataSets(version uint16, baseTime uint32, uptime uint32, dataFlowSet []netflow.DataFlowSet, mapperNetFlow *NetFlowMapper, mapperSFlow *SFlowMapper) []*flowmessage.FlowMessage {
flowMessageSet := make([]*flowmessage.FlowMessage, 0)
var flowMessageSet []*flowmessage.FlowMessage
for _, dataFlowSetItem := range dataFlowSet {
fmsg := SearchNetFlowDataSetsRecords(version, baseTime, uptime, dataFlowSetItem.Records, mapperNetFlow, mapperSFlow)
if fmsg != nil {
Expand Down Expand Up @@ -444,40 +444,40 @@ func SearchNetFlowOptionDataSets(dataFlowSet []netflow.OptionsDataFlowSet) (uint
}

func SplitNetFlowSets(packetNFv9 netflow.NFv9Packet) ([]netflow.DataFlowSet, []netflow.TemplateFlowSet, []netflow.NFv9OptionsTemplateFlowSet, []netflow.OptionsDataFlowSet) {
dataFlowSet := make([]netflow.DataFlowSet, 0)
templatesFlowSet := make([]netflow.TemplateFlowSet, 0)
optionsTemplatesFlowSet := make([]netflow.NFv9OptionsTemplateFlowSet, 0)
optionsDataFlowSet := make([]netflow.OptionsDataFlowSet, 0)
var dataFlowSet []netflow.DataFlowSet
var templatesFlowSet []netflow.TemplateFlowSet
var optionsTemplatesFlowSet []netflow.NFv9OptionsTemplateFlowSet
var optionsDataFlowSet []netflow.OptionsDataFlowSet
for _, flowSet := range packetNFv9.FlowSets {
switch flowSet.(type) {
switch tFlowSet := flowSet.(type) {
case netflow.TemplateFlowSet:
templatesFlowSet = append(templatesFlowSet, flowSet.(netflow.TemplateFlowSet))
templatesFlowSet = append(templatesFlowSet, tFlowSet)
case netflow.NFv9OptionsTemplateFlowSet:
optionsTemplatesFlowSet = append(optionsTemplatesFlowSet, flowSet.(netflow.NFv9OptionsTemplateFlowSet))
optionsTemplatesFlowSet = append(optionsTemplatesFlowSet, tFlowSet)
case netflow.DataFlowSet:
dataFlowSet = append(dataFlowSet, flowSet.(netflow.DataFlowSet))
dataFlowSet = append(dataFlowSet, tFlowSet)
case netflow.OptionsDataFlowSet:
optionsDataFlowSet = append(optionsDataFlowSet, flowSet.(netflow.OptionsDataFlowSet))
optionsDataFlowSet = append(optionsDataFlowSet, tFlowSet)
}
}
return dataFlowSet, templatesFlowSet, optionsTemplatesFlowSet, optionsDataFlowSet
}

func SplitIPFIXSets(packetIPFIX netflow.IPFIXPacket) ([]netflow.DataFlowSet, []netflow.TemplateFlowSet, []netflow.IPFIXOptionsTemplateFlowSet, []netflow.OptionsDataFlowSet) {
dataFlowSet := make([]netflow.DataFlowSet, 0)
templatesFlowSet := make([]netflow.TemplateFlowSet, 0)
optionsTemplatesFlowSet := make([]netflow.IPFIXOptionsTemplateFlowSet, 0)
optionsDataFlowSet := make([]netflow.OptionsDataFlowSet, 0)
var dataFlowSet []netflow.DataFlowSet
var templatesFlowSet []netflow.TemplateFlowSet
var optionsTemplatesFlowSet []netflow.IPFIXOptionsTemplateFlowSet
var optionsDataFlowSet []netflow.OptionsDataFlowSet
for _, flowSet := range packetIPFIX.FlowSets {
switch flowSet.(type) {
switch tFlowSet := flowSet.(type) {
case netflow.TemplateFlowSet:
templatesFlowSet = append(templatesFlowSet, flowSet.(netflow.TemplateFlowSet))
templatesFlowSet = append(templatesFlowSet, tFlowSet)
case netflow.IPFIXOptionsTemplateFlowSet:
optionsTemplatesFlowSet = append(optionsTemplatesFlowSet, flowSet.(netflow.IPFIXOptionsTemplateFlowSet))
optionsTemplatesFlowSet = append(optionsTemplatesFlowSet, tFlowSet)
case netflow.DataFlowSet:
dataFlowSet = append(dataFlowSet, flowSet.(netflow.DataFlowSet))
dataFlowSet = append(dataFlowSet, tFlowSet)
case netflow.OptionsDataFlowSet:
optionsDataFlowSet = append(optionsDataFlowSet, flowSet.(netflow.OptionsDataFlowSet))
optionsDataFlowSet = append(optionsDataFlowSet, tFlowSet)
}
}
return dataFlowSet, templatesFlowSet, optionsTemplatesFlowSet, optionsDataFlowSet
Expand All @@ -494,7 +494,7 @@ func ProcessMessageNetFlowConfig(msgDec interface{}, samplingRateSys SamplingRat
var baseTime uint32
var uptime uint32

flowMessageSet := make([]*flowmessage.FlowMessage, 0)
var flowMessageSet []*flowmessage.FlowMessage

switch msgDecConv := msgDec.(type) {
case netflow.NFv9Packet:
Expand Down
2 changes: 1 addition & 1 deletion producer/producer_nflegacy.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func ConvertNetFlowLegacyRecord(baseTime uint32, uptime uint32, record netflowle
}

func SearchNetFlowLegacyRecords(baseTime uint32, uptime uint32, dataRecords []netflowlegacy.RecordsNetFlowV5) []*flowmessage.FlowMessage {
flowMessageSet := make([]*flowmessage.FlowMessage, 0)
var flowMessageSet []*flowmessage.FlowMessage
for _, record := range dataRecords {
fmsg := ConvertNetFlowLegacyRecord(baseTime, uptime, record)
if fmsg != nil {
Expand Down
4 changes: 2 additions & 2 deletions producer/producer_sf.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

func GetSFlowFlowSamples(packet *sflow.Packet) []interface{} {
flowSamples := make([]interface{}, 0)
var flowSamples []interface{}
for _, sample := range packet.Samples {
switch sample.(type) {
case sflow.FlowSample:
Expand Down Expand Up @@ -236,7 +236,7 @@ func SearchSFlowSamples(samples []interface{}) []*flowmessage.FlowMessage {
}

func SearchSFlowSamplesConfig(samples []interface{}, config *SFlowMapper) []*flowmessage.FlowMessage {
flowMessageSet := make([]*flowmessage.FlowMessage, 0)
var flowMessageSet []*flowmessage.FlowMessage

for _, flowSample := range samples {
var records []sflow.FlowRecord
Expand Down
6 changes: 1 addition & 5 deletions producer/reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,13 +157,9 @@ func MapFieldsSFlow(fields []SFlowMapField) *SFlowMapper {
Length: field.Length,
Destination: field.Destination,
}
retLayer, ok := ret[field.Layer]
if !ok {
retLayer = make([]DataMapLayer, 0)
}
retLayer := ret[field.Layer]
retLayer = append(retLayer, retLayerEntry)
ret[field.Layer] = retLayer

}
return &SFlowMapper{ret}
}
Expand Down
2 changes: 1 addition & 1 deletion transport/kafka/kafka.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func (d *KafkaDriver) Init(context.Context) error {
}
}

addrs := make([]string, 0)
var addrs []string
if d.kafkaSrv != "" {
addrs, _ = utils.GetServiceAddresses(d.kafkaSrv)
} else {
Expand Down
2 changes: 1 addition & 1 deletion utils/netflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func (s *StateNetFlow) DecodeFlow(msg interface{}) error {
return err
}

flowMessageSet := make([]*flowmessage.FlowMessage, 0)
var flowMessageSet []*flowmessage.FlowMessage

switch msgDecConv := msgDec.(type) {
case netflow.NFv9Packet:
Expand Down

0 comments on commit 7d4c41a

Please sign in to comment.