Code design/optimization

hellisher wrote on Thursday, October 10, 2013:

First of all. I have followed glfw for some time and like the basic functions of it. :slight_smile:
But i have been looking into the code of glfw and there seams to be some unnecessary overhead for functions/design flaw.
For example when calling: glfwGetTime() that functions calls: _glfwPlatformGetTime() and that functions calls: getRawTime() and that functions calls the OS timer function.
Why not make just make glfwGetTime get the time from the OS directly? Could easily be done with #ifdef for example.

Just my thoughts.

dougbinks wrote on Thursday, October 10, 2013:

Most compilers can optimize away internal function calls like these. The design makes platform changes easier and safer to make since any changes are local to a given platform.

elmindreda wrote on Thursday, October 10, 2013:

It’s done that way because GLFW isn’t nearly performance critical enough to violate consistent design and any cost a few function calls within GLFW incurs is dwarfed by the actual syscall. Then there are the optimisations Doug mentions.

Some of the _glfwPlatform functions also perform a lot of work and all of them perform at least some. Duplicating that code across every platform’s implementation would make the code larger, more bug-prone and difficult to maintain. You can see the ifdef model used in freeglut, although they are now moving away from it due to the difficulty of supporting more than two platforms with it.