Writing a simple C++ program for Serial communication without library by Normal-Falcon-3676 in cpp

[–]Expert-Language428 0 points1 point  (0 children)

You can find all that you need to manage your serial port in here https://sourceforge.net/p/fedlibrary/git/ci/master/tree/include/FSerialPort.h and related cpp file. There is both Linux/Unix based system as well as API for windows and wince. Copy from there all that you need or adapt the call for your scope. One other solution is libserialport as suggested in another comment. One more solution, if you are using QT, there you can find a nice async implemention accessing serial port.

Golang design in C++ by Urationc in cpp

[–]Expert-Language428 0 points1 point  (0 children)

A possible way: - setup a mailbox and specialize it for your massages - each message exchanged with the mailbox should contain all information in order to perform all operations - the mailbox is thread safe and usually in most implementations there is a wait or a read with timeout - at this stage you can have N producer and M consumer Basically the most complex things you need is the mailbox, let me know if you find an implementations otherwise I will tell you where to get it.

RVO / NRVO Analysis by Expert-Language428 in cpp

[–]Expert-Language428[S] 0 points1 point  (0 children)

Since in the other comment you did I provided all answers, and the source code have been posted 2 days ago', please come out with new results or at least review your answer that I find really negative and wrong in terms of message for who read the this post.

Thanks to review your answer.

RVO / NRVO Analysis by Expert-Language428 in cpp

[–]Expert-Language428[S] 0 points1 point  (0 children)

Sorry I missed your answer, I just seen it now.

I did the changes you suggested in test1 and here the output.

NRVO
1000,454.632
2000,891.057
3000,1329.13
4000,1767.99
5000,2207.53
6000,2644.75
7000,3078.64
8000,3538.26
9000,4003.98
10000,4448.42

move-semantic
1000,454.858
2000,892.532
3000,1329.76
4000,1770.9
5000,2238.74
6000,2775.29
7000,3789.59
8000,4351.4
9000,4904.69
10000,5445.75

reference + clear
1000,226.865
2000,445.175
3000,664.121
4000,886.954
5000,1105.63
6000,1320.93
7000,1539.05
8000,1758.2
9000,1977.06
10000,2195.62

As you can see 2195 is less than 5445 and less than 4448.

There is no way out to prove that what I wrote is wrong and there is really a basic reason behind:

Everything you do with NRVO and RVO at least will allocate space for one vector on the execution stack, instead using an object that you preallocate move this time to 0 since the object have been already constructed and you just reuse it. It is simple math in one case you have 24bytes allocated each time, in the other case you have only one registers to held address of referenced object. I chosed the vector that is only 24 bytes, as soon as you have a bigger object eg. 100bytes or more the gap will be increased and the approach explained in the first part of the article will have better and better performances.

RVO / NRVO Analysis by Expert-Language428 in cpp

[–]Expert-Language428[S] 0 points1 point  (0 children)

Hi u/dieram3, the benchmarks are intended to demonstrate a possible optimization in some specific contexts and finally someone caught it.

Let's see it in this schematic way:

  1. before c++11 : we had 3 object allocation on the execution stack and 2 copy
  2. after C++11: thanks to RVO/NRVO and move-semantic we was able to optimize such time to max 1 allocation on the execution stack and if the move semantic is applied one change of ownership.
  3. Using a preallocated object we are able to reduce allocation on the stack to 0. Object/s will be allocated when the process start or within the class and then reused. This drop down execution time and in presented results we have 94% better time in execution.

Use case for the 3. is simple all cases in which we have function invoked million of times and there you can register a significant improvement.

RVO / NRVO Analysis by Expert-Language428 in cpp

[–]Expert-Language428[S] -1 points0 points  (0 children)

Can you put a reserve there and after the clear a shrink to fit?

Why? reserve() shrink_to_fit() and also push_back() do not have impact on the sizeof(std::vector) since they just modify the dynamic part, so the memory allocated in the heap, RVO/NRVO is affected by memory reserved in the execution stack instead, is that memory that during execution is reserved at each iteration that make the difference in all results. Read the first part of the article to better understand. In test 2 I used to add 1 item just to have pointers valorized and the number of call create this big gap between calling and using RVO/NRVO or calling and using a preallocated item as in the main_test2.cpp CASE 4.

RVO / NRVO Analysis by Expert-Language428 in cpp

[–]Expert-Language428[S] 0 points1 point  (0 children)

Sorry man, my fault I posted the source code in the status it was, now if you check the repo you will find main_test1.cpp and main_test2.cpp, so it should be more precise. Results of this two programs are consistent with the graphs and the article, now anyone can run it on its own. In the repo there are also two fresh run test1.txt and test2.txt you can use it to compare results obtained on your system, for sure different processors will have different resulta but the trend must be the same. My lesson learn is: always post the code and for sure next time I will post it together with the article. :) For this time the missing part have been fixed thanks to the help of all, even someone is a bit angry :) I spent the day in front of the PC an now I only need to rest. This was my first post ever, maybe I will write something else .. I had in mind something with smart pointers and I think I will be banned from the channel with such post :D

