Relating C++ move semantics to python objects

I have been learning lately about C++11 move semantics, originally being a C programmer who is concerned with the performance it was very interesting to explore such an important optimization feature. I had to go through many articles explaining rvalues, lvalues and the && operator. I believe you still need to learn these topics to develop a firm understanding of the topic but in my point of view the move semantics can be explained intuitively by drawing the similarity between it and python’s object execution model.

The goal of move semantics is to avoid copying large objects unnecessarily when they are passed from one function to another.

How are objects passed in python?

Interestingly enough, python was created with that particular optimization in mind.

Firstly, It’s important to understand the subtle difference between the interpretation of the following statement in python and C++

In C++ the variable str is created in memory, this location in memory contains the data medium and str refers to this memory location.

However in python the story is different. Although this statement also creates an object with the same content, but str isn’t the object reference. str is a merely a name and the statement binds it to the object. This subtle deviation may not look crucial but it makes all the difference when passing arguments to a function.

Consider the following 2 code snippets

In the first snippet, an immutable object is created containing the data ‘medium’ and str is bound to this object. When the method modify is called a new name arg is also bound to ‘medium’, but as soon as line 2 is executed the binding of arg is relocated to another new object with the data ‘hard’ because there is no way to change an immutable. The resulting behavior appears as a conventional pass by value call.

On the other hand, the behavior depicted in the second snippet resembles a pass by reference call as modify here doesn’t rebind arg to a new object because this object is mutable. arg and l are two names bound to the same object. When line 2 is executed this object is modified.

In python object references are passed by value, in other words objects are moved.

C++ move semantics in layman terms

Before the move semantics there were a couple of ways to avoid unnecessary large copies by passing pointers around which is somehow considered an unsafe practice compared to use an instantiated object which auto destructs.

Move semantics are based on rvalue references. An rvalue appears on the right hand side of the assignment statement. Example of an rvalue could be “medium” here:

Also the function getName() can return an rvalue because it appears on the right side of the statement.

name is an rvalue reference.

I like to think of an rvalue as a python object and the rvalue reference as the name bound to this object. The difference being that an rvalue can’t exist on its own while python relies on garbage collection to clean up objects which are no longer bound to any scope.

Without a move constructor the last line assignment will

  • allocate memory for the temporary object (and destruct it later)
  • allocate memory again at the destination

Adding a move constructor to the object definition will optimize out the second allocation and reuse the previous allocation for the destination.

Conclusion

Having this analogy in head, comprehending the power of move semantics was no more complicated. When a move constructor is defined for an object, it will be possible to rebind it to different name while avoiding expensive copies.

Resources

https://medium.com/@meghamohan/mutable-and-immutable-side-of-python-c2145cf72747

https://www.internalpointers.com/post/c-rvalue-references-and-move-semantics-beginners

https://mbevin.wordpress.com/2012/11/20/move-semantics/

https://stackoverflow.com/questions/3106110/what-is-move-semantics

 

Serial Communication with FTDI D2xx API on Windows

I was performing some testing on the FTDI throughput and I wrote this simple program that measures the throughput of FTDI chip if you connect its Rx pin to the Tx pin.

You are free to use it for any application with FTDI, it proved to be actually faster than using Windows VCP API but remember you need to install D2xx driver and make sure that your FTDI is actually using this driver and not the default ًWindows VCP driver

Note that the D2xx API uses a different method than conventional

    hSerial = CreateFile("COM9",GENERIC_READ | GENERIC_WRITE,
                              0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);

, it uses a string name instead that you can get from
device manager >> Right click on COM >> Details >> From the scroll list choose Device instance id >> you will find it after VID and PID

Code on github 

 

 

 

 

 

al-haitham is open source

I have benefited a lot from the open source community and it’s time to give back, I uploaded the source code of my graduation project which proposes a real time computer vision system  implemented on FPGA to translate human gestures into computer commands 

 First I would like to thank my graduation project team members:

   Ahmed Hafez Khalil

   Asmaa Omar

   Amani Mohamed Sedek

   Mohamed Ismail Khalil

   Mohamed Kamal Ali

   Mohamed Maged Abdel Majed

 

and here are the links to my grad project github repos: 

Hardware repo : https://github.com/aabdelfattah/alhaitham-hardware

Software repo : https://github.com/aabdelfattah/alhaitham-software

Have a look at thd brochure I designed for the EED competition 2012 which we won its 1st place 🙂

project-brochure-1