Page 1 of 1

Looking for an efficient string implementation.

Posted: Thu Mar 10, 2016 11:09 am
by Roman
Hello, everyone.

I'm looking for an efficient implementation of functions like memcpy, memset, etc. Is there anything like that available? I don't need a full C library, just the memory/string code.

Thanks in advance

Re: Looking for an efficient string implementation.

Posted: Thu Mar 10, 2016 12:06 pm
by Solar
The most efficient, and easily obtained, implementation are the compiler builtins. Check your compiler manual for details; it is very unlikely a homegrown implementation can beat those.

Re: Looking for an efficient string implementation.

Posted: Thu Mar 10, 2016 1:32 pm
by Octocontrabass
Perhaps these optimized subroutines are what you are looking for? The code is GPL, so that may affect whether or not you can use it.

Re: Looking for an efficient string implementation.

Posted: Thu Mar 10, 2016 4:06 pm
by Combuster
The most space-efficient implementations are the REP ~~~~ opcodes, which cover a good deal of the mem/str functionality in single instructions. It scores the same lack of portability points as using compiler-specific builtins though, so if that is an issue you might want to use a proper C implementation anyway, and let the compiler optimise it based on your architectural constraints.

Re: Looking for an efficient string implementation.

Posted: Fri Mar 11, 2016 7:21 am
by Candy
Solar wrote:The most efficient, and easily obtained, implementation are the compiler builtins. Check your compiler manual for details; it is very unlikely a homegrown implementation can beat those.
+1 on using compiler builtins. I optimized a program to run in half the time by replacing std::string::find(X) with strstr(str.c_str(), X); because the former does not resolve to a builtin and the latter does.

Re: Looking for an efficient string implementation.

Posted: Fri Mar 11, 2016 10:09 am
by osdever
Roman wrote:Hello, everyone.

I'm looking for an efficient implementation of functions like memcpy, memset, etc. Is there anything like that available? I don't need a full C library, just the memory/string code.

Thanks in advance
There's a code on the forum, I've got my mem* functions from it. I don't remember that thread, but see code in my repo - string.h.

Re: Looking for an efficient string implementation.

Posted: Fri Mar 11, 2016 3:25 pm
by Roman
Thanks everyone for the answers.
catnikita255 wrote:
Roman wrote:Hello, everyone.

I'm looking for an efficient implementation of functions like memcpy, memset, etc. Is there anything like that available? I don't need a full C library, just the memory/string code.

Thanks in advance
There's a code on the forum, I've got my mem* functions from it. I don't remember that thread, but see code in my repo - string.h.
Your code is just generic C code, I could implement that myself. What I'm looking for is an efficient architecture-optimized code.

Regarding other answers,
1) a permissive license would be preferred, but I will have a look at that GPL code;
2) what do you mean by using compiler builtins? LLVM in my case (Rust, armv7) just inserts calls to memcpy.

Re: Looking for an efficient string implementation.

Posted: Sat Mar 12, 2016 12:24 am
by Solar
Most compilers offer build-in implementations of the basic memory functions that are highly optimized already. Seeing how much of the code generated by the compiler deals with moving memory around, that is only logical. Since this code ends up in EVERY binary anyway, it is usually exempt from the licensing scheme of the compiler itself.

I don't know the specifics for ARM / LLVM, but builtins are definitely what you are looking for. They are usually named like __memcpy, __builtin_memcpy or something like that.