Skip to content

Commit

Permalink
reduce heap allocations #10
Browse files Browse the repository at this point in the history
Summary:
Reuse the delay request buffer
```
ROUTINE ======================== github.com/facebook/time/ptp/sptp/client.(*Client).SendEventMsg in time/ptp/sptp/client/client.go
         0       5462 (flat, cum)  3.53% of Total
         .          .    120:func (c *Client) SendEventMsg(p ptp.Packet) (uint16, time.Time, error) {
         .          .    121:	seq := c.eventSequence
         .          .    122:	p.SetSequence(c.eventSequence)
         .       5462    123:	b, err := ptp.Bytes(p)
```
 {F1686268020}

Reviewed By: abulimov

Differential Revision: D58462288

fbshipit-source-id: bf2e44c513cb07a46e1702b171c79597c081ae92
  • Loading branch information
leoleovich authored and facebook-github-bot committed Jun 12, 2024
1 parent dfc67bc commit 4585814
Showing 1 changed file with 30 additions and 4 deletions.
34 changes: 30 additions & 4 deletions ptp/sptp/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ type Client struct {
eventConn UDPConnWithTS
// outgoing delay request packet
delayRequest *ptp.SyncDelayReq
// outgoing packet bytes buffer
delayReqBytes []byte

eventAddr *net.UDPAddr

Expand All @@ -117,15 +119,15 @@ func (c *Client) incrementSequence() {
}

// SendEventMsg sends an event message via event socket
func (c *Client) SendEventMsg(p ptp.Packet) (uint16, time.Time, error) {
func (c *Client) SendEventMsg(p *ptp.SyncDelayReq) (uint16, time.Time, error) {
seq := c.eventSequence
p.SetSequence(c.eventSequence)
b, err := ptp.Bytes(p)
_, err := ptp.BytesTo(p, c.delayReqBytes)
if err != nil {
return 0, time.Time{}, err
}
// send packet
_, hwts, err := c.eventConn.WriteToWithTS(b, c.eventAddr)
_, hwts, err := c.eventConn.WriteToWithTS(c.delayReqBytes, c.eventAddr)

c.incrementSequence()
if err != nil {
Expand All @@ -137,6 +139,29 @@ func (c *Client) SendEventMsg(p ptp.Packet) (uint16, time.Time, error) {
return seq, hwts, nil
}

// SendAnnounce sends an announce message via event socket
// It's used for external pingers such as ptping and not required for sptp itself
func (c *Client) SendAnnounce(p *ptp.Announce) (uint16, error) {
seq := c.eventSequence
p.SetSequence(c.eventSequence)
b, err := ptp.Bytes(p)
if err != nil {
return 0, err
}
// send packet
// since client only has the event conn we have to read the TS
_, _, err = c.eventConn.WriteToWithTS(b, c.eventAddr)

c.incrementSequence()
if err != nil {
log.Warnf("Error sending packet with SeqID = %04x: %v", seq, err)
return 0, err
}

log.Debugf("sent packet to %v", c.eventAddr)
return seq, nil
}

// NewClient initializes sptp client
func NewClient(target string, targetPort int, clockID ptp.ClockIdentity, eventConn UDPConnWithTS, cfg *Config, stats StatsServer) (*Client, error) {
// addresses
Expand All @@ -151,6 +176,7 @@ func NewClient(target string, targetPort int, clockID ptp.ClockIdentity, eventCo
sequenceIDMask: sequenceIDMask,
sequenceIDValue: sequenceIDMaskedValue,
delayRequest: ReqDelay(clockID, 1),
delayReqBytes: make([]byte, binary.Size(ptp.Header{})+binary.Size(ptp.SyncDelayReq{})),
eventConn: eventConn,
eventAddr: eventAddr,
inChan: make(chan bool, 100),
Expand Down Expand Up @@ -204,7 +230,7 @@ func (c *Client) handleDelayReq(clockID ptp.ClockIdentity, ts time.Time) error {
return err
}
announce := ReqAnnounce(clockID, 1, txts)
_, _, err = c.SendEventMsg(announce)
_, err = c.SendAnnounce(announce)
return err
}

Expand Down

0 comments on commit 4585814

Please sign in to comment.