perf(strings): optimize formatting algorithm function

This commit is contained in:
Redstone1024 2025-01-15 17:04:05 +08:00
parent 35f0ba71ab
commit 8a834a9c05

View File

@ -235,8 +235,6 @@ FORCEINLINE constexpr TRangeIterator<R2> Format(R2&& Output, R1&& Fmt, Ts&&... A
// If the output range is insufficient. // If the output range is insufficient.
if (OutIter == OutSent) UNLIKELY return OutIter; if (OutIter == OutSent) UNLIKELY return OutIter;
TTuple<TFormatter<TRemoveCVRef<Ts>, FCharType>...> Formatters;
// For each character in the format string. // For each character in the format string.
for (FCharType Char; FmtIter != FmtSent; ++FmtIter) for (FCharType Char; FmtIter != FmtSent; ++FmtIter)
{ {
@ -315,35 +313,40 @@ FORCEINLINE constexpr TRangeIterator<R2> Format(R2&& Output, R1&& Fmt, Ts&&... A
// Jump over the ':' character. // Jump over the ':' character.
if (Char == LITERAL(FCharType, ':')) ++FmtIter; if (Char == LITERAL(FCharType, ':')) ++FmtIter;
FormatStringContext.AdvanceTo(MoveTemp(FmtIter)); if (FmtIter == FmtSent) UNLIKELY
// Parse the format description string.
FmtIter = Formatters.Visit([&FormatStringContext](auto& Formatter) -> decltype(FmtIter) { return Formatter.Parse(FormatStringContext); }, Index);
if (FmtIter == FmtSent || *FmtIter != LITERAL(FCharType, '}')) UNLIKELY
{ {
checkf(false, TEXT("Illegal format string. Missing '}' in format string.")); checkf(false, TEXT("Illegal format string. Missing '}' in format string."));
break; break;
} }
Char = *FmtIter;
ForwardAsTuple(Forward<Ts>(Args)...).Visit([&]<typename T>(T&& Arg)
{
TFormatter<TRemoveCVRef<T>, FCharType> Formatter;
if (Char != LITERAL(FCharType, '}'))
{
FormatStringContext.AdvanceTo(MoveTemp(FmtIter));
// Parse the format description string.
FmtIter = Formatter.Parse(FormatStringContext);
if (FmtIter == FmtSent || *FmtIter != LITERAL(FCharType, '}')) UNLIKELY
{
checkf(false, TEXT("Illegal format string. Missing '}' in format string."));
return;
}
}
FormatObjectContext.AdvanceTo(MoveTemp(OutIter)); FormatObjectContext.AdvanceTo(MoveTemp(OutIter));
auto FormatHandler = [&]<size_t... Indices>(TIndexSequence<Indices...>) // Format the object and write the result to the context.
{ OutIter = Formatter.Format(Forward<T>(Arg), FormatObjectContext);
TTuple<TConstant<size_t, Indices>...> Visitor;
return Visitor.Visit([&]<size_t ConstantIndex>(TConstant<size_t, ConstantIndex>)
{
check(ConstantIndex == Index);
return Formatters.template GetValue<ConstantIndex>().Format(ForwardAsTuple(Forward<Ts>(Args)...).template GetValue<ConstantIndex>(), FormatObjectContext);
} }
, Index); , Index);
};
// Format the object and write the result to the context.
OutIter = FormatHandler(TIndexSequenceFor<Ts...>());
} }
else else