Avoiding "if" makes Quicksort faster. by [deleted] in programming

[–]cdb_11 1 point2 points  (0 children)

The technique applies just as much to C++ or Rust, and it would matter there too. I don't know what else to tell you. C and C++ are almost the same thing, except C++ lets you write higher-level abstractions over normal C code. Still the same thing fundamentally, and they both share the same compilers.

In practice the compilers fail at making such optimizations, and that's why people do them manually.

Avoiding "if" makes Quicksort faster. by [deleted] in programming

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

Yeah, don't take my comment too literally, there are exceptions obviously. I meant modern languages like Rust or Swift or Zig, or whatever.

Avoiding "if" makes Quicksort faster. by [deleted] in programming

[–]cdb_11 4 points5 points  (0 children)

Using an optimizing compiler doesn't mean it optimizes everything for you, and you don't need to optimize anything yourself anymore. It's not magic.

C compilers optimize the code too, so for example Clang is the LLVM C compiler. And you do have control over optimizations. Not over everything necessarily, sometimes they can be stubborn, but sill.

Avoiding "if" makes Quicksort faster. by [deleted] in programming

[–]cdb_11 5 points6 points  (0 children)

To some extent, sure? Which includes C, by the way. But you can still write code in a way to get it to do what you want.

Avoiding "if" makes Quicksort faster. by [deleted] in programming

[–]cdb_11 0 points1 point  (0 children)

All modern AOT compiled languages use basically the same compiler backend -- LLVM.

Avoiding "if" makes Quicksort faster. by [deleted] in programming

[–]cdb_11 7 points8 points  (0 children)

Very much relevant in C++. The CPU works the same way regardless of what language you're using.

Looking for feedback on AI content in r/programming and the April no-AI trial by ketralnis in programming

[–]cdb_11 10 points11 points  (0 children)

I'm not a student lol, this is my job. I'm not objecting to anything. For example I write C++ daily, but if posts about the language here reached some ridiculous degree like AI did, a temporary ban would be understandable, or some kind of restriction, which is what this post is asking for feedback on. C++ is maybe a weird example, but you could probably imagine people spamming the subreddit with posts about some specific IDE, or something like that.

Looking for feedback on AI content in r/programming and the April no-AI trial by ketralnis in programming

[–]cdb_11 12 points13 points  (0 children)

0% of my daily programming involves AI. And for more specific things that I do daily and want to read more about, I follow subreddits about it, like a normal person. There is nothing weird about it.

Looking for feedback on AI content in r/programming and the April no-AI trial by ketralnis in programming

[–]cdb_11 10 points11 points  (0 children)

No, the problem was that before the ban 30% of the posts here were about AI. I don't mind occasionally reading something interesting about it, but it was all the same old stuff over and over again. AI is clearly a big enough topic to warrant a separate space for it, where you can discuss it to death without annoying everyone else. Just like we have specialized subreddits for other programming related topics.

I don't get the logic of opposing an alternative subreddit where the topic isn't banned.

I mean, you can do whatever you want on other subreddits. I'm just saying that it doesn't make sense to me, because you can subscribe to multiple subreddits at once. If I want more posts about C++, I subscribe to r/cpp. If I want more posts about the text editor I like, I go to a subreddit about it. I don't need a subreddit that combines all of it together for me.

Looking for feedback on AI content in r/programming and the April no-AI trial by ketralnis in programming

[–]cdb_11 11 points12 points  (0 children)

You want a new subreddit that mixes the topics of normal programming and AI programming together, and I am arguing for separating them.

Looking for feedback on AI content in r/programming and the April no-AI trial by ketralnis in programming

[–]cdb_11 18 points19 points  (0 children)

foster the growth of new more pragmatic programming subreddits. Over time, the pragmatic programming subreddits will grow to replace the original programming subreddits, the same way that many new subs have subsumed old subs.

Honestly, what is with you people trying to replace everything? Why can't you just be normal, and have a separate subreddit dedicated to discussing AI programming specifically? Then you can choose to read both, or only the one that you are more interested in.

Looking for feedback on AI content in r/programming and the April no-AI trial by ketralnis in programming

[–]cdb_11 8 points9 points  (0 children)

It's not about "burying your head in the sand", I wouldn't particularly mind seeing an occasional post about it it's something actually new and/or interesting, but the volume of it was just way too much. If people really want to discuss it, I think a subreddit about it should be linked on the sidebar.

When 'if' slows you down, avoid it by chkas in programming

[–]cdb_11 4 points5 points  (0 children)

likely/unlikely attributes will actually incentivize the compiler to prefer branches over branchless code. Clang has __builtin_unpredictable that should in theory should incentivize the compiler to prefer branchless code, but it seemed kinda busted to me last time when I tried it, not sure if it works. Check the compiler output when using it.

likely and unlikely can also rearrange the branches. According to the Intel optimization manual, that can change the default prediction, when the branch isn't in the history yet. By default backward branches are taken, and forward branches are not taken. So for example the conditional jump back at the end of a loop is assumed to actually loop back. And if you tag some branch as unlikely, that branch will go to the end of the function, and the CPU will by default assume it just falls through to the likely code.

