Skip to content

Commit

Permalink
ndnlp: reduce one allocation
Browse files Browse the repository at this point in the history
  • Loading branch information
pulsejet committed Dec 25, 2024
1 parent 8a90457 commit a0bcdf2
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 26 deletions.
33 changes: 19 additions & 14 deletions fw/face/ndnlp-link-service.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ type NDNLPLinkService struct {
BufferReader enc.BufferReader
congestionCheck uint64
outFrame []byte
outWire enc.Wire
}

// MakeNDNLPLinkService creates a new NDNLPv2 link service
Expand All @@ -88,7 +89,8 @@ func MakeNDNLPLinkService(transport transport, options NDNLPLinkServiceOptions)
l.nextSequence = 0
l.nextTxSequence = 0
l.congestionCheck = 0
l.outFrame = make([]byte, defn.MaxNDNPacketSize)
l.outFrame = make([]byte, defn.MaxNDNPacketSize*2)
l.outWire = make(enc.Wire, defn.MaxNDNPacketSize)
return l
}

Expand Down Expand Up @@ -266,23 +268,26 @@ func sendPacket(l *NDNLPLinkService, out dispatch.OutPkt) {
fragment.CongestionMark = congestionMark
}

pkt := &spec.Packet{
LpPacket: fragment,
}
pkt := spec.Packet{LpPacket: fragment}

// Use preallocated buffers for outgoing frame
encoder := spec.PacketEncoder{}
encoder.Init(pkt)
frameWire := encoder.Encode(pkt)
if frameWire == nil {
core.LogError(l, "Unable to encode fragment - DROP")
break
wirePlan := encoder.Init(&pkt)
outFrame := l.outFrame
outWire := l.outWire[:len(wirePlan)]

for i, l := range wirePlan {
outWire[i] = outFrame[:l]
outFrame = outFrame[l:]
}
encoder.EncodeInto(&pkt, outWire)

// Use preallocated buffer for outgoing frame
l.outFrame = l.outFrame[:0]
for _, b := range frameWire {
l.outFrame = append(l.outFrame, b...)
// Consolidate fragments. Since we only overwrite behind, there is no conflict.
nbytes := 0
for _, b := range outWire {
nbytes += copy(l.outFrame[nbytes:], b)
}
l.transport.sendFrame(l.outFrame)
l.transport.sendFrame(l.outFrame[:nbytes])
}
}

Expand Down
5 changes: 4 additions & 1 deletion std/encoding/codegen/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ func (m *TlvModel) GenEncoderStruct(buf *bytes.Buffer) error {

func (m *TlvModel) GenInitEncoder(buf *bytes.Buffer) error {
return template.Must(template.New("ModelInitEncoderStruct").Parse(`
func (encoder *{{.Name}}Encoder) Init(value *{{.Name}}) {
func (encoder *{{.Name}}Encoder) Init(value *{{.Name}})
{{- if .NoCopy -}} ([]uint) {{- end -}}
{
{{- range $f := .Fields}}
{{$f.GenInitEncoder}}
{{- end}}
Expand All @@ -83,6 +85,7 @@ func (m *TlvModel) GenInitEncoder(buf *bytes.Buffer) error {
wirePlan = append(wirePlan, l)
}
encoder.wirePlan = wirePlan
return wirePlan
{{- end}}
}
`)).Execute(buf, m)
Expand Down
3 changes: 2 additions & 1 deletion std/encoding/tests/gen_basic/zz_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions std/encoding/tests/gen_composition/zz_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions std/encoding/tests/gen_signature/zz_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 8 additions & 4 deletions std/ndn/spec_2022/zz_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion std/schema/demosec/zz_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a0bcdf2

Please sign in to comment.