Looking for an efficient string implementation.

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
User avatar
Roman
Member
Member
Posts: 568
Joined: Thu Mar 27, 2014 3:57 am
Location: Moscow, Russia
Contact:

Looking for an efficient string implementation.

Post 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
"If you don't fail at least 90 percent of the time, you're not aiming high enough."
- Alan Kay
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: Looking for an efficient string implementation.

Post 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.
Every good solution is obvious once you've found it.
Octocontrabass
Member
Member
Posts: 5587
Joined: Mon Mar 25, 2013 7:01 pm

Re: Looking for an efficient string implementation.

Post 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.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Looking for an efficient string implementation.

Post 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.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re: Looking for an efficient string implementation.

Post 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.
User avatar
osdever
Member
Member
Posts: 492
Joined: Fri Apr 03, 2015 9:41 am
Contact:

Re: Looking for an efficient string implementation.

Post 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.
Developing U365.
Source:
only testing: http://gitlab.com/bps-projs/U365/tree/testing

OSDev newbies can copy any code from my repositories, just leave a notice that this code was written by U365 development team, not by you.
User avatar
Roman
Member
Member
Posts: 568
Joined: Thu Mar 27, 2014 3:57 am
Location: Moscow, Russia
Contact:

Re: Looking for an efficient string implementation.

Post 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.
"If you don't fail at least 90 percent of the time, you're not aiming high enough."
- Alan Kay
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: Looking for an efficient string implementation.

Post 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.
Every good solution is obvious once you've found it.
Post Reply