bertmelis / espMqttClient

MQTT 3.1.1 client library for the Espressif devices ESP8266 and ESP32 on the Arduino framework.
https://www.emelis.net/espMqttClient/
MIT License
100 stars 21 forks source link

improved memory by no coping packet #100

Closed lumapu closed 1 year ago

lumapu commented 1 year ago

in MqttClient::_onPublish() the IncomingPacket is unnecessarily copied. Now the Parser-class returns a const pointer of the packet.

bertmelis commented 1 year ago

The original code returns by reference so no copy is made afaik.

Disassembly shows this:

# src/Packets/Parser.cpp:61:   return _packet;
    .loc 1 61 10 is_stmt 0 view .LVU1391
    leaq    40(%rdi), %rax  #, tmp84
# src/Packets/Parser.cpp:62: }
bertmelis commented 1 year ago

For what it's worth, I changed to a proper copy and the assembly showed this:

# src/Packets/Parser.cpp:61:   return _packet;
    .loc 1 61 10 is_stmt 0 view .LVU1392
    movdqu  40(%rsi), %xmm0 # this_2(D)->_packet, tmp97
    movups  %xmm0, (%rdi)   # tmp97, <retval>
    movdqu  56(%rsi), %xmm1 # this_2(D)->_packet, tmp98
    movups  %xmm1, 16(%rdi) # tmp98, <retval>
    movdqu  72(%rsi), %xmm2 # this_2(D)->_packet, tmp99
    movups  %xmm2, 32(%rdi) # tmp99, <retval>
    movdqu  88(%rsi), %xmm3 # this_2(D)->_packet, tmp100
    movups  %xmm3, 48(%rdi) # tmp100, <retval>
    movdqu  104(%rsi), %xmm4    # this_2(D)->_packet, tmp101
    movups  %xmm4, 64(%rdi) # tmp101, <retval>
    movdqu  120(%rsi), %xmm5    # this_2(D)->_packet, tmp102
    movups  %xmm5, 80(%rdi) # tmp102, <retval>
    movdqu  136(%rsi), %xmm6    # this_2(D)->_packet, tmp103
    movups  %xmm6, 96(%rdi) # tmp103, <retval>
    movdqu  152(%rsi), %xmm7    # this_2(D)->_packet, tmp104
    movups  %xmm7, 112(%rdi)    # tmp104, <retval>
    movdqu  168(%rsi), %xmm0    # this_2(D)->_packet, tmp105
    movups  %xmm0, 128(%rdi)    # tmp105, <retval>
    movdqu  184(%rsi), %xmm1    # this_2(D)->_packet, tmp106
    movups  %xmm1, 144(%rdi)    # tmp106, <retval>
    movdqu  200(%rsi), %xmm2    # this_2(D)->_packet, tmp107
    movups  %xmm2, 160(%rdi)    # tmp107, <retval>
# src/Packets/Parser.cpp:62: }
lumapu commented 1 year ago

Thanks for the disassembly, it definetly shows that no copy is made