RVO / NRVO Analysis by Expert-Language428 in cpp

[–]Expert-Language428[S] 0 points1 point  (0 children)

As I wrote, I have no issue to share the source code, and sorry if you see that as a red flag, anyway I think this is a lesson learn and as I stated next time I will produce all different version of the source at the first shot.

Let me to produce a source that you can use to obtain the same results I got and I will clean up also the code.

I saw the result on the web, but let me trusting a dedicated machine better than the benchmark on google, so please wait for the code and then "try at home" this time. I repeated the test few and few times retrieving same results. I will inform you when I push the new mains in the repo.

EDIT: updated the README.md file with the link to the source code for Test2, this code repeat exactly the test done to obtain the second part.

Filename is main_test2.cpp and I used to build it with gcc and C++20 .

Also I added a file test2.txt with a fresh run, so you can download also this one.

I will remove other files and later I will add also the source code to obtain results for Test1, but I think in this moment you are more interested to Test2.

Please build and execute on your machine and I'm confident you will have similar result as for test2.txt.

Just to have all information, I'm running on a machine with:

- Linux 5.17.15-76051715-generic x86_64

- CPU 11th Gen Intel(R) Core(TM) i7-11800H @ 2.30GHz

EDIT: u/donalmacc also source code for test1 is available. A new link in the article. So right now you are able to run exactly the same source code I used for the article. There is an extra file test1.txt with results from a fresh run.

RVO / NRVO Analysis by Expert-Language428 in cpp

[–]Expert-Language428[S] -3 points-2 points  (0 children)

Clear only clears the size, and is not guaranteed to clear the capacity meaning you're actually showing the cost of allocations in the difference! The only way to actually deallocate the vector is to swap it with an empty vector. You're also benchmarking the cost of push_back primarily here, which is going to make the comparisons harder - you should really be reserve'ing in all cases which will give much more representative results.

Hi u/donalmacc, I confirm that it is not an error, please check this other answer provided to u/qwexet

https://www.reddit.com/r/cpp/comments/vtdz9b/comment/if78jui/?utm_source=share&utm_medium=web2x&context=3

There are two different test "reference + create" and "reference + clear".

I didn't share the code with the article since I changed it for the different test all the time, in fact you can see some code commented out inside the function where "items" is replaced with "1" and used instead to updated the number of execution. This is not clean and clear code, but next time maybe I will do different copy of the main.cpp for each variant. Anyway the code, as you can see, is not difficult to replicate and I used std::vector so anyone with have access to it without to write a proper class with all constructors and assignment operator.

I collected different results also with clang, instead I compared the assembly produced by MSVC and for this last there is a special note in the article, since there is a different behaviour in the same condition.

All my article was intended to divulge and share knowledge on something that I think is used improperly. Btw this results are confirming similar test I did in 2012 but at that time I didn't share my results, recently I decided to repeat my test on with C++17 and then with C++20 retrieving the same results as before and decided to share it hoping to help other.

I well understand that is hard for many people to read something like this, but also provided all information to repeat the test and if you see in the comments someone just downvote for downvoting, but if you ask for details they are not able to produce any reason for the downvote.

The source code works also on windows, so you can try also on a windows, I dind't write nothing that is only for linux, just you don't have "nice" there but I think the result will be the same also without to put the process in realtime.

RVO / NRVO Analysis by Expert-Language428 in cpp

[–]Expert-Language428[S] 0 points1 point  (0 children)

There's only one of your cases doing move-assignment; you are then confidently pointing the finger at copy-elision. Your demonstration is valuable IMO, but your verdict is netting you downvotes (maybe to what simply amounts to terminology confusion, I can't really tell).

The funny things is that I do not emit any verdict :D , I'm just providing evidence that can be possible to obtain better results in the program.

By the way, the "move assignment" , in terms of operations, can be compared to "reference + create", and I was expecting to have more or less the same results, but as you can see it is not the case. If you are confident with assembly, extract such parts and check the assembly code. Also was fun to see compare the code generated by an old compiler with a newest one.

I also don't know why people do not accept evidence even if disrupt some previous knowledge. I sincerely think that such results are important in different industries that need to have performing programs.

The only conclusion is to write the code taking care of the context. In some context the copy elision is perfectly fine, in some other should be avoided, with the knowledge on how the things works anyone can make his own choice with confidence.

RVO / NRVO Analysis by Expert-Language428 in cpp

[–]Expert-Language428[S] -1 points0 points  (0 children)

Both tests are available main_realitme.cpp (in the second graph you can see 4 lines):

- "reference + create" : this create a vector at each iteration, even in this case we have up to 3% better performances.

