Standard library thickness (Rust, C and C++) for the hello world


#1

This post is more like a fact than a question or searching for a help.

I have compared three “hello world” applications in Rust, C and C++, to learn profiling with valgrind utility.
I have found interesting thing: the number of “dl_lookup_x” calls vary very much in each application.

Rust:

fn main() {
    println!("Hello world");
}

C++:

#include <iostream>

int main () {
    std::cout << "Hello world" << std::endl;
    return 0;
}

C:

#include <stdio.h>

int main() {
    printf("Hello world\n");
    return 0;
}

C++'s callgrind top 2:

1,267,311  ???:_dl_lookup_symbol_x [/usr/lib/ld-2.26.so]
  837,745  ???:do_lookup_x [/usr/lib/ld-2.26.so]

Rust’s callgrind top 3:

180,784  ???:do_lookup_x [/usr/lib/ld-2.26.so]
142,171  ???:strcmp [/usr/lib/libc-2.26.so]
 81,277  ???:_dl_lookup_symbol_x [/usr/lib/ld-2.26.so]

C’s callgrind top 5-6:

25,015  ???:do_lookup_x [/usr/lib/ld-2.26.so]
19,282  ???:_dl_lookup_symbol_x [/usr/lib/ld-2.26.so]

It seems that the C++'s standard library is so thick that it requires more than a million of calls
for resolving symbols of the standard library. The rust requires more than 6x less amount of
symbol lookup, while C library is the thinnest one and requires nearly 6x calls less than Rust.
So, if the take C as 1, Rust takes 7.23, C++ takes 50.66 times more resolution calls than C for
the “hello world” using native language approach (iostream, stdio.h and println! macro).

What do you think? Are my assumptions correct?


#2

Perhaps related to this thread


#3

Also the FAQ goes into this a bit https://www.rust-lang.org/en-US/faq.html#why-do-rust-programs-have-larger-binary-sizes-than-C-programs


#4

Hmmm, interesting. I would suggest you add an assembly one that calls write only. Or, even, a bare metal one.