[factory] Make NewByteArray return canonical empty byte array

... for length = 0, analogously to what e.g. NewFixedArray does.

Simplify some call sites that had special handling for this case
(there are others that didn't).

Change-Id: Ib3de5506300e967aca072fad53df7ab04ef68839
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3009225
Reviewed-by: Leszek Swirski <leszeks@chromium.org>
Commit-Queue: Georg Neis <neis@chromium.org>
Cr-Commit-Position: refs/heads/master@{#75629}
This commit is contained in:
Georg Neis 2021-07-07 14:12:19 +02:00 committed by V8 LUCI CQ
parent 9a914dd9f0
commit 8d3c809349
6 changed files with 5 additions and 8 deletions

View File

@ -60,7 +60,6 @@ namespace baseline {
template <typename IsolateT>
Handle<ByteArray> BytecodeOffsetTableBuilder::ToBytecodeOffsetTable(
IsolateT* isolate) {
if (bytes_.empty()) return isolate->factory()->empty_byte_array();
Handle<ByteArray> table = isolate->factory()->NewByteArray(
static_cast<int>(bytes_.size()), AllocationType::kOld);
MemCopy(table->GetDataStartAddress(), bytes_.data(), bytes_.size());

View File

@ -173,9 +173,7 @@ void SourcePositionTableBuilder::AddEntry(const PositionTableEntry& entry) {
template <typename IsolateT>
Handle<ByteArray> SourcePositionTableBuilder::ToSourcePositionTable(
IsolateT* isolate) {
if (bytes_.empty()) return isolate->factory()->empty_byte_array();
DCHECK(!Omit());
Handle<ByteArray> table = isolate->factory()->NewByteArray(
static_cast<int>(bytes_.size()), AllocationType::kOld);
MemCopy(table->GetDataStartAddress(), bytes_.data(), bytes_.size());

View File

@ -983,10 +983,6 @@ Handle<PodArray<InliningPosition>> CreateInliningPositions(
OptimizedCompilationInfo* info, Isolate* isolate) {
const OptimizedCompilationInfo::InlinedFunctionList& inlined_functions =
info->inlined_functions();
if (inlined_functions.size() == 0) {
return Handle<PodArray<InliningPosition>>::cast(
isolate->factory()->empty_byte_array());
}
Handle<PodArray<InliningPosition>> inl_positions =
PodArray<InliningPosition>::New(
isolate, static_cast<int>(inlined_functions.size()),

View File

@ -170,6 +170,7 @@ Handle<ByteArray> FactoryBase<Impl>::NewByteArray(int length,
FATAL("Fatal JavaScript invalid size error %d", length);
UNREACHABLE();
}
if (length == 0) return impl()->empty_byte_array();
int size = ByteArray::SizeFor(length);
HeapObject result = AllocateRawWithImmortalMap(
size, allocation, read_only_roots().byte_array_map());

View File

@ -115,15 +115,17 @@ class EXPORT_TEMPLATE_DECLARE(V8_EXPORT_PRIVATE) FactoryBase
int length, AllocationType allocation = AllocationType::kYoung);
// Allocates a weak fixed array-like object with given map and initialized
// with undefined values.
// with undefined values. Length must be > 0.
Handle<WeakFixedArray> NewWeakFixedArrayWithMap(
Map map, int length, AllocationType allocation = AllocationType::kYoung);
// Allocates a fixed array which may contain in-place weak references. The
// array is initialized with undefined values
// The function returns a pre-allocated empty weak fixed array for length = 0.
Handle<WeakFixedArray> NewWeakFixedArray(
int length, AllocationType allocation = AllocationType::kYoung);
// The function returns a pre-allocated empty byte array for length = 0.
Handle<ByteArray> NewByteArray(
int length, AllocationType allocation = AllocationType::kYoung);

View File

@ -89,6 +89,7 @@ inline void MemCopy(void* dest, const void* src, size_t size) {
case N: \
memcpy(dest, src, N); \
return;
CASE(0)
CASE(1)
CASE(2)
CASE(3)