6 ms·
I did not know std::shared_ptr would not slow things down. I've learned something new today! :) Yes, I agree, epoll is a lot better than FD_ISSET. Maybe I can
by sneilan1 2y ago
I did not know std::shared_ptr would not slow things down. I've learned something new today! :)
Yes, I agree, epoll is a lot better than FD_ISSET.
Maybe I can keep moving with my C++ code but do people still trust C++ projects anymore? My ideal use case is a hobbyist who wants a toy stock exchange to run directly in AWS. I felt that C++ has a lot of bad publicity and if I want anyone to trust/try my code I would have to rebuild it in rust.
- shaklee3 2y agoThat's not true. It does slow things down because it has an atomic access. How slow depends on the platform. unique_ptr does not slow things down.
- worstspotgain 2y agoHere's how to maximize shared_ptr performance: - In function signatures, use const references: foo(const std::shared_ptr<bar> &p). This will prevent unnecessary bumps of the refcount. - If you have an inner loop copying a lot of pointers around, you can dereference the shared_ptr's to raw pointers. This is 100% safe provided that the shared_ptr continues to exist in the meantime. I would consider this an optimization and an edge case, though. I would say people trust C++ projects at least as much as any other professional language - more so if you prove that you know what you're doing.
- Maxatar 2y ago> In function signatures, use const references: foo(const std::shared_ptr<bar> &p). This will prevent unnecessary bumps of the refcount. This advice doesn't seem quite right to me, and in my codebases I strictly forbid passing shared_ptr by const reference. If you don't need to share ownership of bar, then you do the following: foo(const bar&); If you do need to share ownership of bar, then you do the following: foo(std::shared_ptr<bar>); Why do we pass by value when sharing ownership? Because it allows for move semantics, so that you give the caller to option to make a copy, which bumps up the reference count, or to entirely avoid any copy whatsoever, which allows transfering ownership without bumping the reference count. Having said that, shared_ptrs do have their uses but they are very very rare and almost all of our use cases do not expose shared_ptr's in the public API but rather use them as an implementation detail. We use them almost exclusively for things like immutable data structures, or copy-on-write semantics, or as a part of a lock-free data structure.
- a_t48 2y agoIf you _maybe_ need to share ownership, the second is a little pessimistic - you always increase the ref count.
- Maxatar 2y agoThat is correct and I can see that being a justification for passing a const&, in fact the C++ Core Guidelines agree with you that such a scenario is the only acceptable reason for passing a shared_ptr by const&, although they encourage passing by value, or just passing a const T&.
- a_t48 2y agoObviously the correct way is to accept a templated type and use perfect forwarding. /s /s /s /s /s
- worstspotgain 2y agoBonus points for using static_cast<T &&>(p) instead of std::forward<T>(p) ;)
- worstspotgain 2y agofoo(const bar&) is ideal if you precisely wish to bar ownership. If (and in many kinds of projects, invariably it's more like when) you later decide to share ownership, or if nullptr is an option, then it's no good. foo(std::shared_ptr<bar>) is copy-constructed as part of your function call (bumping the refcount) unless copy elision is both available and allowed. It's only ideal if you almost always pass newly instantiated objects. Pass by const reference is the sweet spot. If you absolutely must minimize the refcount bumps, overload by const reference and by rvalue. As for shared_ptrs being very rare, uh, no. We use them by the truckload. To each their own!
- CyberDildonics 2y agofoo(const bar&) is ideal if you precisely wish to bar ownership. What? invariably it's more like when) you later decide to share ownership, shared_ptr shouldn't even be necessary for keeping track of single threaded scope based ownership. As for shared_ptrs being very rare, uh, no. We use them by the truckload. To each their own! You might want to look into that, you shouldn't need to count references in single threaded scope based ownership. If you need something to last longer, make it's ownership higher scoped. If something already works it works, but this is not necessary and is avoiding understanding the actual scope of variables.
- fooker 2y agoReference counting definitely slows down tight loops if you are not careful. The way to avoid that in low latency code is to break the abstraction and operate with the raw pointer in the few areas where this could be a bottleneck. It is usually not a bottleneck if your code is decently exploiting ipc, an extra addition or subtraction easily gets executed while some other operation is waiting a cycle for some cpu resource.
- pjmlp 2y agoC++ might have a bad reputation, but in many fields the only alternative, in terms of ecosystem, tooling and tribal knowledge is C. Between those two, I rather pick the "Typescript for C" one.
- chipdart 2y ago> I felt that C++ has a lot of bad publicity and if I want anyone to trust/try my code I would have to rebuild it in rust. C++ gets bad publicity only from evangelists of the flavour of the month of self-described "successor of C++". They don't have a sales pitch beyond "C++ bad" and that's what they try to milk. And yet the world runs on C++.
- rmlong 2y agostd::shared_ptr definitely slows things down. It's non-intrusive therefore requires a memory indirection.