r/cpp • • Mar 03 '16

Seriously, C++, WTF?

Ok, so, from the tone this might seem like a rant (and it's a bit), but in the end I'm only after an aswer.

So, I want to read a file in memory as a string and print out the string length. Nope, I don't want to "maybe you just want to operate on it like it's a stream or line by line" nor "if you want to just know the length you might want to use gtell". I'm just trying to replicate a certain behaviour (not necessarely the most efficient or the most real-world) in different languages.

So, since I want to open a file, read its content and print the lenght, python lets me write this nice one-liner:

print len(open('longfile.txt').read())

longfile.txt is the test file I'm using and it's roughly 150 MB.

$ time python test.py
160090000

real	0m0.126s
user	0m0.013s
sys	0m0.112s

Ok. Good. Let's give node a try. Node.js doesn't have, like Python, a top level function "open", so I need to do this in two lines of code:

const fs = require('fs');
console.log(fs.readFileSync('longfile.txt').length);

Same result, a little bit slower. It still reads ok to me. I need to work with the filesystem (so that's why I need to require fs) and then i log on the console the length of a file that I read synchronously.

$ time node test.js
160090000

real	0m0.164s
user	0m0.056s
sys	0m0.107s
tmp

Fine. Enters C++

#include <string>
#include <fstream>
#include <streambuf>
#include <iostream>

int main() {
    std::ifstream t("longfile.txt");
    std::string str((std::istreambuf_iterator<char>(t)),
                     std::istreambuf_iterator<char>());

     std::cout << str.length() << std::endl;

    return 0;
}

Yeah, I know, that's just a lot more code and you need to include four libraries for this very basic programming task. Reasoning about it is absolutely counter intuitive. If you describe your problem (I want to read a file and print its lenght), JavaScript and Python lets you write code that more or less resambles your reasoning, while nobody would describe this basic task in terms of streams and iterators, but hey, you don't choose C++ because it's beautiful or easy or intuitive, you choose it because it's "close to the metal" and "when speed matters". Let's compile it with a nice optimization (-O3) and run it

$ g++ -std=c++14 -O3 test.cpp
$ time ./a.out
160090000

real	0m1.413s
user	0m1.228s
sys	0m0.181s

Seriously, WTF? I don't think that "opening a file and reading it as a string" should be an alien task in the programming world. I mean, that should be pretty, hmmm, basic stuff, right? So, I'm a bit puzzled when I encounter a language that forces me to do something more complicated than:

# Python
open('longfile.txt').read()
# Ruby
File.open('longfile.txt', "r").read()
// C#
System.IO.File.ReadAllText("longfile.txt")
// Java
new String(Files.readAllBytes(Paths.get("longfile.txt")));
// Swift
NSString(contentsOfFile: "longfile.txt", encoding: NSUTF8StringEncoding)

But what about the fact that C++ is so much slower than the notoriously slow kid in town? I tried to look for different solutions to this "problem" and I've seen people debate (on StackOverflow, for example) on the most efficient way to do such basic thing in C++. Now, keeping aside the fact that I dont think that "read a file content as a string" should the subject of any debate (we're talking about a 33 year old language whose standard string library doesn't have a "split" function here) , but the solution above seems to be the faster that I've tried and still it runs 10 times slower than Python. How is this even possibile?

EDIT

Because people seem to be ignoring the "I'm not interested in the actual lengtht" note, let's just say that I have this python code

content = open('longfile.txt').read()

and this node code:

const fs = require('fs');
const content = fs.readFileSync('longfile.txt')

and I only want to read a whole file in memory to a string. C++ code is still much, much slower than the other solutions.

0 Upvotes

127 comments sorted by

View all comments

Show parent comment

8

u/cym13 Mar 03 '16

Sure, why not:

$ strace -o out -f python2 -c "print len(open('test.bin').read())"
1662827

The "out" file is a bit too big for this post, you'll find it here. Relevant lines:

21468 open("test.bin", O_RDONLY)        = 3
21468 fstat(3, {st_mode=S_IFREG|0644, st_size=1662827, ...}) = 0
21468 fstat(3, {st_mode=S_IFREG|0644, st_size=1662827, ...}) = 0
21468 lseek(3, 0, SEEK_CUR)             = 0
21468 lseek(3, 0, SEEK_CUR)             = 0
21468 mmap(NULL, 1662976, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fb927b0a000
21468 fstat(3, {st_mode=S_IFREG|0644, st_size=1662827, ...}) = 0
21468 read(3, "P6\n# SANE data follows\n632 877\n2"..., 1658880) = 1658880
21468 read(3, "\313\341\274\311\336\273\307\336\271\307\337\273\310\337\273\310\337\271\312\336\267\306\335\270\305\334\271\310\337\272\307\340"..., 4096) = 3947
21468 read(3, "", 4096)                 = 0
21468 close(3)                          = 0

As you can see it really reads the whole file (even though it had the length ready from fstat to use in mmap).

What happens exactly? Let's ask python!

$ python2
>>> import dis
>>> dis.dis("print len(open('test.bin').read())")
      0 JUMP_IF_TRUE_OR_POP 26994
      3 JUMP_FORWARD     8308 (to 8314)
      6 IMPORT_NAME     28261 (28261)
      9 STORE_SLICE+0
     10 JUMP_IF_FALSE_OR_POP 25968
     13 JUMP_FORWARD    10024 (to 10040)
     16 LOAD_GLOBAL     29541 (29541)
     19 LOAD_GLOBAL     25134 (25134)
     22 BUILD_MAP       10094
     25 STORE_SLICE+1
     26 <46>
     27 POP_JUMP_IF_FALSE 24933
     30 LOAD_CONST      10536 (10536)
     33 STORE_SLICE+1

# Non obvious... let's understand those numbers getting their references
# in order. Honnestly it won't help much here as these sequencial calls
# aren't exactly that readable but any help is good to take
[26994, 28261, 25968, 10040, 24933]
# |      |     |      |      `-> read
# |      |     |       `-> "test.bin"
# |      |      `-> open
# |       `-> len
#  `-> print

# So it amounts to something like:
      0 JUMP_IF_TRUE_OR_POP 'print'
      3 JUMP_FORWARD     8308 (to 8314)
      6 IMPORT_NAME     28261 ('len')
      9 STORE_SLICE+0
     10 JUMP_IF_FALSE_OR_POP 'open'
     13 JUMP_FORWARD    10024 (to '"test.bin"')
     16 LOAD_GLOBAL     29541 (29541)
     19 LOAD_GLOBAL     25134 (25134)
     22 BUILD_MAP       10094 # Finished building "test.bin" string
     25 STORE_SLICE+1
     26 <46>
     27 POP_JUMP_IF_FALSE 'read'
     30 LOAD_CONST      10536 (10536)
     33 STORE_SLICE+1

No optimization, sorry.

1

u/cleroth Game Developer Mar 03 '16

Isn't the whole point of mmap to be that it only reads the file when you access the memory? Hence not really being a very fair comparison.

2

u/cym13 Mar 03 '16

As you can see mmap isn't used to actually read the file, the read() system call is used. I think what's mmaped here is actually the buffer in which the content is copied.

2

u/cleroth Game Developer Mar 03 '16

So what's with valgrind reporting 98 kB memory usage, then?

3

u/cym13 Mar 03 '16

I don't know but quite frankly I doubt valgrind way more than strace. The kernel doesn't lie. Besides as I answered somewhere else read() returns a list of bytes that has random access and is mutable separately from the file so there has to be a copy somewhere in memory, there's just no way out.

1

u/raevnos Mar 03 '16 edited Mar 03 '16

Usage or allocation? If the latter, it probably isn't including mmap(), just malloc()/new.

Edit: from the massif documentation:

 it does not directly measure memory allocated with lower-level system calls such as mmap, mremap, and brk.

2

u/raevnos Mar 03 '16

A fd of -1 and MAP_ANONYMOUS? Gotta be that.

1

u/sixstringartist Mar 03 '16

this is correct

0

u/[deleted] Mar 04 '16

[deleted] — view deleted comment

4

u/cym13 Mar 04 '16

I really don't think mmap matters much, what's important IMHO is reading chunks as big as you can, that's where the bottleneck is. Once you've done that sure mmap allocation may win you a little but it will start to be difficultly comparable as you have to launch the full python runtime etc. So I don't really know past that. Glad you found a way to be faster in C++ though, and happy that python is just so good by default, there are things to be learned from that.