Why are functions like memchr linked to C implementations and not written in pure Rust?

Functions such as memchr seem fairly simple, but Rust projects only use C code bindings with Rust backups and not with their implementation Rust. Can't memchr implement effectively in Rust?

+5
source share
3 answers

They can. If you look at the glibc implementation, it will look like fallback:memchr . However, this is only part of the story. a common implementation is used only when there is no more suitable one.

For example, x68-64 has an option written on the assembly . And many other architectures that provide complex instructions.

To achieve the same speed, Rust would have to provide something similar at the instruction level, which essentially boils down to the same (or better) build. At this point, you are simply duplicating the work. It already exists, there is no need to create everything anew.

+10
source

There is speculation that the only reason to link to the C library is for efficiency.

I am afraid that you forget about convenience here. Just because a function can be implemented as efficiently in Rust as it is in C (possibly using unsafe code and assembly) does not mean that it is convenient.

Instead of trying to create an optimized implementation for each platform under the sun, it’s just more convenient to be able to return to the already provided C function to start with it, and then gradually configure it for the platforms that you need if necessary.

Creating implementations specifically designed for hardware / OS is a long job, and if someone has already invested in it, it might make sense to just reuse its result!

+8
source

They can, but according to the documentation in Rust GitHub this is for performance reasons:

memchr comes down to a super-optimized machine code in order of magnitude larger than haystack.iter().position(|&b| b == needle) . (See Tests).

Since tests show that memchr() from C bindings leads to much better performance, this version is preferred. An attempt to optimize the Rust option further would probably lead to the inclusion of some assembly, so it would come down to the same thing.

+2
source

Source: https://habr.com/ru/post/1257430/


All Articles