Past several years was different financial applications but I am looking at getting into trading engines.
I'm not familiar with what financial applications really do, but if you imagine some kind of web server, you could define a pool of memory blocks used for each request, the number of items in the pool being the max number of concurrent requests, and the size of each block being the max memory each request can use. Then you can take a block from the pool when a request comes in, wrap it in an allocator, and allocate all request-related data into it, then you just reset it or zero it after the response is sent
I understand your scheme. However, remember, the necessity of RAII is more than clean resource deallocation at the end of normal execution. It also protects against exceptions that might abort the execution of your operation, before your clean deallocation at the end of normal execution. That, and sometimes some developers have multiple points of return, which if not careful about, might lead to several resource leaks. A big part of the C++ std library depends on RAII; the std containers, smart pointers, I/O facilities, etc. Same with the most trusted C++ libraries outside std. RAII is the reason we can allocate resource handles on the stack, simplifying code, instead of dealing with pointers every time or the disposal of the resources managed by those handles. For safe, clean, high quality C++, RAII is indispensable.
I don't use exceptions either heh
resource handles on the stack through RAII doesn't make sense to me
I also don't use C++ stdlib much
So there's another language feature that C++ lacks that solves the multiple points of return thing
I like exceptions quite a lot in other languages but not sure they can be as good in C++ if you like C-style
I think any language where the type system doesn't know about them they're real bad
It does, 100%. Thing is, constructing objects on the stack in C++ guarantees that their destructor will be called on scope exit, no matter how that exit occurs. Not the same with heap allocated objects; the deallocation has to be done manually, which is a breeding ground for error. Therefore, with RAII, the allocation of the resource is accomplished in the constructor of the handle-like class, while the deallocation is performed in the destructor. This handle object then becomes a convenient interface to using the resource safely, cause the second that handle object goes out of scope, no matter how, it's destructor will be called, thus properly deallocating and disposing the resource. This is exactly what the std library containers, smart pointers, etc are doing.
If you did, you would know how prevalent and beloved RAII is in C++. Some years back, several C++ authorities/experts spoke AB their favourite C++ features. The most prevalent was a code example, simply, } Yes, that one curly brace was favourite feature. Why? Cause that signifies end of scope, meaning guaranteed calling of destructors for stack objects. RAII is an idiom born out of that.
I don't really care about this, since I don't find myself making the mistake of not releasing resources, and you could do a bulk approach to this too
Most C++ devs I follow hate it and don't use RAII heh
This is the error of overconfidence, actually. Even seasoned professionals occasionally make the simplest mistakes. You are also not considering the factor of dealing with other people's code, especially in large code bases. And no, bulk approaches are not ideal for a large many developers for their needs; you may as well suggest garbage collection. One of the reasons people choose C++ is it guarantees deterministic destruction of local objects; you know exactly when it will happen, and we take advantage of that. This is the backbone of RAII.
I don't think you understand what I mean by bulk approach, I'm talking about collecting file handles into a container and closing them all at once
Try a language which conventionally uses defer for closing handles, and you'll see it's hard to make the mistake
Yes and the bulk container handles the closing because of RAII. So RAII is a useful concept that you will eventually end up using anyway
The C++ devs you follow are plainly not familiar with the actual strengths of C++. Follow the C++ devs that are actual recognized authorities in the community, who are part of the Standards Committee, who have driven the development of C++ for the past several decades, or the major C++ libraries. Not one would eschew RAII or be a proponent of manual resource disposal. Not one. Stroustrup. Sutter. Abraham. Parent. Sutton. Hinnant. Carruth. Lavavej (STL). Dos Reis. Voutilainen. Gregory. Winters. Niebler. Vandevoorde. Lelbach. Plauger. Austern. Dionne. Turner. Chen. Alexandrescu. Dusíková. Meyers. Berris. Josuttis. Gregor. etc. The above is but a small list of the most influential people in the community. I challenge you to find one who hates RAII.
No, you'd call the bulk container manually to close all
They're very familiar with it lol, they're mostly oldschool gamedev people
You are still keeping the resources open for longer than necessary. And, in C++, the example you gave will definitely be handled by a RAII class. Your example begs for a library solution. The class will have functions that will add the file handles to the container, then the destructor will do the actual container traversal to close the files. You have just given an example where RAII will provide a library solution. Look into thread pool implementations. How are the threads in the pool eventually all disposed?
No, I'd create a facility to do that, and it would be a RAII enabled facility.
Harder, not impossible. With RAII, its impossible to forget to dispose a resource. A smart pointer guarantees memory disposal. A file class guarantees file closing. A thread pool class will shutdown all threads at destruction. A container will dispose it's memory. A string class will clean up its resources when out of scope. With defer, you have to still specify what clean up routines to call. With RAII, I don't, as long the default works, which is most cases.
You are still keeping the resources open for longer than necessary. Sometimes you want that, closing can be a performance bottleneck (Windows) that you might want to defer to later
It's not impossible to forget with RAII, you could forget while implementing the destructor
If they really hate RAII, I honestly question their C++ expertise. And if they are "old school", I hope they are not stuck in a mindset of C++ that is closer to C, cause getting stuck in old mindsets and ways happens often. Game environments are usually constrained, which is why they avoid exceptions and use specialized versions of the std library. However, RAII can be used in any environment cause it is an idiom based on a core C++ feature, no matter the environment; guaranteed deterministic local object destruction at end of scope.
That is what pool classes are for. I mentioned thread pools in several messages above. Those pool classes will still be RAII classes in C++.
Notice the difference between RAII and defer here. With RAII, the error is if the RAII class is not implemented correctly. My original comment that led to this thread mentioned "a proper RAII interface." My use of proper meant implemented correctly. This is the case with the std library and most high quality C++ libraries. It is impossible to forget with such high quality libraries. With defer, it comes down to your memory. And that, coupled with more typing, is an excess cognitive load.
In my case I think it's an excess cognitive load to remember destructors when you can't easily see them in the calling code
In my case, it's an unnecessary cognitive load to have to remember to clean up resources if the language provides a way to do that behind a clean interface. This was one of the reasons for the fast adoption of Java and C#; not having to think about resource management.
Обсуждают сегодня