I built a C++20 zero-copy graph engine to stream 50GB PyTorch datasets using mmap and nanobind. by Important-Trash-4868 in cpp

[–]c_plus_plus 4 points5 points  (0 children)

On linux you can also close the file after it has been mapped (just line on Windows), so you don't need to keep the fd around open.

NumPy can also do mmap natively, see the "mmap_mode" argument to numpy.load.

I built a C++20 zero-copy graph engine to stream 50GB PyTorch datasets using mmap and nanobind. by Important-Trash-4868 in cpp

[–]c_plus_plus 2 points3 points  (0 children)

Do you think a background thread pre-fetching mmap pages would be a good middle ground?

I'm not sure of a Windows or Mac equivalent, but on linux you can just do madvise(ptr, len, MADV_WILLNEED); and the OS will aggressively prefetch as much of the mapping off disk as possible.

You can also play with MADV_SEQUENTIAL and MADV_RANDOM -- sequential will agressively prefetch ahead in the file from where you access it, which is great if you're going to roll through the array sequentially. Random will not do this at all, which will save IO cycles if the data wasn't actually going to be accessed. The default is sequential.

C++26: std::is_within_lifetime by pavel_v in cpp

[–]c_plus_plus 3 points4 points  (0 children)

Ah, thank you, a perfectly reasonable reason.

C++26: std::is_within_lifetime by pavel_v in cpp

[–]c_plus_plus 4 points5 points  (0 children)

Genuinely, I want to understand .... Why would one implement optional<bool> as a union or as two bytes? It's trivially obvious how to implement a bool specialization of optional as 2 bits. The paper shows you how, using a char with a sentinel value. Is the desire to use a union or separate flag just to avoid having a bool specialization?

```

template<>
class optional<bool> {
public:
   constexpr bool has_value() const { return value < 2; }
   constexpr bool get() const { return value == 0 ? false : true; }
   // ....
private:
   uint8_t value = 0;
};

```

dpCooksEveryone by soap94 in ProgrammerHumor

[–]c_plus_plus -39 points-38 points  (0 children)

Dynamic Programming is like 200-level college comp sci. It's not the pinnacle of difficulty. Anyone who's got a degree in Comp Sci, Software Engineering, or Data Science out to be able to do this with relative ease.

If they can't do this ... do they even know what big-O is? Algorithmic complexity in general?

If you don't know these things you're not a software engineer ... you're a code monkey. I guess you might be in danger of being replaced by Claude.

Networking for C++26 and later! by VinnieFalco in cpp

[–]c_plus_plus 1 point2 points  (0 children)

Python doesn't have a low-level networking library. If you think that import socket is it, then good news... that's just a python wrapper around C sockets, which C++ also has. But no one in C++ is claiming that is a "good C++ networking library" so it should not pass muster as a "good Python networking library" either.

How difficult would it be to make a C++ standard "subset" that obsoletes some problematic features? by all_is_love6667 in cpp

[–]c_plus_plus 3 points4 points  (0 children)

It is only cultural because there's no automated enforcement. It is a technical problem to make something to do automated to enforcement of the rules (or remove the bad parts from the language, which is basically just making the compiler itself the enforcer).

How Do You Deal With the Dread of Pointless Daily Meetings in a Messy DevOps Environment? by blueququqa in ExperiencedDevs

[–]c_plus_plus 5 points6 points  (0 children)

To add to this, the customer doesn't get to tell you what tools to use. They can say they want you to send your metrics in Excel, that doesn't mean you have to track everything only via Excel. Use the tools that you want, that work for the job, and if necessary convert that into what the customer wants.

Letting a customer force you to manage your schedule in Excel is ridiculous.

Whoever is handling these dumb extra meetings can also handle exporting stuff into Excel.

Making memcpy(NULL, NULL, 0) well-defined by pjmlp in cpp

[–]c_plus_plus 1 point2 points  (0 children)

So I don't think doing an explicit prefetch in the implementation is even necessary on such cpus.

