I’m looking to improve the D garbage collector by adding some heuristics to avoid garbage collection runs that are unlikely to result in significant freeing. One heuristic I’d like to add is that GC should not be run more than once per X amount of time (maybe once per second or so). To do this I need a timer with the following properties:
-
It must be able to grab the correct time with minimal overhead. Calling
core.stdc.timetakes an amount of time roughly equivalent to a small memory allocation, so it’s not a good option. -
Ideally, should be cross-platform (both OS and CPU), for maintenance simplicity.
-
Super high resolution isn’t terribly important. If the times are accurate to maybe 1/4 of a second, that’s good enough.
-
Must work in a multithreaded/multi-CPU context. The x86
rdtscinstruction won’t work.
EDIT: The plain old C function clock() seems fast enough. However, here overflow is a problem. On 32-bit Windows and Linux, clock_t is defined as a 32-bit signed integer. When it overflows, does it become negative, or does the clock() function use extra logic to make it wrap to zero instead? If it wraps to zero, then this will do the trick. If it wraps to negative (which also represents error codes, etc.) then it isn’t going to work.
Edit # 2: I tried the heuristic out anyhow, using clock() and ignoring the overflow issue, just as a test. It performs so poorly that it’s not worth further investigation.
I think that brings you down to whatever you can find in the standard C library.
Perhaps clock?