5 ms·
Sure. https://gist.github.com/tqbf/4de61a3e34d2e4664044666c107abe77 https://gist.github.com/tqbf/4de61a3e34d2e4664044666c107abe7... (I don't vouch for this co
by tptacek 6y ago
Sure.
https://gist.github.com/tqbf/4de61a3e34d2e4664044666c107abe77 https://gist.github.com/tqbf/4de61a3e34d2e4664044666c107abe7...
(I don't vouch for this code; I wrote it off the top of my head, and, in keeping with the exercise, I wrote it in pico).
It's not 10x longer. I'm honestly not sure why either Rust or C++ bothered with regexps for this problem.
Anyways, you get the gist of what this looks like in C now. Obviously, don't write things like this in C.
- cb321 6y agoYou have/had a bug in your code. The realloc() needs to multiply its second argument csz by sizeof *counts or sizeof(struct wordcount).
- tptacek 6y agoOf course I have a bug in this code. Thanks, fixed! :) With that fixed, this dumb program does my whole (very large) homedir in about 10 seconds, for some definition of "does" that may or may not include counting every word in every txt file. For the curious: 0. get / 566006 1. the / 158168 2. pkg / 119419 3. syscall / 105828 4. const / 98259 5. and / 86670 6. ideal-int / 43078 7. that / 31609 8. for / 31129 9. this / 24980 10. text / 22907 :P
- cb321 6y agoJust trying to help, not criticize. As to correctness, if you change the tokenization to: while (fgets(buf, 1024, fp)) { char *c, word[1024], *w = word; for (c = buf; *c; c++) { *c = tolower(*c); if (*c >= 'a' && *c <= 'z') *w++ = *c; else { *w = '\0'; count(word, (size_t)(w - word)); w = word; } } *w = '\0'; count(word, (size_t)(w - word)); } and you have that `count` function skip words with len < 2 then I can match results with the other 3 (Nim, Rust, C++) versions. Also, your C version runs in just 7 ms on Tale Of Two Cities, about half the time of the PGO optimized Nim.
- tptacek 6y agoOh, no, you don't sound critical at all. I'm making fun of myself. Thank you for actually reading it!
- cb321 6y agoCool. You could probably simultaneously speed it up and simplify it by changing from linked list collision to open addressing with linear probes (but you may need a larger TSZ). Besides being more cache friendly for larger files, at the end you can then just qsort your table instead of first building a flat copy. :-) You could also use the unreduced hash value as a comparison prefix (only calling strcmp on different values). But it's not bad for potato code. ;-)
- tptacek 6y agoI didn't do open addressing because I didn't want to write code to resize the whole table. :) I thought about also keeping the top 10 as I go instead of copying the whole table. But I'm guessing that virtually all the time this program spends is in I/O.
- cb321 6y agoI kind of suspected that might be the reason..but you do still have a scalability problem for very large vocabularies. :-) I just did a profile and saw about 15% in strcmp in the hot cache run, but sure if it's not in RAM then IO is likely the bottleneck.
- cb321 6y ago(You could also just save the len and use that as a comparison prefix, but the hash value is less likely to yield a false positive.)
- markus_zhang 6y agoThanks I got the gist. Doesn't look too bad IMO.
- agv720 6y agoIsn't this code kind of supporting the first point in the article? I mean, the article started by pointing how rust "protected" you from a "TTCTTOU" error. This C code is not only not protecting you against that, but also it seems to have little to none error checking. What happens if fopen fails? It just skips to the next file and happily ignores all the entries in the file. What if fget fails? Again, it stops processing the file and goes to the next. I won't even get into what happens if calloc fails and returns null. Or if ++ wraps around and you get a negative value (let's hope we are using 64 bits) This program will likely work, but if it doesn't you will just get an invalid number and never find out. Don't get me wrong, I understand this is "not serious" code, written in a Sunday and for fun. And I would be ok with it if the article wasn't about language safety.
- tptacek 6y agoTry as hard as you like, you aren't going to find anyone to argue that C is as safe as Rust. As for the TOCTTOU issue --- which is kind of silly, and had really nothing to do with Rust --- the diff to fix that problem is tiny: https://gist.github.com/tqbf/4de61a3e34d2e4664044666c107abe77/revisions https://gist.github.com/tqbf/4de61a3e34d2e4664044666c107abe7... On a sane system, what's going to happen if calloc (or realloc, or strdup) returns NULL is the same as what's going to happen in the Rust program: the program will abort. Checking malloc returns is a bad idea, and causes more problems than it solves. Similarly: this code does essentially the same thing with errors that the Rust one does: it swallows them and continues. At any rate, while this C++ vs Rust discussion might be about language safety (I vote Rust, like any other software security person would), this subthread isn't.
- agv720 6y ago>you aren't going to find anyone to argue that C is as safe as Rust. That's good, I was starting to doubt humanity :) >As for the TOCTTOU issue --- which is kind of silly, and had really nothing to do with Rust agree. But well, was what it was claimed in the article. >On a sane system... I disagree with this one, although I know I am in the minority. A malloc failure is not a unrecoverable error, and the only reason to abort on memory error is because if you allow malloc errors, then virtually every line of code can fail, because it is either allocating memory or calling some other method that allocates memory. (I imagine you've already read http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p0709r4.pdf http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p070... section 4.3 but I mention just in case someone hasn't heard of it) Something similar happens with integer overflow, although we've collectively decided that we prefer to have 2,147,483,647 + 1 = -2,147,483,648. (in both C++ or rust in release mode). But if we assumed that i++ can cause an error, it would mean that every line of code could possible be an error. But well, back to memory allocation: Just a month ago I had a situation where using a language (C#) which just throws an exception in memory overflow saved my day. I left running overnight a program that builds and creates setups for the programs I make. There are about 190 different setups (win32, 64, linux, and variations), all compressed using max settings in 7zip. Those builds run in parallel in a threadpool, and each 7zip compression takes a lot of memory. So much, that during the night one thread raised an out of memory error. When I woke up next day, I had 189 setups created correctly, and one that failed. I rerun that missing setup and was ready to publish them. Had I used a "sane" language, the full app would have aborted and I would have to run the 190 setups again. But seriously, it can happen a lot, specially in embedded. You are processing images and one image is too big. Do you abort the app, or report the error and move to the next image? >this code does essentially the same thing with errors that the Rust one does: it swallows them and continues Thats was my point in the answer: Rust and C++ don't shallow the error. You have the line: reader.lines().filter_map(Result::ok) And then: if let Err(e) = scanfiles() { println!("Error: Something unexpected happened: {:#?}", e); So, if there is an error, it will report it to you, not shallow it. And that is to me the full point: The guy who coded the Rust version would have to go out of his way to shallow the error, while in C it is the default.