Does checking for getOutputCount before writing on the port make sense? #2533
Replies: 3 comments
-
Related issues (that actually sparkled this discussion):
This is quite relevant to the icub-main codebase, in which pattern is quite widespread. @pattacini @Nicogene |
Beta Was this translation helpful? Give feedback.
-
I think that using Ideally, the function should simply return a number, hence no other instructions should be executed. |
Beta Was this translation helpful? Give feedback.
-
To my understanding, if you use it to avoid writing on the port, this is just wrong, because the port will do the same internally. |
Beta Was this translation helpful? Give feedback.
-
Hi YARP developers and users,
I have a doubt on the
BufferPort::getOutputCount
function. A pattern quite widespread in some YARP-related codebases (see for example icub-main, i.e. https://github.com/robotology/icub-main/search?q=getOutputCount) that I also propagated over the years is to write on a BufferPort only ifgetOutputCount()
is greater then zero:I never found this discussed, but I can imagine that the goal is to reduce the use of CPU time (or other resources used in
_port.prepare()/_port.write()
calls) by avoiding those calls if no one is connected to the specific port. The hidden assumption here is that_port.getOutputCount()
is rather cheap call, i.e. something that is just accessing a small buffer.However, if you wear your muddy boots and check the code in PortCore, it seems that
getOutputCount
also calls thecleanUnits(false)
method:yarp/src/libYARP_os/src/yarp/os/impl/PortCore.cpp
Line 1428 in 9b52a71
That in turns seems a rather complicated function (and to be honest I am not sure what it is supposed to do):
yarp/src/libYARP_os/src/yarp/os/impl/PortCore.cpp
Line 591 in 9b52a71
If you check the
cleanUnits
docs, it says "// called anytime, garbage collects terminated units":And while I am not sure what that means in this context, "garbage collection" is not something that is typically associated with inexpensive operations.
Having said that, my questions is:
TL;DR: Does it make sense use getOutputCount to avoid writing on a BufferedPort, or perhaps it is better to just write all the time on the BufferPort?
Beta Was this translation helpful? Give feedback.
All reactions