Better HN
topnewbestaskshowjobs
Log inSign up

writebetterc

135 karma · joined Jan 16, 2025 (1y ago) · 77 submissions

Submissions

comment 1y ago · link · parent

It's a bit annoying that there is a standard desugaring of all of this, but there's no actual way of implementing this sugaring yourself. I guess you could have a token macro (or whatever they're called) at the top scope and do it yourself?

Anyway, my conclusion is that in order to learn Rust you can't use any syntactic sugar the language provides, those are 'expert features'. There's no way I'd be able to figure out why this happens. Do rust-analyzer provide a way of desugaring these constructs?

comment 1y ago · link · parent

>Benchmarks show its about 25% faster than the raw pointer version. (I don't know why - but I suspect the reason is due to better cache locality.)

Cache locality matters, but so does having less allocator pressure. Use 32-bit unsigned ints as indices, and you get improvements on that as well.

>The original version of my b-tree works just like how you'd implement it in C. Each internal node / leaf is a raw allocations on the heap.

I'd always try to avoid that type of allocation pattern in C++, FWIW :-).

comment 1y ago · link · parent

You're wrong :-). I hate that stuff, and I'm technically not even in my 20s anymore!

comment 1y ago · link · parent

REPL-ify your command line then? There's nothing that says you have to be stuck on bash for your command line needs. https://www.nushell.sh/

comment 1y ago · link · parent

IDEs understand preprocessor macros, so IDE features (jump2def, etc) work with this. IDEs also can expand the macro invocations. So, I prefer the macros when possible :-).

comment 1y ago · link · parent

Why isn't the conclusion "There is a far better way of using C, which the stdlib doesn't promote... But could"? The fact of the matter is that any sufficiently large C codebase will do this stuff anyway, it's not a language issue.

Good Rust code will also care about memory allocations to the same degree as the C code, the difference is that Rust will help you out in making sure your thinking is correct. My experience is that good systems programming has thinking about memory allocations not as an annoying side issue, but as a main concern.

comment 1y ago · link · parent

This way of writing programs is also quite a lot faster than depending on fgetline and the like. The integer and float parsing is probably slow, though.

My question is: Does the author actually use Windows XP?

comment 1y ago · link · parent

Ain't nothing wrong with Swing or JavaFX :).

comment 1y ago · link · parent

Good, but that can also be noted in a subtitle right below the authors (I've seen this in papers myself!)

comment 1y ago · link · parent

No reason to be rude, it was really not clear that you knew that as null pointers have nothing to do with memory models.

comment 1y ago · link · parent

I believe that the parent is talking about how Java, and in turn the JVM, defines its memory model, which describes formally how memory reads and writes occur in the presence of multiple threads of execution. For example, data races are well-defined in Java: Either you read the old or the new value, you'll never read a mix of two values.

Having a well-defined memory model is important when running on infra with very different models, such as x86 and aarch64.

comment 1y ago · link · parent

Don't use Fedora if you have a nvidia gpu, or if you have ample of free time.

comment 1y ago · link · parent

>pioneer something when somebody pioneers something, they are one of the first people to do, discover or use something new.

comment 1y ago · link · parent

> In fact, it was the United States that pioneered this in the modern age.

This was instituted in Sweden in the year 1766. Source: https://sv.wikipedia.org/wiki/Offentlighetsprincipen

That's 10 years before the USA declared independence.

comment 1y ago · link · parent

You sure can write C which makes you less prone to making memory safety mistakes, yes. Knowing how to do that is a skill issue. As you say, sometimes our skills fail us, and that's where tooling comes in. ASan, borrow checker, whatever. There's a reason that a lot of Rust programmers find their programs filled with Arc<Mutex<T>>. That's because they didn't have the skills to write memory safe systems programs in the first place.

To be clear, of course I'm also not immune to fucking up :-).

comment 1y ago · link · parent

Memory will always be the limiting factor here. A strong adversary can ensure that you must know the whole list in order to sort it.

You can always mmap the file instead and let the OS page in and out parts of the file, however :-).

comment 1y ago · link · parent

This post caused me to create an account. This C code is not good. Writing C is absolutely harder than Python, but you're making it so much harder than it has to be. Your program is buggy as heck, has very finicky cleanup code, and so on.

Here's a much easier way to write the program:

1. Dump whole file into buffer as one string

2. Find newlines in buffer, replace with NULs. This also let's you find each line and save them in another buffer

3. Sort the buffer of all the lines you found

4. qsort the buffer

5. Print everything

6. Free both buffers

Or, as a C program: https://godbolt.org/z/38nq1MorM

123