pymavlink
pymavlink copied to clipboard
Added slots
Slots is a great way to increase efficiency without an in-depth code change. The only caution I would have in accepting this is the following line of thought with regards to those who may try to write a wrapper and create an object within the Class that wasn't slotted ahead of time.
class Bob(object):
__slots__ = ('foo')
def __init__(self):
self.foo = 1
b = Bob()
In [1]: %run ./test.py
In [2]: b.bar = 2
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-2-9fce950889ff> in <module>
----> 1 b.bar = 2
AttributeError: 'Bob' object has no attribute 'bar'
If this is accepted I will continue submitting until everything that can be slotted is.
This is interesting, was not aware of slots! learnt a thing :-) From reading about it the main benefit to this that it is more memory efficient. @stryngs do you agree or is there something else to it?
@Gone4Dirt > This is interesting, was not aware of slots! learnt a thing :-) From reading about it the main benefit to this that it is more memory efficient. @stryngs do you agree or is there something else to it?
There is a speed boost to be gained as well. In a way you give up some of the dynamic capabilities Python3 offers in exchange for a faster outcome.
speed boost is going to be negligeable in most case unless you have a really big dict. So the real gain from this will be on message usage I think. For the use in this PR, that will provide a bit of type safety but the speed will be totally negligeable. I can be wrong, but until I see some real data show speed boost, I will trust Python docs on this !
I like slots, i'm in favour,if that help sway anyone.
On Sun, 30 Apr 2023, Buzz wrote:
I like slots, i'm in favour,if that help sway anyone.
Big questions to be answered
- does adding slots to each message save time/memory?
- does it kill any users of pymavlink that we know of?
- e.g. dronekit-python
- e.g. MAVProxy
- e.g. pymavlink itself :-)
- e.g. the ArduPilot autotest suite?
slots is a different way of thinking. The biggest straw to chew on would be to know how many projects out there customize the existing libraries. When you add slots to the mix the dynamic nature for that given class is gone. You can do workarounds to where this is not the case but then you're reducing the effectiveness of it and thus I wouldn't.
All a matter of choice as anyone who would be writing wrappers is probably capable of modifying the core Class to their needs, etc.
My experience with slots has been positive but I am limited to my worldview where I sniff packets. I write in Python and so any speed increase I can get is a want. It will "always?" lag behind a compiled language. When you add pypy3 to the mix Python starts to get really really snappy and it keeps up with Enterprise loads.
I have no idea if slots is the right fit for this project but if it is wanted I will happily do the PRs.
I look forward to the decision =)
great summary of what implications slots have here: https://wiki.python.org/moin/UsingSlots key takeaways: "slots are more efficient in terms of memory space and speed of access, and a bit safer than the default Python method of data access." "a slots declaration uses less memory than a dictionary, and direct memory access is faster than dictionary lookups" "slots is a class variable. If you have more than one instance of your class, any change made to slots will show up in every instance."
"you can add '__dict__' or '__weakref__' as the last element in the slots declaration" ...to enable ... "your class to use dynamic attribute creation or weak references".