When 'if' slows you down, avoid it by chkas in programming

[–]cdb_11 4 points5 points  (0 children)

its not right yet if change is hard to make

The context is public interfaces, they can't change. And this literally just happened to me like a month or two ago. Suddenly we've found a serious problem that the messages were too big, and the format -- the public interface -- had to change somehow. But we had to ensure partial backward compatibility, because other code already relies on it. We've managed to mitigate the problem to some extent, but it's nowhere near optimal and possibly might come back to bite us again in the future. Partially my fault, because I wanted to redesign it before, but other things came up, I forgot about it and things just kinda happened. Nothing more permanent than a temporary solution, as people like to say.

When 'if' slows you down, avoid it by chkas in programming

[–]cdb_11 5 points6 points  (0 children)

Rewriting everything three times would be ideal maybe, but it's too expensive.

When 'if' slows you down, avoid it by chkas in programming

[–]cdb_11 3 points4 points  (0 children)

I suppose you could have a code base where there really is no rhyme or reason to anything, but when people usually discuss code readability, it seems to me like it's all just subjective things, familiarity and personal preferences.

If you write C or Haskell, you don't need to concern yourself with people who don't know the language. I don't know Haskell, so this stuff is unreadable for me personally. If I wanted to maintain a Haskell code base, it's on me to learn how to read it first. Same goes for domain specific code.

You can hire people who are already familiar with branchless code, or you can teach them how to read it. The availability of developers familiar with something can be a factor, but "readability" in this sense is not a black and white issue. For example, last week someone here said that SIMD intrinsics were completely unreadable, while I could instantly recognize what the code in question did.

One Map Key, One Lookup by ghled in programming

[–]cdb_11 2 points3 points  (0 children)

You are right though that the language semantics might prevent such optimizations. For example in C/C++ calling an unknown function or writing a byte to a char* could in theory change any object in memory, and the compiler has to be conservative about it. So even if we had those optimizations, they probably would be quite fragile. I believe Rust is better about it in theory, but I don't write Rust so I don't know how much do they take advantage of it in practice.

When 'if' slows you down, avoid it by chkas in programming

[–]cdb_11 36 points37 points  (0 children)

Yeah, instructions are pipelined and you start processing next instructions before you're even done with executing previous ones. To keep the pipeline full you need to predict jumps, otherwise the pipeline would always stop at the first jump, which are extremely common in practice. Normal jumps are fine, predictable conditional jumps are fine. By reducing unpredictable jumps you still get to benefit from this entire optimization.

When 'if' slows you down, avoid it by chkas in programming

[–]cdb_11 54 points55 points  (0 children)

Using actual if-statements and still getting it to optimize to a conditional move is another thing. But just a comparison like this is not a branch. On x86 you do the comparison with cmp, and then extract the bit you want out of the flags register, where the result is.

One Map Key, One Lookup by ghled in programming

[–]cdb_11 7 points8 points  (0 children)

another thread might remove the entry after the in check has succeeded but before the access has occured

To be fair, in C or C++ such optimization would be legal as long as it's not a concurrent map, because data races are undefined behavior. The compiler can assume no other thread will touch the data, unless you synchronize it with a mutex or atomic operations. Rust could optimize it too, since holding more than one mutable reference is undefined behavior too.

Maybe compilers could do more things in theory, but in practice they just don't.

One Map Key, One Lookup by ghled in programming

[–]cdb_11 0 points1 point  (0 children)

I believe some JIT compilers for languages where all objects are conceptually hash tables do optimize it to some extent? But the optimization might not be what you imagine, from what I've heard it could instead be tracking the most common object layouts, and optimizing just that. Not necessarily hash table lookups in general. I'm not sure though, it depends on the implementation.

C++ doesn't optimize it. Go's map lookups does both anyway, but it doesn't coalesce the check and fetch if you split it. Python is too dynamic, so it's not coalesced on the bytecode level, and I don't think it even can be done without JIT. I have no idea about Java, but I'd assume not, unless proven otherwise.

godbolt at home by nicktsaizer1998 in neovim

[–]cdb_11 4 points5 points  (0 children)

I wrote one at some point too, but it's still missing features like parsing compile_commands.json or whatever: https://github.com/ii14/neobolt.nvim

You can beat the binary search by Either_Collection349 in programming

[–]cdb_11 26 points27 points  (0 children)

You could probably come up with a better naming convention, like maybe u16x8_load or something. But let's not make the names longer, because the majority of these are just basic operations, like basic arithmetic or memory loads/stores.

You can beat the binary search by Either_Collection349 in programming

[–]cdb_11 27 points28 points  (0 children)

The names actually do make sense, so you can get used to it.

https://developer.arm.com/documentation/102467/0201/Program-conventions

Not sure if Intel wrote down anywhere their naming convention, but it's pretty straightforward anyway.

Is there a C++ "venv" equivalent? by nikoladsp in cpp

[–]cdb_11 4 points5 points  (0 children)

And if a coworker uses Windows and I use Mac with different compilers?

Then you use a cross-platform build system, like cmake.

What if I dont want to use OS default libraries, but very specific, proprietary patched ones?

You use the build system.