dart-sdk/runtime/vm/bitmap.cc
Teagan Strickland c9f6e669f9 [vm/compiler] Reland "Further compress the information... in StackMaps."
Fixes an assumption that CompressedStackMapsIterator::Find() is never
passed a PC offset of 0. Adds back an ASSERT that was dropped which checks
for specific cases where a given PC offset does not have a stack map entry.

Original commit message:

Lifting the PC offset in a2bb730 was a small, lightweight change that
gave us big gains, at least on 32-bit architectures. Here, we make
much more invasive changes that will improve the amount of memory used
by the information previously stored in StackMap objects.

Instead of allocating separate objects for StackMaps, we instead compress
all StackMap information for a given Code object into a single object
(CompressedStackMaps, or CSM for short). This replaces the Array used to
store PC offsets (as Smis) and the individual StackMap objects.

While we lose all canonicalization for individual StackMap entries, the
drop in space required to store stack map information more than offsets that.

-----

The impact on AOT snapshot size when compiling the Flutter Gallery
in release mode:

   armv7: Total size -2.58% (Isolate RO: +14.46%, Isolate snapshot: -22.93%)
   armv8: Total size -1.85% (Isolate RO: +15.69%, Isolate snapshot: -22.97%)

The impact on in-memory, not on-disk, size for the Flutter Gallery as seen
in the Observatory while running a profile (not release) build:

   armv7: Drops from 7.1 MB to 6.2MB (-0.9 MB)
   armv8: Drops from 13.5MB to 11.7MB (-1.8 MB)

-----

Bug: https://github.com/dart-lang/sdk/issues/35274, https://github.com/dart-lang/sdk/issues/38873
Cq-Include-Trybots: luci.dart.try:vm-kernel-precomp-linux-debug-x64-try,vm-kernel-precomp-linux-debug-simarm_x64-try,vm-kernel-mac-debug-simdbc64-try
Change-Id: I111b129b0ed64f03184370bceb7cda69d5d4b3c9
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/121700
Commit-Queue: Teagan Strickland <sstrickl@google.com>
Reviewed-by: Martin Kustermann <kustermann@google.com>
2019-10-16 08:25:53 +00:00

144 lines
4.5 KiB
C++

// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
#include "vm/bitmap.h"
#include "platform/assert.h"
#include "vm/object.h"
namespace dart {
void BitmapBuilder::SetLength(intptr_t new_length) {
// When this function is used to shorten the length, affected bits in the
// backing store need to be cleared because the implementation assumes it.
if (new_length < length_) {
// Byte offset containing the first bit to be cleared.
intptr_t byte_offset = new_length >> kBitsPerByteLog2;
if (byte_offset < data_size_in_bytes_) {
// First bit index (in the byte) to be cleared.
intptr_t bit_index = new_length & (kBitsPerByte - 1);
intptr_t mask = (1 << bit_index) - 1;
data_[byte_offset] &= mask;
// Clear the rest.
++byte_offset;
if (byte_offset < data_size_in_bytes_) {
memset(&data_[byte_offset], 0, data_size_in_bytes_ - byte_offset);
}
}
}
length_ = new_length;
}
bool BitmapBuilder::Get(intptr_t bit_offset) const {
if (!InRange(bit_offset)) {
return false;
}
intptr_t byte_offset = bit_offset >> kBitsPerByteLog2;
// Bits not covered by the backing store are implicitly false.
return (byte_offset < data_size_in_bytes_) && GetBit(bit_offset);
}
void BitmapBuilder::Set(intptr_t bit_offset, bool value) {
if (!InRange(bit_offset)) {
length_ = bit_offset + 1;
// Bits not covered by the backing store are implicitly false.
if (!value) return;
// Grow the backing store if necessary.
intptr_t byte_offset = bit_offset >> kBitsPerByteLog2;
if (byte_offset >= data_size_in_bytes_) {
uint8_t* old_data = data_;
intptr_t old_size = data_size_in_bytes_;
data_size_in_bytes_ =
Utils::RoundUp(byte_offset + 1, kIncrementSizeInBytes);
ASSERT(data_size_in_bytes_ > 0);
data_ = Thread::Current()->zone()->Alloc<uint8_t>(data_size_in_bytes_);
ASSERT(data_ != NULL);
memmove(data_, old_data, old_size);
memset(&data_[old_size], 0, (data_size_in_bytes_ - old_size));
}
}
SetBit(bit_offset, value);
}
void BitmapBuilder::SetRange(intptr_t min, intptr_t max, bool value) {
for (intptr_t i = min; i <= max; i++) {
Set(i, value);
}
}
void BitmapBuilder::Print() const {
for (intptr_t i = 0; i < Length(); i++) {
if (Get(i)) {
OS::PrintErr("1");
} else {
OS::PrintErr("0");
}
}
}
void BitmapBuilder::AppendAsBytesTo(GrowableArray<uint8_t>* bytes) const {
// Early return if there are no bits in the payload to copy.
if (Length() == 0) return;
const intptr_t total_size =
Utils::RoundUp(Length(), kBitsPerByte) / kBitsPerByte;
intptr_t payload_size;
intptr_t extra_size;
if (total_size > data_size_in_bytes_) {
// A [BitmapBuilder] does not allocate storage for the trailing 0 bits in
// the backing store, so we need to add additional empty bytes here.
payload_size = data_size_in_bytes_;
extra_size = total_size - data_size_in_bytes_;
} else {
payload_size = total_size;
extra_size = 0;
}
for (intptr_t i = 0; i < payload_size; i++) {
bytes->Add(data_[i]);
}
for (intptr_t i = 0; i < extra_size; i++) {
bytes->Add(0U);
}
// Make sure any bits in the payload beyond the bit length are cleared to
// ensure deterministic snapshots.
#if defined(DEBUG)
if (Length() % kBitsPerByte == 0) return;
const int8_t mask = (1 << (Length() % kBitsPerByte)) - 1;
ASSERT(bytes->Last() == (bytes->Last() & mask));
#endif
}
bool BitmapBuilder::GetBit(intptr_t bit_offset) const {
if (!InRange(bit_offset)) {
return false;
}
intptr_t byte_offset = bit_offset >> kBitsPerByteLog2;
ASSERT(byte_offset < data_size_in_bytes_);
intptr_t bit_remainder = bit_offset & (kBitsPerByte - 1);
uint8_t mask = 1U << bit_remainder;
ASSERT(data_ != NULL);
return ((data_[byte_offset] & mask) != 0);
}
void BitmapBuilder::SetBit(intptr_t bit_offset, bool value) {
if (!InRange(bit_offset)) {
FATAL1(
"Fatal error in BitmapBuilder::SetBit :"
" invalid bit_offset, %" Pd "\n",
bit_offset);
}
intptr_t byte_offset = bit_offset >> kBitsPerByteLog2;
ASSERT(byte_offset < data_size_in_bytes_);
intptr_t bit_remainder = bit_offset & (kBitsPerByte - 1);
uint8_t mask = 1U << bit_remainder;
ASSERT(data_ != NULL);
if (value) {
data_[byte_offset] |= mask;
} else {
data_[byte_offset] &= ~mask;
}
}
} // namespace dart