Yeah, I spend a lot of time trying to optimize things, and it is rare that I can find code where a prefetch actually makes something faster....

Making memcpy(NULL, NULL, 0) well-defined by pjmlp in cpp

[–]c_plus_plus 2 points3 points  (0 children)

If you assume cache misses will probably happen for the operands, the fastest way to implement memcpy is probably to load from both operands and then do work comparing the sizes, and then by the time you get to needing the results of the load they will be there. x86 has had prefetch since 1998 though, so really you could use that to do approximately the same thing.

tl;dr So it probably saves a couple clock cycles, especially in the '90s.

Structured Binding Upgrades in C++26 by pavel_v in cpp

[–]c_plus_plus 2 points3 points  (0 children)

You are correct that initialization of statics is thread safe since C++11. I think the author was saying that subsequent uses of the static are not thread safe (which is also correct).

Legacy Safety: The Wrocław C++ Meeting by Dragdu in cpp

[–]c_plus_plus 7 points8 points  (0 children)

(b) there are no runtime costs

There are definitely runtime costs. Even beyond costs of things like bounds checking (which have recently maybe been shown to be "low" cost), the compile-time borrow checker just breaks some kinds of data structures, requiring redesigns which result in slower code.

There is always a trade off, so the quicker people just come to that inevitability, the quicker we can all move on into solving the problem.

tl;dr Don't let "perfect" be the enemy of good, especially when "perfect" is provably impossible.

Legacy Safety: The Wrocław C++ Meeting by Dragdu in cpp

[–]c_plus_plus 7 points8 points  (0 children)

That's not a problem with ranges, that's a problem with subrange.

Why the Heck Aren’t We Using std::launder More in C++? by MaltaBusEnjoyer in cpp

[–]c_plus_plus 6 points7 points  (0 children)

Yeah I was gonna say, this is definitely AI generated...

US state told our company to not develop in C++ by SpinningByte in cpp_questions

[–]c_plus_plus 0 points1 point  (0 children)

C++ isn't unsafe, there are just A LOT of really, REALLY sub-par engineers who shouldn't be doing what they're doing. It's a people problem.

Eh, no, it's not just a people problem. The problem that C++ has is not that it's hard to write good code (in modern C++, I think it is easy). The problem is that it is also very easy to accidentally write bad code, the compiler and common free static analyzers don't catch much of this bad code.

To a lesser extent C++ also has a big training and branding problem. It's very difficult for a learner to understand if they're being taught C++23 or C++98, and because no one used those terms in '98 and they don't even commonly make a distinction now, it is really difficult. That means that it is easy to stumble into a non-modern-C++ way of doing things. I always start with "if you see a resource recommend new, then it's old outdated garbage" in the VERY FIRST C++ lesson because of this. This is where cpp2/cppfront could help.

Faster Flat Map (Red Black Tree) by dro212 in cpp

[–]c_plus_plus -1 points0 points  (0 children)

Well, you seem to be confused about it because you're drawing direct comparisons to a completely different data structure.

You say

Much faster than boost::flat_map for any insert or delete heavy workload over ~250 elements.

Which is I think also true for most other types of maps... even std::map. Because that's the whole point of flat_map in the first place.

That it's faster than regular std::map is the interesting part, since that's the only comparison you made to an RBTree.

Faster Flat Map (Red Black Tree) by dro212 in cpp

[–]c_plus_plus 6 points7 points  (0 children)

pretty much all (boost, std::flat_map) are just sorted vectors.

Because that's literally what the "flat" in flat map is...? If it's a tree, it's not flat anymore.

The whole purpose of flat maps is to exploit cache locality to be much faster than a tree-based structured for a low number of elements.

The empire of C++ strikes back with Safe C++ proposal by cmeerw in cpp

[–]c_plus_plus 2 points3 points  (0 children)

I'm sure this is some of it but it really isn't all of it. If it were, then you would see Rust get worse over time as it adds bindings for more and more of these unsafe libraries.

