Skip to content

Commit 387476b

Browse files
committed
Time::HiRes: tweak Win32 static polyfill _GetSystemTimePreciseAsFileTime()
To summarize, MS's FILETIME type is an 8 bytes long, 64 bit integer, that might aligned to 4 bytes, not 8. SW E-Attorneys, will vigorously argue, MS's FILETIME type, is an 8 byte long C struct, wrapping a union that wraps a U8 array[8]; string that is 8 bytes long. Claiming type FILETIME is a 64 bit int is libel and slander. Since P5P does not publish a C compiler or C linker. That alignment detail for Windows on RISC machine code is irrelavent. This commit was written to preventing redundant re-reads of a C auto U64 from C stack memory to a CPU register around any possible function call, if they exist, and to narrow down the peak width of each caller function's callstack frame on the C stack.
1 parent a9048c9 commit 387476b

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

dist/Time-HiRes/HiRes.xs

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ START_MY_CXT
193193
# define gettimeofday(tp, not_used) _gettimeofday(aTHX_ tp, not_used)
194194

195195
# undef GetSystemTimePreciseAsFileTime
196-
# define GetSystemTimePreciseAsFileTime(out) _GetSystemTimePreciseAsFileTime(aTHX_ out)
196+
# define GetSystemTimePreciseAsFileTime(out) (void)(*(out) = _GetSystemTimePreciseAsFileTime(aTHX))
197197

198198
# undef clock_gettime
199199
# define clock_gettime(clock_id, tp) _clock_gettime(aTHX_ clock_id, tp)
@@ -219,9 +219,28 @@ START_MY_CXT
219219
* Windows 8 introduced GetSystemTimePreciseAsFileTime(), but currently we have
220220
* to support older systems, so for now we provide our own implementation.
221221
* In the future we will switch to the real deal.
222+
*
223+
* FILETIME, switch to "return by copy", vs MS's "return by reference" prototype.
224+
* We never take the fn ptr of static fn _GetSystemTimePreciseAsFileTime(pTHX).
225+
* The MS API GetSystemTimePreciseAsFileTime() has a void return type but we
226+
* have no reason to match ABI compatibility with MS's function symbol.
227+
* Return by copy, encourages CC optimizations, since the C stack FILETIME var
228+
* never escaped the function that declared it. This allows the CC, in the
229+
* caller of _GetSystemTimePreciseAsFileTime(), to keep C stack FILETIME var
230+
* in CPU registers at all times in its function body, if the CC wants to
231+
* do that.
232+
*
233+
* Note even on Win64 x64, where "return by copy" return types > 8 bytes, become
234+
* secret C++ "this"-style first arguments, a > 8 bytes "return by copy" retval
235+
* is still more efficient!!! than explicitly passing a ptr to a C stack alloced
236+
* temporary C struct in C code. The latter requires the CC to re-read the
237+
* temporary C struct each time after any child function call, since the CC
238+
* can't know if SvPV() or GetSystemTimePreciseAsFileTime(), permanently saved
239+
* the pointer for long term Interlocked or Atomic message passing from an
240+
* unknown 2nd OS thread running on another CPU Core.
222241
*/
223-
static void
224-
_GetSystemTimePreciseAsFileTime(pTHX_ FILETIME *out)
242+
static FILETIME
243+
_GetSystemTimePreciseAsFileTime(pTHX)
225244
{
226245
dMY_CXT;
227246
FT_t ft;
@@ -250,9 +269,7 @@ _GetSystemTimePreciseAsFileTime(pTHX_ FILETIME *out)
250269
}
251270
}
252271

253-
*out = ft.ft_val;
254-
255-
return;
272+
return ft.ft_val;
256273
}
257274

258275
static int

0 commit comments

Comments
 (0)