11508 shaares
421 private links
421 private links
Reading a file is actually slow.
getCurrentThreadUserTime() uses many syscalls because it reads from /proc.
clock_gettime(CLOCK_THREAD_CPUTIME_ID) has only one syscall and a direct function call chain.
The optimisation can be done, but:
- The kernel policy is clear: don't break userspace
- It's undocumented anywhere!
- Author's take: if glibc depends on it, it's not going away.
This is why I like browsing commits of large open source projects. A 40-line deletion eliminated a 400x performance gap. The fix required no new kernel features, just knowledge of a stable-but-obscure Linux ABI detail.
The lessons:
- read the kernel source. POSIX tells what's portable; the kernel source code tells what's possible.
- check the old assumptions: revisiting them occasionally pays off.