Improve string formatting performance, Fix crash when compiled with latest CRT

This commit is contained in:
dmex
2020-05-01 23:29:42 +10:00
parent 0627db2941
commit b06b8c50a4
7 changed files with 157 additions and 59 deletions
+6
View File
@@ -144,6 +144,7 @@
<StringPooling>true</StringPooling>
<ControlFlowGuard>Guard</ControlFlowGuard>
<IntelJCCErratum>true</IntelJCCErratum>
<LanguageStandard>stdcpplatest</LanguageStandard>
</ClCompile>
<Link>
<AdditionalDependencies>aclui.lib;comctl32.lib;dnsapi.lib;iphlpapi.lib;noarg.obj;noenv.obj;ntdll.lib;phlib.lib;shlwapi.lib;userenv.lib;uxtheme.lib;version.lib;wbemuuid.lib;windowscodecs.lib;winhttp.lib;winsta.lib;ws2_32.lib;%(AdditionalDependencies)</AdditionalDependencies>
@@ -179,6 +180,7 @@
<StringPooling>true</StringPooling>
<ControlFlowGuard>Guard</ControlFlowGuard>
<IntelJCCErratum>true</IntelJCCErratum>
<LanguageStandard>stdcpplatest</LanguageStandard>
</ClCompile>
<Link>
<AdditionalDependencies>aclui.lib;comctl32.lib;dnsapi.lib;iphlpapi.lib;noarg.obj;noenv.obj;ntdll.lib;phlib.lib;shlwapi.lib;userenv.lib;uxtheme.lib;version.lib;wbemuuid.lib;windowscodecs.lib;winhttp.lib;winsta.lib;ws2_32.lib;%(AdditionalDependencies)</AdditionalDependencies>
@@ -212,6 +214,7 @@
<TreatWarningAsError>true</TreatWarningAsError>
<MultiProcessorCompilation>true</MultiProcessorCompilation>
<StringPooling>true</StringPooling>
<LanguageStandard>stdcpplatest</LanguageStandard>
</ClCompile>
<Link>
<AdditionalDependencies>aclui.lib;comctl32.lib;dnsapi.lib;iphlpapi.lib;noarg.obj;noenv.obj;ntdll.lib;phlib.lib;shlwapi.lib;userenv.lib;uxtheme.lib;version.lib;wbemuuid.lib;windowscodecs.lib;winhttp.lib;winsta.lib;ws2_32.lib;%(AdditionalDependencies)</AdditionalDependencies>
@@ -250,6 +253,7 @@
<EnableEnhancedInstructionSet>StreamingSIMDExtensions</EnableEnhancedInstructionSet>
<ControlFlowGuard>Guard</ControlFlowGuard>
<IntelJCCErratum>true</IntelJCCErratum>
<LanguageStandard>stdcpplatest</LanguageStandard>
</ClCompile>
<Link>
<AdditionalDependencies>aclui.lib;comctl32.lib;dnsapi.lib;iphlpapi.lib;noarg.obj;noenv.obj;ntdll.lib;phlib.lib;shlwapi.lib;userenv.lib;uxtheme.lib;version.lib;wbemuuid.lib;windowscodecs.lib;winhttp.lib;winsta.lib;ws2_32.lib;%(AdditionalDependencies)</AdditionalDependencies>
@@ -289,6 +293,7 @@
<MultiProcessorCompilation>true</MultiProcessorCompilation>
<ControlFlowGuard>Guard</ControlFlowGuard>
<IntelJCCErratum>true</IntelJCCErratum>
<LanguageStandard>stdcpplatest</LanguageStandard>
</ClCompile>
<Link>
<AdditionalDependencies>aclui.lib;comctl32.lib;dnsapi.lib;iphlpapi.lib;noarg.obj;noenv.obj;ntdll.lib;phlib.lib;shlwapi.lib;userenv.lib;uxtheme.lib;version.lib;wbemuuid.lib;windowscodecs.lib;winhttp.lib;winsta.lib;ws2_32.lib;%(AdditionalDependencies)</AdditionalDependencies>
@@ -327,6 +332,7 @@
<TreatWarningAsError>true</TreatWarningAsError>
<MultiProcessorCompilation>true</MultiProcessorCompilation>
<ControlFlowGuard>Guard</ControlFlowGuard>
<LanguageStandard>stdcpplatest</LanguageStandard>
</ClCompile>
<Link>
<AdditionalDependencies>aclui.lib;comctl32.lib;dnsapi.lib;iphlpapi.lib;noarg.obj;noenv.obj;ntdll.lib;phlib.lib;shlwapi.lib;userenv.lib;uxtheme.lib;version.lib;wbemuuid.lib;windowscodecs.lib;winhttp.lib;winsta.lib;ws2_32.lib;%(AdditionalDependencies)</AdditionalDependencies>
+38 -29
View File
@@ -3,6 +3,7 @@
* string formatting
*
* Copyright (C) 2010-2015 wj32
* Copyright (C) 2020 dmex
*
* This file is part of Process Hacker.
*
@@ -44,11 +45,6 @@ extern ULONG PhMaxSizeUnit;
#define PHP_FORMAT_POSITIVE 0x2
#define PHP_FORMAT_PAD 0x4
// Internal CRT routine needed for floating-point conversion
errno_t __cdecl _cfltcvt_l(double *arg, char *buffer, size_t sizeInBytes,
int format, int precision, int caps, _locale_t plocinfo);
// Keep in sync with PhSizeUnitNames
static PH_STRINGREF PhpSizeUnitNamesCounted[7] =
{
@@ -66,35 +62,48 @@ static WCHAR PhpFormatDecimalSeparator = L'.';
static WCHAR PhpFormatThousandSeparator = L',';
static _locale_t PhpFormatUserLocale = NULL;
#if (_MSC_VER >= 1900)
// See Source\10.0.10150.0\ucrt\convert\cvt.cpp in SDK v10.
errno_t __cdecl __acrt_fp_format(
double const* const value,
char* const result_buffer,
size_t const result_buffer_count,
char* const scratch_buffer,
size_t const scratch_buffer_count,
int const format,
int const precision,
UINT64 const options,
_locale_t const locale
);
static errno_t __cdecl _cfltcvt_l(double *arg, char *buffer, size_t sizeInBytes,
int format, int precision, int caps, _locale_t plocinfo)
VOID PhpFormatDoubleToUtf8Locale(
_In_ DOUBLE Value,
_In_ ULONG Type,
_In_ INT32 Precision,
_Out_writes_bytes_opt_(BufferLength) PSTR Buffer,
_In_opt_ SIZE_T BufferLength,
_Out_opt_ PSIZE_T ReturnLength
)
{
char scratch_buffer[_CVTBUFSIZE + 1];
PhFormatDoubleToUtf8(
Value,
Type,
Precision,
Buffer,
BufferLength,
NULL
);
if (caps & 1)
format -= 32; // Make uppercase
if (PhpFormatUserLocale)
{
for (PCH c = Buffer; *c; ++c)
{
if (*c == '.')
{
*c = (CHAR)PhpFormatDecimalSeparator;
break;
}
}
}
return __acrt_fp_format(arg, buffer, sizeInBytes, scratch_buffer, sizeof(scratch_buffer),
format, precision, 0, plocinfo);
if (Type & FormatUpperCase)
{
if (PhpFormatUserLocale)
_strupr_l(Buffer, PhpFormatUserLocale);
else
_strupr(Buffer);
//for (PCH c = Buffer; *c; ++c)
// *c = RtlUpperChar(*c);
}
}
#endif
// From Source\10.0.10150.0\ucrt\inc\corecrt_internal_stdio_output.h in SDK v10.
VOID PhpCropZeros(
_Inout_ PCHAR Buffer,
+19 -30
View File
@@ -32,8 +32,8 @@
PhpFormatThousandSeparator = localeBuffer[0];
}
if (PhpFormatDecimalSeparator != '.')
PhpFormatUserLocale = _create_locale(LC_ALL, "");
if (PhpFormatDecimalSeparator != L'.')
PhpFormatUserLocale = _wcreate_locale(LC_ALL, L"");
PhEndInitOnce(&PhpFormatInitOnce);
}
@@ -179,7 +179,7 @@
} \
else \
{ \
*temp-- = '0'; \
*temp-- = L'0'; \
tempCount++; \
} \
\
@@ -205,13 +205,13 @@
if (OK_BUFFER) \
{ \
if (flags & PHP_FORMAT_NEGATIVE) \
*buffer++ = '-'; \
*buffer++ = L'-'; \
else if ((Format)->Type & FormatPrefixSign) \
*buffer++ = '+'; \
*buffer++ = L'+'; \
\
if (flags & PHP_FORMAT_PAD) \
{ \
wmemset(buffer, '0', padCount); \
wmemset(buffer, L'0', padCount); \
buffer += padCount; \
} \
\
@@ -284,7 +284,6 @@ CommonInt64Format:
do { \
ULONG precision; \
DOUBLE value; \
CHAR c; \
PSTR temp; \
ULONG length; \
\
@@ -300,25 +299,15 @@ CommonInt64Format:
precision = 6; \
} \
\
c = 'f'; \
\
if ((Format)->Type & FormatStandardForm) \
c = 'e'; \
else if ((Format)->Type & FormatHexadecimalForm) \
c = 'a'; \
\
/* Use MS CRT routines to do the work. */ \
\
value = (Format)->u.Double; \
temp = (PSTR)tempBuffer + 1; /* leave one character so we can insert a prefix if needed */ \
_cfltcvt_l( \
&value, \
PhpFormatDoubleToUtf8Locale( \
value, \
(Format)->Type, \
precision, \
temp, \
sizeof(tempBuffer) - 1, \
c, \
precision, \
!!((Format)->Type & FormatUpperCase), \
PhpFormatUserLocale \
NULL \
); \
\
/* if (((Format)->Type & FormatForceDecimalPoint) && precision == 0) */ \
@@ -379,9 +368,9 @@ CommonInt64Format:
if (OK_BUFFER) \
{ \
if (flags & PHP_FORMAT_NEGATIVE) \
*buffer++ = '-'; \
*buffer++ = L'-'; \
else if (flags & PHP_FORMAT_POSITIVE) \
*buffer++ = '+'; \
*buffer++ = L'+'; \
\
while (copyCount--) \
{ \
@@ -437,13 +426,13 @@ CommonInt64Format:
if (OK_BUFFER) \
{ \
if (flags & PHP_FORMAT_NEGATIVE) \
*buffer++ = '-'; \
*buffer++ = L'-'; \
else if (flags & PHP_FORMAT_POSITIVE) \
*buffer++ = '+'; \
*buffer++ = L'+'; \
\
if (flags & PHP_FORMAT_PAD) \
{ \
wmemset(buffer, '0', padLength); \
wmemset(buffer, L'0', padLength); \
buffer += padLength; \
} \
} \
@@ -478,7 +467,7 @@ CommonInt64Format:
{
ENSURE_BUFFER(sizeof(WCHAR));
if (OK_BUFFER)
*buffer = '0';
*buffer = L'0';
ADVANCE_BUFFER(sizeof(WCHAR));
goto ContinueLoop;
}
@@ -510,7 +499,7 @@ CommonInt64Format:
ENSURE_BUFFER(sizeof(WCHAR) + PhpSizeUnitNamesCounted[i].Length);
if (OK_BUFFER)
{
*buffer = ' ';
*buffer = L' ';
memcpy(buffer + 1, PhpSizeUnitNamesCounted[i].Buffer, PhpSizeUnitNamesCounted[i].Length);
}
ADVANCE_BUFFER(sizeof(WCHAR) + PhpSizeUnitNamesCounted[i].Length);
@@ -541,7 +530,7 @@ ContinueLoop:
if (format->Type & FormatUsePad)
pad = format->Pad;
else
pad = ' ';
pad = L' ';
if (format->Type & FormatLeftAlign)
{
+70
View File
@@ -0,0 +1,70 @@
/*
* Process Hacker -
* string formatting
*
* Copyright (C) 2020 dmex
*
* This file is part of Process Hacker.
*
* Process Hacker is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Process Hacker is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Process Hacker. If not, see <http://www.gnu.org/licenses/>.
*/
#include <phbase.h>
#include <charconv>
using namespace std;
BOOLEAN PhFormatDoubleToUtf8(
_In_ DOUBLE Value,
_In_ ULONG Type,
_In_ ULONG Precision,
_Out_writes_bytes_opt_(BufferLength) PSTR Buffer,
_In_opt_ SIZE_T BufferLength,
_Out_opt_ PSIZE_T ReturnLength
)
{
chars_format format = chars_format::fixed;
to_chars_result result;
SIZE_T returnLength;
CHAR buffer[_CVTBUFSIZE + 1];
if (Type & FormatStandardForm)
format = chars_format::general;
else if (Type & FormatHexadecimalForm)
format = chars_format::hex;
result = to_chars(
buffer,
end(buffer),
Value,
format,
Precision
);
if (result.ec == errc::value_too_large)
return FALSE;
returnLength = result.ptr - buffer;
if (returnLength == 0)
return FALSE;
memcpy_s(Buffer, BufferLength, buffer, returnLength);
Buffer[returnLength] = ANSI_NULL;
if (ReturnLength)
*ReturnLength = returnLength;
return TRUE;
}
+12
View File
@@ -3627,6 +3627,18 @@ PhFormatToBuffer(
_Out_opt_ PSIZE_T ReturnLength
);
PHLIBAPI
BOOLEAN
NTAPI
PhFormatDoubleToUtf8(
_In_ DOUBLE Value,
_In_ ULONG Type,
_In_ ULONG Precision,
_Out_writes_bytes_opt_(BufferLength) PSTR Buffer,
_In_opt_ SIZE_T BufferLength,
_Out_opt_ PSIZE_T ReturnLength
);
// error
PHLIBAPI
+9
View File
@@ -45,6 +45,7 @@
<CharacterSet>Unicode</CharacterSet>
<PlatformToolset>v142</PlatformToolset>
<SpectreMitigation>false</SpectreMitigation>
<UseDebugLibraries>true</UseDebugLibraries>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>StaticLibrary</ConfigurationType>
@@ -65,6 +66,7 @@
<CharacterSet>Unicode</CharacterSet>
<PlatformToolset>v142</PlatformToolset>
<SpectreMitigation>false</SpectreMitigation>
<UseDebugLibraries>true</UseDebugLibraries>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|ARM64'" Label="Configuration">
<ConfigurationType>StaticLibrary</ConfigurationType>
@@ -121,6 +123,7 @@
<CallingConvention>StdCall</CallingConvention>
<TreatWarningAsError>true</TreatWarningAsError>
<MultiProcessorCompilation>true</MultiProcessorCompilation>
<LanguageStandard>stdcpplatest</LanguageStandard>
</ClCompile>
<Lib>
<TargetMachine>MachineX86</TargetMachine>
@@ -140,6 +143,7 @@
<TreatWarningAsError>true</TreatWarningAsError>
<MultiProcessorCompilation>true</MultiProcessorCompilation>
<IntelJCCErratum>true</IntelJCCErratum>
<LanguageStandard>stdcpplatest</LanguageStandard>
</ClCompile>
<Lib>
<TargetMachine>MachineX64</TargetMachine>
@@ -157,6 +161,7 @@
<DebugInformationFormat>ProgramDatabase</DebugInformationFormat>
<TreatWarningAsError>true</TreatWarningAsError>
<MultiProcessorCompilation>true</MultiProcessorCompilation>
<LanguageStandard>stdcpplatest</LanguageStandard>
</ClCompile>
<Lib>
<TargetMachine>MachineARM64</TargetMachine>
@@ -178,6 +183,7 @@
<MultiProcessorCompilation>true</MultiProcessorCompilation>
<EnableEnhancedInstructionSet>StreamingSIMDExtensions</EnableEnhancedInstructionSet>
<ControlFlowGuard>Guard</ControlFlowGuard>
<LanguageStandard>stdcpplatest</LanguageStandard>
</ClCompile>
<Lib>
<TargetMachine>MachineX86</TargetMachine>
@@ -199,6 +205,7 @@
<MultiProcessorCompilation>true</MultiProcessorCompilation>
<ControlFlowGuard>Guard</ControlFlowGuard>
<IntelJCCErratum>true</IntelJCCErratum>
<LanguageStandard>stdcpplatest</LanguageStandard>
</ClCompile>
<Lib>
<TargetMachine>MachineX64</TargetMachine>
@@ -218,6 +225,7 @@
<TreatWarningAsError>true</TreatWarningAsError>
<MultiProcessorCompilation>true</MultiProcessorCompilation>
<ControlFlowGuard>Guard</ControlFlowGuard>
<LanguageStandard>stdcpplatest</LanguageStandard>
</ClCompile>
<Lib>
<TargetMachine>MachineARM64</TargetMachine>
@@ -239,6 +247,7 @@
<ClCompile Include="fastlock.c" />
<ClCompile Include="filepool.c" />
<ClCompile Include="format.c" />
<ClCompile Include="format_std.cpp" />
<ClCompile Include="global.c" />
<ClCompile Include="graph.c" />
<ClCompile Include="guisup.c" />
+3
View File
@@ -245,6 +245,9 @@
<ClCompile Include="http.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="format_std.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="include\circbuf.h">