@@ -156,12 +156,20 @@ typedef struct {
156
156
unsigned long run_count ;
157
157
} my_cxt_t ;
158
158
159
+ typedef BOOL (WINAPI * pfnQueryPerformanceCounter_T )(LARGE_INTEGER * );
160
+
159
161
static unsigned __int64 tick_frequency = 0 ;
160
162
static unsigned __int64 qpc_res_ns = 0 ;
161
163
static unsigned __int64 qpc_res_ns_realtime = 0 ;
164
+ static pfnQueryPerformanceCounter_T pfnQueryPerformanceCounter = NULL ;
162
165
163
166
#define S_InterlockedExchange64 (_d ,_s ) \
164
167
InterlockedExchange64((LONG64 volatile *)(_d),(LONG64)(_s))
168
+ #define S_InterlockedExchangePointer (_d ,_s ) \
169
+ InterlockedExchangePointer((PVOID volatile *)(_d),(PVOID)(_s))
170
+
171
+ #undef QueryPerformanceCounter
172
+ #define QueryPerformanceCounter pfnQueryPerformanceCounter
165
173
166
174
/* Visual C++ 2013 and older don't have the timespec structure.
167
175
* Neither do mingw.org compilers with MinGW runtimes older than 3.22. */
@@ -259,17 +267,19 @@ _GetSystemTimePreciseAsFileTime(pTHX)
259
267
{
260
268
#define MY_CXTX (*MY_CXT_x)
261
269
unsigned __int64 ticks ;
262
- unsigned __int64 ticks_mem ;
270
+
263
271
unsigned __int64 timesys ;
264
- __int64 diff ;
265
272
/* If no threads, CC will probably optimize away all MY_CXT_x references
266
273
so they directly access the C static global struct. */
267
274
my_cxt_t * MY_CXT_x ;
268
275
269
- QueryPerformanceCounter ((LARGE_INTEGER * )& ticks_mem );
276
+ {
277
+ unsigned __int64 ticks_mem ;
278
+ QueryPerformanceCounter ((LARGE_INTEGER * )& ticks_mem );
270
279
/* Inform the CC nothing external or in this fn (ptr aliasing) can ever
271
280
rewrite the value in ticks. Increases chance of CC using registers. */
272
- ticks = ticks_mem ;
281
+ ticks = ticks_mem ;
282
+ }
273
283
{
274
284
dMY_CXT ;
275
285
MY_CXT_x = & (MY_CXT );
@@ -282,6 +292,7 @@ _GetSystemTimePreciseAsFileTime(pTHX)
282
292
MY_CXTX .reset_time = timesys + MAX_PERF_COUNTER_TICKS ;
283
293
}
284
294
else {
295
+ __int64 diff ;
285
296
ticks -= MY_CXTX .base_ticks ;
286
297
timesys = MY_CXTX .base_systime_as_filetime .ft_i64
287
298
+ Const64 (IV_1E7 ) * (ticks / tick_frequency )
@@ -292,6 +303,7 @@ _GetSystemTimePreciseAsFileTime(pTHX)
292
303
GetSystemTimeAsFileTime (& MY_CXTX .base_systime_as_filetime .ft_val );
293
304
timesys = MY_CXTX .base_systime_as_filetime .ft_i64 ;
294
305
}
306
+ /* Note this invisible else {} branch, SKIPS calling GetSystemTimeAsFileTime() */
295
307
}
296
308
#undef MY_CXTX
297
309
{
@@ -1002,7 +1014,7 @@ S_croak_xs_unimplemented(const CV *const cv)
1002
1014
SV * sv = cv_name (cv , NULL , 0 );
1003
1015
Perl_croak_nocontext (
1004
1016
"%s::%s(): unimplemented in this platform" + (sizeof ("%s::" )- 1 ), SvPVX (sv ));
1005
- #if 0
1017
+ #if 0 /* former implementation, retired because of machine code bloat */
1006
1018
char buf [sizeof ("CODE(0x%" UVxf ")" ) + (sizeof (UV )* 8 )];
1007
1019
const char * pv1 ;
1008
1020
const GV * const gv = CvGV (cv );
@@ -1066,6 +1078,25 @@ BOOT:
1066
1078
l_qpc_res_ns_realtime = l_qpc_res_ns > 100 ? l_qpc_res_ns : 100 ;
1067
1079
S_InterlockedExchange64 (& qpc_res_ns_realtime , l_qpc_res_ns_realtime );
1068
1080
}
1081
+ {/* Remove a couple jump stub funcs between kernel32->kernelbase->ntdll
1082
+ for perf reasons. RtlQueryPerformanceCounter() was added in NT 6.1,
1083
+ so a fallback path is still required to QPC()@K32.dll. */
1084
+ pfnQueryPerformanceCounter_T QPCfn = pfnQueryPerformanceCounter ;
1085
+ if (!QPCfn ) {
1086
+ HMODULE hmod = GetModuleHandleW (L"NTDLL.DLL" );
1087
+ if (hmod ) {
1088
+ QPCfn = (pfnQueryPerformanceCounter_T )GetProcAddress (hmod ,"RtlQueryPerformanceCounter" );
1089
+ if (QPCfn )
1090
+ goto QPC_done ;
1091
+ }
1092
+ #undef QueryPerformanceCounter
1093
+ QPCfn = QueryPerformanceCounter ; /* Get the public API fallback sym. */
1094
+ #undef QueryPerformanceCounter
1095
+ #QueryPerformanceCounter pfnQueryPerformanceCounter
1096
+ QPC_done :
1097
+ S_InterlockedExchangePointer (& pfnQueryPerformanceCounter , QPCfn );
1098
+ }
1099
+ }
1069
1100
}
1070
1101
#endif
1071
1102
#ifdef HAS_GETTIMEOFDAY
0 commit comments