Railgun: a fast strstr(3)-like function
sanmayce.com
sanmayce.com
http://www.codeproject.com/info/cpol10.aspx
Good luck figuring out what this is legal to use with.
You agree not to use the Work for illegal, immoral or improper purposes,
or on pages containing illegal, immoral or improper material.
Good luck figuring out what that even means.Maybe they're there and I missed them because the website made my eyes bleed.
Searching for Pattern('an',2bytes) into String(206908949bytes) line-by-line ...
strstr_Microsoft_hits/strstr_Microsoft_clocks: 1212509/544
strstr_Microsoft performance: 248KB/clock
StrnglenTRAVERSED: 138478024 bytes
strstr_GNU_C_Library_hits/strstr_GNU_C_Library_clocks: 1212509/359
strstr_GNU_C_Library performance: 376KB/clock
StrnglenTRAVERSED: 138478024 bytes
Railgun_Doublet_hits/Railgun_Doublet_clocks: 1212509/321
Railgun_Doublet performance: 421KB/clock
StrnglenTRAVERSED: 138478024 bytes
Railgun_Quadruplet_8Triplet_hits/Railgun_Quadruplet_8Triplet_clocks: 1212509/335
Railgun_Quadruplet_8Triplet performance: 403KB/clock
StrnglenTRAVERSED: 138478024 bytes
Railgun_Mischa_8Triplet_hits/Railgun_Mischa_8Triplet_clocks: 1212509/348
Railgun_Mischa_8Triplet performance: 388KB/clock
StrnglenTRAVERSED: 138478024 bytes
BNDM_32_hits/BNDM_32_clocks: 1212509/505
BNDM_32 performance: 267KB/clock
StrnglenTRAVERSED: 138478024 bytes
...The article is licensed under CPOL, not the code. Railgun is licenseless, one developer working for Mozilla advised me to put it under BSD or public domain - which is guess what: just another license, all my etudes/tools/functions are 100% FREE, not as pseudo-copylefters understand and try to sell their "Free" - which is ridiculous, especially the free beer part, if I am to share my joy with my buddies I buy beers and give them for free UNCONDITIONALLY.
The bottom-line: Railgun is people's choice 'memmem', if you ever face the possibility to go to jail, just call me I will tell the judges some copyleft sagas of my own, that is to educate them how university professors are funded with people's money (not only) and any derivate of those algorithms/implementations should follow the same licenselessness - a nifty word - everything else is just one perverted game for money, as I like to say hypocrisy in action.
Regards to all, and no, my endless dumps are not to obstruct the usage, quite the contrary - to provide field feedback - to give thorough comparisons, had I had more than one computer I would have dumped several times more stats.
Best, Georgi
p.s. I fully agree that code from publicly funded academic work should be open source – ideally under a very liberal license like BSD or MIT.
[1] http://www.codeproject.com/Articles/250566/Fastest-strstr-li...
It looks pretty awesome, I think it could be ported to PHP fairly easily.
I didn't read every update of the code but it looks like it is written in c only. Critical code is usually implemented with the best algorithm and then converted/optimized manually to/in assembly.
I was going to have that be my entire comment here, but I figured this was easy enough to check - so I pulled php 5.5.7 source, and because of option parsing complexities strstr ends up being implemented in terms of php_memnstr, which is a macro for zend_memnstr, which in turn calls memchr and memcmp repeatedly in a loop. So, no, libc's strstr doesn't seem to be used.
I'm a little unsure whether or why this has to be so complex, but after a quick dip the water doesn't seem inviting enough for me to follow up.
For example, Boyer-Moore (http://en.wikipedia.org/wiki/Boyer-Moore_string_search_algor...) starts by looking at the end of the substring. If it finds a match, it searches earlier. If it does not find a match, it can skip ahead by several characters (possibly even the length of the substring, depending on how the match failed). How much to skip ahead is a bit complicated, but can be calculated in advance.
Consider searching for a substring consisting of 1000 'a's. Boyer-Moore starts by looking at the 1000th (1-indexed) character. If it's an 'a', it then walks back and checks the 999th, 998th etc. However, if it's not an 'a', it can immediately skip on to examine the 2000th character, i.e. only looking at 1 in every 1000 characters. As you can imagine, this can be very fast!
The Railgun implementation seems to be a combination of improved Boyer-Moore (Boyer-Moore-Horspool-Sunday) with Rabin-Karp (which uses hashing). My understanding is that these algorithms complement each other, so if you have an input string that is particularly inefficient with one algorithm, it automatically picks the other one.
Since many programs have string-searching in their innermost loops, spending some time optimizing this function can be worthwhile.
As another commentator pointed out, libc's strstr assumes NUL-terminated strings. Maybe php's doesn't? Which seems a bit odd to me in light of the explanation of PHP's genesis as having roots in C, but stranger things...
I'm not surprised at all that libc's strstr would be complex.
Here is a gist of the code: https://gist.github.com/jaytaylor/8102304
I would be glad to read that Sanmayce reads this or some similar input and then starts to think about making his output really more accessible. But I guess he likes it as it is. Bon Appétit.
This is how I feel when I hit a webpage that offers zero content without having to execute JavaScript in a browser first.
For whatever it's worth, this page loads in less than 1 second and looks fine in my text-only browser.
I guess in this case the web developer has chosen to recklessly punish users who never disable images or JavaScript, in the same way some web developers recklessly punish users who never enable such "essential features".
If the user's objective is to read and perhaps download some source code (as in this "article"), there is arguably no reason that images or JavaScript should be necessary.
"recklessly" here means the web developer does not intend to make users suffer but he knows some users will suffer if he makes a certain design choice and, knowing this, he makes that choice anyway