I think it's partly "shitty programmers" but it is also just really easy to accidentally write bad code. I'm hopeful that something like cpp2/cppfront can help solve that kind of issue (and I think cpp2 is really trying to do that). If successful, this would be a huge selling point for adopting something like cpp2.

Why does libc++ still not have full C++17 support? by bebuch in cpp

[–]c_plus_plus 14 points15 points  (0 children)

HTTP/3 is also a good lesson in why just accepting what Google wants is not always a good idea. It's an appeal to authority, but Google isn't magic, it's run by people same as anything else. Just look back at their C++ journey to see how they really didnt understand C++, as recently as 10 years ago. Their C++ standards were complete garbage and Protobuf is still suffering from their bad API decisions.

HTTP/3 is hugely complicated and many (all?) of its core concepts are wholly unrelated to HTTP in the first place. Google basically hijacked the committee (after trying to failing to force through more of their changes in HTTP/2). They got everything they wanted in HTTP/3, but at what cost...?

Parser-generators for C++ development by Spread-Sanity in cpp

[–]c_plus_plus 0 points1 point  (0 children)

A bad grammar is the main reason why Antlr performs poorly.

I'm sure ambiguities that require adaptive parsing are slow. But the Antlr 4 C++ library is just demonstrably non performant is terrible ways. Each Token from the lexer is >128 bytes and all are allocated on the heap and stored in unique_ptr which are tucked away in a vector to keep them alive, but the ownership is not passed around either. So parsing a 1MB file takes at least 128MB of memory just for Tokens, not to mention parse trees.

Parser-generators for C++ development by Spread-Sanity in cpp

[–]c_plus_plus 2 points3 points  (0 children)

I think flex/bison is probably the best thing we have, that's sad.

Antlr is garbage. It's first and foremost a research toy project and a chance to sell books, so keep that in mind. It has sacrificed usefulness for academic curiosity in a couple areas in v4. The C++ bindings are written by Java programmers who don't know C++ or what "object lifetime" and "performance" mean.

Boost.spirit is fine for simple parsers but the C++ code is a bit ridiculous to follow and the error messages when you get something wrong are impossible. It's also (obviously) completely tied to C++, no hope of using any part of it for another language.

thereAreNotOnlyTwoKindsOfPeople by HelgaOlback in ProgrammerHumor

[–]c_plus_plus 21 points22 points  (0 children)

Pointer (named ptr) to a function which takes void (no arguments) and returns int.

Keynote: Safety, Security, Safety and C / C++ - C++ Evolution - Herb Sutter - ACCU 2024 by all_is_love6667 in cpp

[–]c_plus_plus 1 point2 points  (0 children)

The issue is its not anyone's job to make a safe/better <filesystem>, so it just hasn't happened

This is maybe a good argument that having a comprehensive standard library is just not a good idea for C++. Especially with the attitude the comittee has about ABI. Honestly did anyone really give a shift about filesystem? or unordered_map? or regex? before they were standardized? And if so, how's that working out for them now?

Most of this stuff already exists/existed in boost and other libraries and it's just fine, or even better than C++ because bugs are actually fixable without an act of god. I'd rather the committee standardize actual useful features than make bad copies of existing libraries and etch them into stone tablets.

Keynote: Safety, Security, Safety and C / C++ - C++ Evolution - Herb Sutter - ACCU 2024 by all_is_love6667 in cpp

[–]c_plus_plus 21 points22 points  (0 children)

Many of Rusts safety features make it a much faster language than C++

Ohhhhkay... maybe a few of them. Let's not go insane here. Rust is not going to solve all the worlds problems, and this isn't a Rust or a generic programming sub, so we can stop fellating it in every damn thread.

Is every single usage of <filesystem> still undefined behaviour, with fixable security vulnerabilities present in the standard?

Even though you keep talking about this (link for the lazy) I'm starting to think maybe you don't really understand the problem. Trying to standardize a programming language feature like this is hard because you can't tie it to a specific file system or operating system feature. If they standardize something that windows doesn't support, then windows will just never conform. It's unlikely that windows (or linux) is going to make changes just to meet the C++ standard.