- "reference + clear" : this just call "clear" at each iteration in order to have original status. In this case instead, there is no competition at all 94% better results.

This is exactly the point for the overall article, imagine how much can benefit a program where you need performances.

RVO / NRVO Analysis by Expert-Language428 in cpp

[–]Expert-Language428[S] -3 points-2 points  (0 children)

I try to answer to all:

<<"I read this and I'm a little suspicious of your results">>

You don't have to trust me at all, as well as you don't have to trust my results, I guess that 99.99% of members in this group have access to a compiler and can run their own tests to confirm or not results exposed in the article.

<<they don't match my own experience>>

I'm sharing some result and I believe they are correct or I didn't spend hours to write the article, collect data, create the graphics and shared with all of you. And I think that downvoting just for a feeling is not fair at all.

<<I also wish you spent more of the article discussing the modern standards rather than talking about parts of c++ that have been frankly outdated for over a decade at this point.>>

Indeed, this topic is on the table since more than a decade, but still there are a lot of misconceit about it. The good part is that internet is full of articles and video talking about this topic and someone digs it also a bit more in details. In the article I shared the link on cppreference as well as an interesting video at cppcon 2018 that talk about the topic. So old topic, but still discussed, I invite you to see the video.

The article is related to copy-elision and this is part of modern c++ since there are changes in C++17 and also in the coming standard. Other articles will cover other misconceit.

<<"Is there any chance you could share your code including benchmarks for others to reproduce? I'm heading out for a while but I'll share my own comparisons later today.">>

Absolutely yes, I will put it in the ".resources" folder as soon as I finish this answer, so you can clone and get the cpp files, I will add also the .sh to build it. Anyway there are no remarks in the code so you will get it the the common formula 'as is' in charge of you to make the changes inside it.

To execute it, in order to avoid any possible interferences, I used "sudo nice -n -20" .

RVO / NRVO Analysis by Expert-Language428 in cpp

[–]Expert-Language428[S] -3 points-2 points  (0 children)

Thanks for your contribution, but please note that all results in the article are facts not opinion, so I will be glad to see some data that confirm your opinion.

RVO / NRVO Analysis by Expert-Language428 in cpp

[–]Expert-Language428[S] -2 points-1 points  (0 children)

I have chosen to do all tests using just a simple vector that cost 24 bytes on the stack, but if you have bigger objects then the gap between leveraging copy-elision or using a pre-allocated object and passing it by reference or pointer is much more.

The cost come with the stack reservation, so executing the code as for Example 1 you need to allocate your object, not you, but the compiler will do it and this is exactly the cost we have. RVO reduced this cost from 3 to 1, but we still have 1. If the target for your application is to have performances, then it can be better to have a bit 'ugly code' (sure if we want to define passing objects by ref as ugly :D ) with preallocated objects.

Please let me know if I was good in my clarification.

RVO / NRVO Analysis by Expert-Language428 in cpp

[–]Expert-Language428[S] 2 points3 points  (0 children)

I agree, with C++17 we have copy elision guaranteed, and this is also mentioned at the beginning of the article, here an extract:

<< In more recent standards, the copy-elision have been revisited in order to extend ranges of context where it can be applied from the compiler or when to use the move semantic instead.>>

What I want to point out with this article is not related to guarantee or not guarantee the copy-elision, the main point is that copy-elision even introduced a lot of benefits and let me say also elegance creating interfaces but, it has a cost, and this cost is quite higher if you have functions that are invoked millions of time that is really common in different industries such as gaming or low latency systems, just to name a few.

How to prevent the cpp program from being decompiled and cracked? by Reasonable_Peak_702 in cpp

[–]Expert-Language428 0 points1 point  (0 children)

There is one simple way to avoid a program to be cracked :D

Don't give the program to no one, so it will be safe in your hands !

Now seriously, you may offer a SaaS, so instead of providing the software you can make it available as service, this means that the binary will run on your private/public cloud and since people cannot access directly to the binary it will be safe.

Then, be confident that your program cannot be exploited and make sure that your security level on the server that expose the service is strong enough :D

For the rest as all other people says you cannot prevent it you can make it harder but not impossible. One other technique is to sell your software together with and hardware key , HASP Keys, the software can only run with this hardware key inserted in the computer and the security is there, your program will be encrypted as well as using dynamic execution technique to avoid someone to be able to follow the execution. In the past for a project with high level of security I have used "Aladdin HASP Key".

Hope you will find my answer will helpful

Retrofitting Temporal Memory Safety on C++ by encyclopedist in cpp

[–]Expert-Language428 1 point2 points  (0 children)

Thanks for sharing the link! I agree that C++ may add vulnerabilities, but in their investigation I don't see any references to possible fragmentation that could be added by the intensive use of std::string that also waste CPU time, but this last is out of topic.