From f65905dd0ff464774f338db44d69925f98e1766c Mon Sep 17 00:00:00 2001 From: Mike Kaganski Date: Tue, 9 Jul 2019 01:09:35 +0200 Subject: [PATCH] Optimize osl_getSystemTime on Windows Make OffTime static const; don't cast from FILETIME to __int64 (see https://docs.microsoft.com/en-us/windows/win32/api/minwinbase/ns-minwinbase-filetime for explanation: "it can cause alignment faults on 64-bit Windows"). Instead, cast in opposite direction: from 8-byte-aligned 64-bit integer to FILETIME. Change-Id: Iba61cc0198f8f25ef471d87e661c8801724b913d Reviewed-on: https://gerrit.libreoffice.org/75256 Tested-by: Jenkins Reviewed-by: Mike Kaganski --- sal/osl/w32/time.cxx | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/sal/osl/w32/time.cxx b/sal/osl/w32/time.cxx index 135aab368fc8..ae499dcf8a8c 100644 --- a/sal/osl/w32/time.cxx +++ b/sal/osl/w32/time.cxx @@ -29,9 +29,7 @@ sal_Bool SAL_CALL osl_getSystemTime(TimeValue* pTimeVal) { - SYSTEMTIME SystemTime; - FILETIME CurTime, OffTime; - __int64 Value; + unsigned __int64 CurTime; typedef VOID (WINAPI *GetSystemTimePreciseAsFileTime_PROC)(LPFILETIME); @@ -46,25 +44,31 @@ sal_Bool SAL_CALL osl_getSystemTime(TimeValue* pTimeVal) // use ~1 microsecond resolution if available if (pGetSystemTimePreciseAsFileTime) - pGetSystemTimePreciseAsFileTime(&CurTime); + pGetSystemTimePreciseAsFileTime(reinterpret_cast(&CurTime)); else { + SYSTEMTIME SystemTime; GetSystemTime(&SystemTime); - SystemTimeToFileTime(&SystemTime, &CurTime); + SystemTimeToFileTime(&SystemTime, reinterpret_cast(&CurTime)); } - SystemTime.wYear = 1970; - SystemTime.wMonth = 1; - SystemTime.wDayOfWeek = 0; - SystemTime.wDay = 1; - SystemTime.wHour = 0; - SystemTime.wMinute = 0; - SystemTime.wSecond = 0; - SystemTime.wMilliseconds = 0; + static const unsigned __int64 OffTime = [] { + SYSTEMTIME SystemTime; + SystemTime.wYear = 1970; + SystemTime.wMonth = 1; + SystemTime.wDayOfWeek = 0; + SystemTime.wDay = 1; + SystemTime.wHour = 0; + SystemTime.wMinute = 0; + SystemTime.wSecond = 0; + SystemTime.wMilliseconds = 0; - SystemTimeToFileTime(&SystemTime, &OffTime); + unsigned __int64 ft; + SystemTimeToFileTime(&SystemTime, reinterpret_cast(&ft)); + return ft; + }(); - Value = *reinterpret_cast<__int64 *>(&CurTime) - *reinterpret_cast<__int64 *>(&OffTime); + const unsigned __int64 Value = CurTime - OffTime; pTimeVal->Seconds = static_cast(Value / 10000000L); pTimeVal->Nanosec = static_cast((Value % 10000000L) * 100);