Yes, cache is important. But there's a world of difference between the "flush the caches and the TLB" behavior that system calls used to incur and the "expensive relative to local variable access" behavior we're talking about here.
"A bounce to non-local, non-current execution environment" literally doesn't mean anything. You could be talking about the CR3 change that swaps page hierarchies, the CPL3->0 change that allows privileged instructions, the CS change that got you there, or even the ESP change to "swap stacks". Which of these are expensive?
I'm chasing you down because it's fun, not because I think you don't know what you're talking about.
But, let's make this relevant: if system call overhead is drowned by disk I/O overhead, then one argument for mmap goes away.
I'm sorry, but you seem to have a wildly inflated idea of the speed of system calls on modern OSs. The real world just doesn't work like that. This is my last post on this thread.
s = (s >> 1) ^ (-(s & 1) & 0xd0000001);
I also timed it the way you asked, with wall clock time over 100M iterations (10M runs too fast to time). Surprising even me, getpid wins by almost a full second.Got another challenge for me to write code for?
Want the punch line? OSX getpid averages under 100 cycles; snprintf (no IO) averages over 1000.
Breakpoint 1, 0x96210c44 in getpid ()
(gdb) disp/i $eip
2: x/i $eip 0x96210c44 <getpid>: call 0x96210c49 <getpid+5>
(gdb) stepi
0x96210c49 in getpid ()
2: x/i $eip 0x96210c49 <getpid+5>: pop %ecx
0x96210c4a in getpid ()
2: x/i $eip 0x96210c4a <getpid+6>: lea 0xa6427a3(%ecx),%ecx
0x96210c50 in getpid ()
2: x/i $eip 0x96210c50 <getpid+12>: mov (%ecx),%eax
0x96210c52 in getpid ()
2: x/i $eip 0x96210c52 <getpid+14>: test %eax,%eax
0x96210c54 in getpid ()
2: x/i $eip 0x96210c54 <getpid+16>: jle 0x96210c57 <getpid+19>
0x96210c57 in getpid () 2: x/i $eip 0x96210c57 <getpid+19>: mov $0x14,%eax
0x96210c5c in getpid ()
2: x/i $eip 0x96210c5c <getpid+24>: call 0x961e8cf4 <_sysenter_trap>
Typically kernel pages (and the TLB entries which cover them) are "pinned." You don't want to take a TLB exception in the middle of an interrupt service routine, let alone a page fault. Most architectures couldn't even handle the nested exception, and would panic.
I haven't kept up with x86 on Linux; does Linux enable the global page extensions? I would assume it does for kernel mappings.
Given that main memory reads are pushing 100 cycles on a modern box, all those things add up. A context switch (my usage: meaning a bounce to a non-local, non-current execution environment) is really expensive.