Skip to content

Commit

Permalink
Improvement : minor improvements to reduce number of allocations in R…
Browse files Browse the repository at this point in the history
…PDOs, changed TPDOs to have same behavior

Only mapped length is considered for slicing and not the actual read/written length, otherwise this can offset
  • Loading branch information
samsamfire committed Oct 7, 2024
1 parent 1dda018 commit 4f3ec47
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
15 changes: 8 additions & 7 deletions pkg/pdo/rpdo.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ func (rpdo *RPDO) Process(timeDifferenceUs uint32, timerNext *uint32, nmtIsOpera
rpdo.mu.Lock()
defer rpdo.mu.Unlock()

var buffer = make([]byte, 0, MaxPdoLength)

pdo := rpdo.pdo
if !pdo.Valid || !nmtIsOperational || (!syncWas && rpdo.synchronous) {
// not valid and op, clear can receive flags & timeouttimer
Expand Down Expand Up @@ -103,9 +105,11 @@ func (rpdo *RPDO) Process(timeDifferenceUs uint32, timerNext *uint32, nmtIsOpera
}
// Copy RPDO into OD variables
rpdoReceived := false
totalNbWritten := uint32(0)

for rpdo.rxNew[bufNo] {
rpdoReceived = true
dataRPDO := rpdo.rxData[bufNo][:]
dataRPDO := rpdo.rxData[bufNo]
rpdo.rxNew[bufNo] = false
for i := range pdo.nbMapped {
streamer := &pdo.streamers[i]
Expand All @@ -114,20 +118,17 @@ func (rpdo *RPDO) Process(timeDifferenceUs uint32, timerNext *uint32, nmtIsOpera
if dataLength > uint32(MaxPdoLength) {
dataLength = uint32(MaxPdoLength)
}
// Prepare for writing into OD
var buffer []byte
buffer, dataRPDO = dataRPDO[:mappedLength], dataRPDO[mappedLength:]
buffer = dataRPDO[totalNbWritten : totalNbWritten+mappedLength]
if dataLength > uint32(mappedLength) {
// Append zeroes up to 8 bytes
buffer = append(buffer, make([]byte, int(MaxPdoLength)-len(buffer))...)
buffer = buffer[:cap(buffer)]
}
streamer.DataOffset = 0
_, err := streamer.Write(buffer)
if err != nil {
log.Warnf("[RPDO][%x] failed to write to OD on RPDO reception because %v", rpdo.pdo.configuredId, err)
}
streamer.DataOffset = mappedLength

totalNbWritten += mappedLength
}
}
if rpdo.timeoutTimeUs <= 0 {
Expand Down
5 changes: 2 additions & 3 deletions pkg/pdo/tpdo.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,12 +184,10 @@ func (tpdo *TPDO) send() error {
pdo := tpdo.pdo
eventDriven := tpdo.transmissionType == TransmissionTypeSyncAcyclic || tpdo.transmissionType >= uint8(TransmissionTypeSyncEventLo)

nbRead := 0
totalNbRead := 0
var err error

for i := range pdo.nbMapped {
totalNbRead += nbRead
streamer := &pdo.streamers[i]
mappedLength := streamer.DataOffset
dataLength := int(streamer.DataLength)
Expand All @@ -198,7 +196,7 @@ func (tpdo *TPDO) send() error {
}

streamer.DataOffset = 0
nbRead, err = streamer.Read(tpdo.txBuffer.Data[totalNbRead:])
_, err = streamer.Read(tpdo.txBuffer.Data[totalNbRead:])
if err != nil {
log.Warnf("[TPDO]sending TPDO cob id %x failed : %v", pdo.configuredId, err)
return err
Expand All @@ -209,6 +207,7 @@ func (tpdo *TPDO) send() error {
if flagPDOByte != nil && eventDriven {
*flagPDOByte |= pdo.flagPDOBitmask[i]
}
totalNbRead += int(mappedLength)
}
tpdo.sendRequest = false
tpdo.eventTimer = tpdo.eventTimeUs
Expand Down

0 comments on commit 4f3ec47

Please sign in to comment.