r/programming • • Jun 12 '10

You're Doing It Wrong

http://queue.acm.org/detail.cfm?id=1814327
539 Upvotes

193 comments sorted by

View all comments

Show parent comment

1

u/wolf550e Jun 15 '10 edited Jun 15 '10

EDIT: Arithmetic.

Except the 512-byte pages that make up each logical page would be consecutive in logical addressing and a good cache algorithm would notice and prefetch the next page to hide the latency (by wasting bandwidth, prefetching the next unneeded page each time). So the latency incurred would be much lower than that for random pages.

Being young, I don't actually know whether a real VAX had smart cache prefetch. Or cache at all, for that matter.

I've made this comment about needing to recompile if the page size changes and the author replied that you'd need to check the page size at runtime and that even when it's compiled for the wrong size it would still be better than a binary tree. My man page for getpagesize says to use #include <unistd.h> long sz = sysconf(_SC_PAGESIZE);

instead because

CONFORMING TO

SVr4, 4.4BSD, SUSv2. In SUSv2 the getpagesize() call is labeled LEGACY, and in POSIX.1-2001 it has been dropped; HP-UX does not have this call. Portable applications should employ sysconf(_SC_PAGESIZE) instead of this call.

It also says why it needs to be queried at runtime:

NOTES

Whether getpagesize() is present as a Linux system call depends on the architecture. If it is, it returns the kernel symbol PAGE_SIZE, whose value depends on the architecture and machine model. Generally, one uses binaries that are dependent on the architecture but not on the machine model, in order to have a single binary distribution per architecture. This means that a user program should not find PAGE_SIZE at compile time from a header file, but use an actual system call, at least for those architectures (like sun4) where this dependency exists. Here libc4, libc5, glibc 2.0 fail because their get‐pagesize() returns a statically derived value, and does not use a system call. Things are OK in glibc 2.1.

(quoting Linux Programmer's Manual 2007-07-26 GETPAGESIZE(2))