3 ms·
His warning against using #ifdef is also misplaced. I have written assembler (GAS) using the GAS x86 asm preprocessor to write portable x86/x86-64 assembly code
by Locke1689 17y ago
His warning against using #ifdef is also misplaced. I have written assembler (GAS) using the GAS x86 asm preprocessor to write portable x86/x86-64 assembly code in one file. Far simpler and shorter than writing the same code in two files.
Basically, whenever he says "never" do something he means he doesn't like to do it. For example, his answer to not having a multi-level function break is a nested helper function. God no. That's one of the few times when it's correct to use goto.
Edit: Ahh, he also advocated always using mmap instead of the standard file IO functions. Wow that's bad.
- mbreese 17y agoI'm curious: are the standard IO functions that much more efficient than a mmap'd file? What makes you choose one over the other? I usually just use standard IO, unless I have a large file that might be randomly accessed by multiple processes. In that case I'll use a mmap. But, I don't code in C very much anymore, so I was wondering how much of a difference there really is.
- Locke1689 17y agoMostly because mmap is not portable. If you're writing a Linux specific version of your code then mmap is fine. If you foresee running your code on other platforms then you shouldn't use it. Basically, there's a reason why it's called "standard" IO. Generally you shouldn't use system calls unless you are absolutely, 100% sure that your code will only ever need to work on one system.
- holygoat 17y agoI've worked on a project that successfully uses memory mapping on all the major OSes. mmap is (allegedly) portable across Unixes: http://opengroup.org/onlinepubs/007908775/xsh/mmap.html http://opengroup.org/onlinepubs/007908775/xsh/mmap.html and you can do the same thing (with a few more hoops) on Windows.
- Locke1689 17y agoThere's no guarantee that it will be portable and act the same (even across the POSIX systems). I'm not saying you should never use it, just that it shouldn't your automatic answer to everything.
- kelnos 17y agoMany people may not care, but mmap() often doesn't exist on hardware without a MMU.
- sorbits 17y agoI did benchmarks on OS X (10.5) between asynchronous IO, mmap, stdio, and normal unix open/read (disabling caching) — it all came down to about the same after I had fiddled with page size, prefetch size, etc. to optimize each. This of course depends on your OS (and version). My advice would be to pick API based on “semantics” (or need for portability). If you need to read through a file, use the API intended for that because mmap will not be optimized for that access pattern, but stdio will.
- jlangenauer 17y agoI'm curious about this too - why is mmap so bad?
- holygoat 17y agoIt's not always bad -- very often it's the right thing to do. Just not "always".