// The format and arguments to append. const uint32_t format_; const uint32_t* const args_;
// References are moved to the handle scope during CalculateLengthWithFlag().
StackHandleScope<kMaxArgs> hs_;
// We convert float/double values using jdk.internal.math.FloatingDecimal which uses // a thread-local converter under the hood. As we may have more than one // float/double argument, we need to copy the data out of the converter. // Maximum number of characters is 26. See BinaryToASCIIBuffer.buffer in FloatingDecimal.java . // (This is more than enough for the `ExceptionalBinaryToASCIIBuffer` cases.) static constexpr size_t kBinaryToASCIIBufferSize = 26;
uint8_t converted_fp_args_[kMaxArgs][kBinaryToASCIIBufferSize];
int32_t converted_fp_arg_lengths_[kMaxArgs];
// The length and flag to store when the AppendBuilder is used as a pre-fence visitor.
int32_t length_with_flag_ = 0u;
};
inline size_t StringBuilderAppend::Builder::Uint64Length(uint64_t value) { if (value == 0u) { return1u;
} // Calculate floor(log2(value)).
size_t log2_value = BitSizeOf<uint64_t>() - 1u - CLZ(value); // Calculate an estimate of floor(log10(value)). // log10(2) = 0.301029996 > 0.296875 = 19/64 // floor(log10(v)) == floor(log2(v) * log10(2)) // >= floor(log2(v) * 19/64) // >= floor(floor(log2(v)) * 19/64) // This estimate is no more that one off from the actual value because log2(value) < 64 and thus // log2(v) * log10(2) - log2(v) * 19/64 < 64*(log10(2) - 19/64) // for the first approximation and // log2(v) * 19/64 - floor(log2(v)) * 19/64 < 19/64 // for the second one. Together, // 64*(log10(2) - 19/64) + 19/64 = 0.56278 < 1 .
size_t log10_value_estimate = log2_value * 19u / 64u; static constexpr uint64_t bounds[] = {
UINT64_C(9),
UINT64_C(99),
UINT64_C(999),
UINT64_C(9999),
UINT64_C(99999),
UINT64_C(999999),
UINT64_C(9999999),
UINT64_C(99999999),
UINT64_C(999999999),
UINT64_C(9999999999),
UINT64_C(99999999999),
UINT64_C(999999999999),
UINT64_C(9999999999999),
UINT64_C(99999999999999),
UINT64_C(999999999999999),
UINT64_C(9999999999999999),
UINT64_C(99999999999999999),
UINT64_C(999999999999999999),
UINT64_C(9999999999999999999),
}; // Add 1 for the lowest digit, add another 1 if the estimate was too low.
DCHECK_LT(log10_value_estimate, std::size(bounds));
size_t adjustment = (value > bounds[log10_value_estimate]) ? 2u : 1u; return log10_value_estimate + adjustment;
}
DCHECK_LE(length, RemainingSpace(new_string, data)); for (size_t i = 0; i != length; ++i) {
data[i] = literal[i];
} return data + length;
}
template <typename CharType> inline CharType* StringBuilderAppend::Builder::AppendString(ObjPtr<mirror::String> new_string,
CharType* data,
ObjPtr<mirror::String> str) {
size_t length = dchecked_integral_cast<size_t>(str->GetLength());
DCHECK_LE(length, RemainingSpace(new_string, data)); if (sizeof(CharType) == sizeof(uint8_t) || str->IsCompressed()) {
DCHECK(str->IsCompressed()); const uint8_t* value = str->GetValueCompressed(); for (size_t i = 0; i != length; ++i) {
data[i] = value[i];
}
} else { const uint16_t* value = str->GetValue(); for (size_t i = 0; i != length; ++i) {
data[i] = dchecked_integral_cast<CharType>(value[i]);
}
} return data + length;
}
template <typename CharType> inline CharType* StringBuilderAppend::Builder::AppendInt64(ObjPtr<mirror::String> new_string,
CharType* data,
int64_t value) {
DCHECK_GE(RemainingSpace(new_string, data), Int64Length(value));
uint64_t v = static_cast<uint64_t>(value); if (value < 0) {
*data = '-';
++data;
v = -v;
}
size_t length = Uint64Length(v); // Write the digits from the end, do not write the most significant digit // in the loop to avoid an unnecessary division. for (size_t i = 1; i != length; ++i) {
uint64_t digit = v % UINT64_C(10);
v /= UINT64_C(10);
data[length - i] = '0' + static_cast<char>(digit);
}
DCHECK_LE(v, 10u);
*data = '0' + static_cast<char>(v); return data + length;
}
int32_t StringBuilderAppend::Builder::ConvertFpArgs() {
int32_t fp_args_length = 0u; const uint32_t* current_arg = args_;
size_t fp_arg_index = 0u; for (uint32_t f = format_; f != 0u; f >>= kBitsPerArg) {
DCHECK_LE(f & kArgMask, static_cast<uint32_t>(Argument::kLast)); bool fp_arg = false;
ObjPtr<mirror::Object> converter; switch (static_cast<Argument>(f & kArgMask)) { case Argument::kString: case Argument::kBoolean: case Argument::kChar: case Argument::kInt: break; case Argument::kLong: {
current_arg = AlignUp(current_arg, sizeof(int64_t));
++current_arg; // Skip the low word, let the common code skip the high word. break;
} case Argument::kFloat: {
fp_arg = true; float arg = bit_cast<float>(*current_arg);
converter = WellKnownClasses::jdk_internal_math_FloatingDecimal_getBinaryToASCIIConverter_F
->InvokeStatic<'L', 'F'>(hs_.Self(), arg); break;
} case Argument::kDouble: {
fp_arg = true;
current_arg = AlignUp(current_arg, sizeof(int64_t)); double arg = bit_cast<double>( static_cast<uint64_t>(current_arg[0]) + (static_cast<uint64_t>(current_arg[1]) << 32));
converter = WellKnownClasses::jdk_internal_math_FloatingDecimal_getBinaryToASCIIConverter_D
->InvokeStatic<'L', 'D'>(hs_.Self(), arg);
++current_arg; // Skip the low word, let the common code skip the high word. break;
} case Argument::kStringBuilder: case Argument::kCharArray: case Argument::kObject:
LOG(FATAL) << "Unimplemented arg format: 0x" << std::hex
<< (f & kArgMask) << " full format: 0x" << std::hex << format_;
UNREACHABLE(); default:
LOG(FATAL) << "Unexpected arg format: 0x" << std::hex
<< (f & kArgMask) << " full format: 0x" << std::hex << format_;
UNREACHABLE();
} if (fp_arg) { // If we see an exception (presumably OOME or SOE), keep it as is, even // though it may be confusing to see the stack trace for FP argument // conversion continue at the StringBuilder.toString() invoke location.
DCHECK_EQ(converter == nullptr, hs_.Self()->IsExceptionPending()); if (UNLIKELY(converter == nullptr)) { return -1;
}
int32_t length; if (converter->GetClass() ==
WellKnownClasses::jdk_internal_math_FloatingDecimal_BinaryToASCIIBuffer.Get()) { // Call `converter.getChars(converter.buffer)`.
StackHandleScope<1u> hs2(hs_.Self());
ArtField* btab_buffer_field =
WellKnownClasses::jdk_internal_math_FloatingDecimal_BinaryToASCIIBuffer_buffer;
Handle<mirror::CharArray> buffer =
hs2.NewHandle(btab_buffer_field->GetObj<mirror::CharArray>(converter));
DCHECK(buffer != nullptr);
length = WellKnownClasses::jdk_internal_math_FloatingDecimal_BinaryToASCIIBuffer_getChars
->InvokeInstance<'I', 'L'>(hs_.Self(), converter, buffer.Get()); if (UNLIKELY(hs_.Self()->IsExceptionPending())) { return -1;
} // The converted string is now at the front of the buffer.
DCHECK_GT(length, 0);
DCHECK_LE(length, buffer->GetLength());
DCHECK_LE(static_cast<size_t>(length), std::size(converted_fp_args_[0]));
DCHECK(mirror::String::AllASCII(buffer->GetData(), length));
std::copy_n(buffer->GetData(), length, converted_fp_args_[fp_arg_index]);
} else {
DCHECK(converter->GetClass() ==
WellKnownClasses::jdk_internal_math_FloatingDecimal_ExceptionalBinaryToASCIIBuffer
.Get());
ArtField* ebtab_image_field = WellKnownClasses::
jdk_internal_math_FloatingDecimal_ExceptionalBinaryToASCIIBuffer_image;
ObjPtr<mirror::String> converted = ebtab_image_field->GetObj<mirror::String>(converter);
DCHECK(converted != nullptr);
length = converted->GetLength(); if (mirror::kUseStringCompression) {
DCHECK(converted->IsCompressed());
memcpy(converted_fp_args_[fp_arg_index], converted->GetValueCompressed(), length);
} else {
DCHECK(mirror::String::AllASCII(converted->GetValue(), length));
std::copy_n(converted->GetValue(), length, converted_fp_args_[fp_arg_index]);
}
}
converted_fp_arg_lengths_[fp_arg_index] = length;
fp_args_length += length;
++fp_arg_index;
}
++current_arg;
DCHECK_LE(fp_arg_index, kMaxArgs);
} return fp_args_length;
}
inline int32_t StringBuilderAppend::Builder::CalculateLengthWithFlag() {
static_assert(static_cast<size_t>(Argument::kEnd) == 0u, "kEnd must be 0."); bool compressible = mirror::kUseStringCompression;
uint64_t length = 0u; bool has_fp_args = false; const uint32_t* current_arg = args_; for (uint32_t f = format_; f != 0u; f >>= kBitsPerArg) {
DCHECK_LE(f & kArgMask, static_cast<uint32_t>(Argument::kLast)); switch (static_cast<Argument>(f & kArgMask)) { case Argument::kString: {
Handle<mirror::String> str =
hs_.NewHandle(reinterpret_cast32<mirror::String*>(*current_arg)); if (str != nullptr) {
length += str->GetLength();
compressible = compressible && str->IsCompressed();
} else {
length += kNullLength;
} break;
} case Argument::kBoolean: {
length += (*current_arg != 0u) ? kTrueLength : kFalseLength; break;
} case Argument::kChar: {
length += 1u;
compressible = compressible &&
mirror::String::IsASCII(reinterpret_cast<const uint16_t*>(current_arg)[0]); break;
} case Argument::kInt: {
length += Int64Length(static_cast<int32_t>(*current_arg)); break;
} case Argument::kLong: {
current_arg = AlignUp(current_arg, sizeof(int64_t));
length += Int64Length(*reinterpret_cast<const int64_t*>(current_arg));
++current_arg; // Skip the low word, let the common code skip the high word. break;
} case Argument::kDouble:
current_arg = AlignUp(current_arg, sizeof(int64_t));
++current_arg; // Skip the low word, let the common code skip the high word.
FALLTHROUGH_INTENDED; case Argument::kFloat: // Conversion shall be performed in a separate pass because it calls back to // managed code and we need to convert reference arguments to `Handle<>`s first.
has_fp_args = true; break;
if (UNLIKELY(has_fp_args)) { // Call Java helpers to convert FP args.
int32_t fp_args_length = ConvertFpArgs(); if (fp_args_length == -1) { return -1;
}
DCHECK_GT(fp_args_length, 0);
length += fp_args_length;
}
if (length > std::numeric_limits<int32_t>::max()) { // We cannot allocate memory for the entire result.
hs_.Self()->ThrowNewException("Ljava/lang/OutOfMemoryError;", "Out of memory for StringBuilder append."); return -1;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.