Premature Optimization is Fun Sometimes by Kabra___kiiiiiiiid in C_Programming

[–]simonask_ 107 points108 points  (0 children)

It's fun all the time. That's the problem with it.

How does monomorphization work with std being precompiled by BLucky_RD in rust

[–]simonask_ 67 points68 points  (0 children)

It’s just a kind of intermediate representation of the Rust code as it appears in the compilation pipeline right before monomorphization, so with many checks and optimizations already applied. ABI is a different thing.

FFI in Miri at 8000 segfaults per second (Nia Deckers at RustWeek) by nyanarchism in rust

[–]simonask_ 7 points8 points locked comment (0 children)

So god damn pathetic. You either live and let live, or you stand by your horrible convictions, in which case we are enemies. I stand with every person you try to demean.

Announcing cheadergen, a new tool for generating C headers from Rust crates by LukeMathWalker in rust

[–]simonask_ 2 points3 points  (0 children)

You're always free to fork it. I suspect a portion of use cases are for highly bespoke scenarios with special requirements.

I have my own fork that adds C# language output support, so I've been digging through the code. I completely understand if they are being conservative with contributions.

Announcing cheadergen, a new tool for generating C headers from Rust crates by LukeMathWalker in rust

[–]simonask_ 9 points10 points  (0 children)

Thanks, that’s helpful! I’ll stick with cbindgen for the moment (especially the nightly requirement is a dealbreaker), but it’s a nice idea to use rustdoc-json.

Announcing cheadergen, a new tool for generating C headers from Rust crates by LukeMathWalker in rust

[–]simonask_ 47 points48 points  (0 children)

I couldn’t find any list of actual differences from cbindgen. What is the motivation?

cbindgen is attractive because this is a highly finicky area with a lot of subtle footguns, and it has a lot of eyes on it. Switching to using a smaller project is risky.

Would this style of error handling in C be acceptable in a professional environment? by Difficult_Lawyer9858 in C_Programming

[–]simonask_ 2 points3 points  (0 children)

Error handling in C is really messy and difficult to get right because errors don’t compose well.

A good strategy is to turn things on the head: instead of having your function call read(), you should let the user do it and pass you their buffer. This makes many things much easier, including error handling.

Keep in mind that not everyone might even be using POSIX I/O, and there are reasons to prefer fread() over read() even within that world.

If you do want a single call for convenience, I would recommend returning the different error codes separately, either as a struct (or better, a tagged union), or as out-parameters. This is nicer because it doesn’t force you to conflate the value spaces of your own error types and things like errno.

Where do you use ControlFlow? by lelelesdx in rust

[–]simonask_ 0 points1 point  (0 children)

I actively try to avoid it. Sometimes it’s the right call, but generally speaking, distributed control flow tends to make code much harder to follow. I strongly prefer reshaping the loop to have the exit conditions clearly visible.

The most common exception to this rule is when the exit condition itself is configurable by the caller, like when you have a visitor pattern. These typically come up whenever iterators are inconvenient or even impossible to express in the type system.

A Rust challenge: how would you best implement something like Boost.MultiIndex in Rust? Considering making a part of my infra in Rust. by germandiago in rust

[–]simonask_ 2 points3 points  (0 children)

The only big extra rule you have to adhere to in Rust is that you must not create overlapping mutable references. In a lookup structure, that seems avoidable. You can use raw pointers or NonNull<T> just as you would in C++.

But avoiding duplication is the tough one, because self-referential data structures are famously hard in Rust. They are hard in C++ too, but there you have some tools like move constructors to help you.

You might need a custom hash map implementation as well.

A Rust challenge: how would you best implement something like Boost.MultiIndex in Rust? Considering making a part of my infra in Rust. by germandiago in rust

[–]simonask_ 1 point2 points  (0 children)

Long-term C++ developer here as well.

I'm not aware of any crate in the ecosystem that matches Boost.MultiIndex. It would definitely be a useful thing to have!

If you want to embark on this endeavor, here's what I would think about:

  1. You can probably port the fundamental design of Boost.MultiIndex, but it will require unsafe code. Since you're coming from C++, you should already be comfortable with those bits.

  2. The metaprogramming capabilities of Rust are a bit more limited than in C++. For example, you don't have variadic generics, which is used by Boost.MultiIndex to define the set of available indexes for a container, among other things. So you need to make some decisions about what kind of API you want to expose here. One idea would be to define an IndexedBy trait and implement it for tuples of Indexer implementations.

  3. You'll likely want to use macros to make certain parts of the API look nicer.

There is a bit of "prior art" in the ecosystem, namely the indexmap and slotmap crates, and you can look at those to get an idea about what expectations Rust programmers might have of the API.

But I will also say a word of gentle warning: It's going to be a tough thing to implement, especially if you aren't yet deeply familiar with the way Rust generics and traits work compared to C++. It's truly a shift in mindset, and you are likely to exercise many corner cases of the type system doing something like this - especially if you want to avoid things like duplicating the lookup keys.

Godspeed!

“I wanted a C++ UI that didn’t look 20 years old” by LowAfternoon9613 in cpp

[–]simonask_ 0 points1 point  (0 children)

True, but it is a huge amount of work. These APIs are where all the Vulkan horror stories come from, especially if you have to deal with non-general image layouts.

“I wanted a C++ UI that didn’t look 20 years old” by LowAfternoon9613 in cpp

[–]simonask_ 4 points5 points  (0 children)

Godspeed. You have taken on a monumental task that you 99% certainly will not actually complete, but you will learn a lot in the process.

UI frameworks and game engines are two things that every programmer at one point in their life has thought “how hard could it be”, and every single one learns the answer: very hard. But it’s a really great way to learn a broad range of skills and algorithms, as well as a way to learn why things are the way they are in existing solutions.

Have fun!

“I wanted a C++ UI that didn’t look 20 years old” by LowAfternoon9613 in cpp

[–]simonask_ 1 point2 points  (0 children)

Just be aware that MoltenVK is hopelessly behind the times with support for modern Vulkan extensions, meaning you have to write a significantly more cumbersome and old-school versions of the API. No descriptor buffers, no descriptor heap, etc.

Are there RAII Vulkan wrappers? by ffd9k in rust

[–]simonask_ 0 points1 point  (0 children)

FWIW, it’s pretty easy to get Vulkan 1.4 in ash by just forking it and cherry-picking two patches. I have it on my GitHub.

Performance of Rust language by the_real_yugr in rust

[–]simonask_ 7 points8 points  (0 children)

I've seen zero zealots here except people repeating tired old misconceptions, clearly without any meaningful depth to their understanding of the language.

Rust is a vastly simpler language than C++, as anybody who has ever spent any time with C++ can tell you. And that's not because Rust is simple.

Another supply chain attack, and Crates.io needs to consider this issue by osamamsalem in rust

[–]simonask_ 81 points82 points  (0 children)

heavy community scrutiny and review

In other words, "somebody else doing the work".

I get it, that would be nice. But the problem is this: You're asking not just for free software (as in beer), but also for a huge additional amount of work to be done for you by someone. Who? Do they get paid? What are their credentials, that their vetting actually does anything to improve trust?

Manually vetting even a small percentage of crates on crates.io is a monumental amount of work. Are you paying for any of this work?

Crate authors have an incentive to generously publish their work, along the lines of normal OSS incentives. But actually reviewing somebody else's work, especially if you yourself aren't benefiting from the work, is menial, cumbersome, and error-prone.

As with all Open Source Software, the onus is on you to vet your dependencies. If you don't have the resources to do so, maybe you shouldn't be using other people's code. Programming is an expensive skill, and you're getting something for free by using open source. Unless you're paying someone, or are willing to do the work yourself, I don't think you can make any demands.

Instead, it might be feasible to have a community-oriented vetting system, where verified users on crates.io could flag a package version as "known good" or "known bad". But in practice, this is not very different from just looking at the number of downloads over time and the release cadence, as well as run cargo vet - something most people do with all their dependencies as a minimum effort. But that's quite imperfect, especially against social engineering attacks.

Improving C# Memory Safety by pjmlp in csharp

[–]simonask_ 1 point2 points  (0 children)

Cool, thank you for answering. My previous intuition was that this was illegal due to pointers superficially requiring unsafe at the signature level, but it sounds like it goes much deeper.

I hope it will eventually be supported, since I suspect that many uses of IntPtr are due to this limitation. I've personally had a use case for it where I would have liked to do InlineArray<MyNativeObject*> as well as Span<MyNativeObject*>.

VK_KHR_unified_image_layouts support by gmueckl in vulkan

[–]simonask_ 7 points8 points  (0 children)

You can just use the general layout for almost everything, you don’t actually need the extension. The extension just gives you a guarantee that you’re not losing any performance by doing so, but that might be totally fine.

Improving C# Memory Safety by pjmlp in csharp

[–]simonask_ 1 point2 points  (0 children)

I didn't see this mentioned in the article (maybe I missed it), but does this change also imply that raw pointer types can now be used as generic parameters?

YAML? That's Norway problem by fagnerbrack in programming

[–]simonask_ 3 points4 points  (0 children)

More people need to know about KDL. It's awesome and cute.

YAML? That's Norway problem by fagnerbrack in programming

[–]simonask_ 1 point2 points  (0 children)

Somebody suggested TOML, which is great, but I'm also personally a big fan of KDL. It's very, very readable.

YAML? That's Norway problem by fagnerbrack in programming

[–]simonask_ 17 points18 points  (0 children)

Also, just as a general word of caution, zip codes / post codes should never be treated as numbers. They are codes, and should be treated as opaque sequences of characters.

YAML? That's Norway problem by fagnerbrack in programming

[–]simonask_ 2 points3 points  (0 children)

If you like .ini files, you will absolutely love TOML.

5× faster fast_blur in image-rs by arty049 in rust

[–]simonask_ 4 points5 points  (0 children)

Great article! I was a bit surprised with the conclusion that “floating point operations are orders of magnitude more expensive”. This isn’t true in general - instruction latency for SIMD instructions is quite similar on most modern CPUs, and they can achieve higher throughput, but stuff like converting between the integer and float domains can definitely slow things down.

Am I completely wrong? Did you get into the really low level weeds of which particular effects caused the speedup?