Skip to content

Commit b316210

Browse files
mkustermanncommit-bot@chromium.org
authored andcommitted
[vm/compiler] Speed up the compiler part which deals with kernel reading up in DEBUG mode
This reduces our AOT compiler's time - gen_snapshot - by half in DEBUG mode. Issue #32603 Change-Id: I235afc40acba32036d4127c31c93f3b22f522314 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/103441 Commit-Queue: Martin Kustermann <[email protected]> Reviewed-by: Alexander Aprelev <[email protected]>
1 parent 0c89145 commit b316210

File tree

3 files changed

+41
-16
lines changed

3 files changed

+41
-16
lines changed

runtime/vm/kernel_binary.h

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -177,15 +177,7 @@ class Reader : public ValueObject {
177177
offset_(0) {
178178
// The reader expects that the actual data is external, though allows
179179
// having a view into this external typed data.
180-
#if defined(DEBUG)
181-
if (typed_data_->IsTypedDataView()) {
182-
const auto& backing_store =
183-
TypedDataBase::Handle(TypedDataView::Cast(*typed_data_).typed_data());
184-
ASSERT(backing_store.IsExternalTypedData());
185-
} else {
186-
ASSERT(typed_data_->IsExternalTypedData());
187-
}
188-
#endif
180+
DEBUG_ASSERT(typed_data_->IsBackedByExternalTypedData());
189181
}
190182

191183
uint32_t ReadFromIndex(intptr_t end_offset,
@@ -199,10 +191,15 @@ class Reader : public ValueObject {
199191
return result;
200192
}
201193

202-
uint32_t ReadUInt32At(intptr_t offset) const {
203-
ASSERT((size_ >= 4) && (offset >= 0) && (offset <= size_ - 4));
204-
uint32_t value;
205-
value = typed_data_->GetUint32(offset);
194+
DART_FORCE_INLINE uint32_t ReadUInt32At(intptr_t offset) const {
195+
ASSERT(size_ >= 4 && offset >= 0 && (offset <= size_ - 4));
196+
// We validated in the [Reader] constructor that we have either an
197+
// ExternalTypedData or a view on top of an ExternalTypedData.
198+
//
199+
// This means the data pointer will not change and we can use
200+
// [DataAddrUnsafe].
201+
const uint32_t value =
202+
*static_cast<uint32_t*>(typed_data_->DataAddrUnsafe(offset));
206203
return Utils::BigEndianToHost32(value);
207204
}
208205

@@ -358,9 +355,13 @@ class Reader : public ValueObject {
358355
}
359356

360357
private:
361-
const uint8_t* buffer() const {
362-
NoSafepointScope no_safepoint(thread_);
363-
return reinterpret_cast<uint8_t*>(typed_data_->DataAddr(0));
358+
DART_FORCE_INLINE const uint8_t* buffer() const {
359+
// We validated in the [Reader] constructor that we have either an
360+
// ExternalTypedData or a view on top of an ExternalTypedData.
361+
//
362+
// This means the data pointer will not change and we can use
363+
// [DataAddrUnsafe].
364+
return reinterpret_cast<uint8_t*>(typed_data_->DataAddrUnsafe(0));
364365
}
365366

366367
Thread* thread_;

runtime/vm/object.cc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21169,6 +21169,19 @@ RawTypedDataView* TypedDataBase::CreateUint8View(intptr_t offset_in_bytes,
2116921169
offset_in_bytes, length_in_bytes, space);
2117021170
}
2117121171

21172+
#if defined(DEBUG)
21173+
bool TypedDataBase::IsBackedByExternalTypedData() const {
21174+
if (IsExternalTypedData()) {
21175+
return true;
21176+
}
21177+
if (IsTypedDataView()) {
21178+
const auto& view = TypedDataView::Cast(*this);
21179+
return TypedDataBase::Handle(view.typed_data()).IsExternalTypedData();
21180+
}
21181+
return false;
21182+
}
21183+
#endif
21184+
2117221185
const char* TypedDataView::ToCString() const {
2117321186
auto zone = Thread::Current()->zone();
2117421187
return OS::SCreate(zone, "TypedDataView(cid: %" Pd ")", GetClassId());

runtime/vm/object.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8410,6 +8410,13 @@ class TypedDataBase : public Instance {
84108410
#if defined(DEBUG)
84118411
ValidateInvariant();
84128412
#endif
8413+
return DataAddrUnsafe(byte_offset);
8414+
}
8415+
8416+
// To speed up debug mode (e.g. for kernel reading - which uses typed data
8417+
// for kernel buffers) we expose the data pointer without asserts for bounds
8418+
// checks and invariants.
8419+
DART_FORCE_INLINE void* DataAddrUnsafe(intptr_t byte_offset) const {
84138420
return reinterpret_cast<void*>(raw_ptr()->data_ + byte_offset);
84148421
}
84158422

@@ -8471,6 +8478,10 @@ class TypedDataBase : public Instance {
84718478

84728479
#undef TYPED_GETTER_SETTER
84738480

8481+
#if defined(DEBUG)
8482+
bool IsBackedByExternalTypedData() const;
8483+
#endif
8484+
84748485
protected:
84758486
void SetLength(intptr_t value) const {
84768487
ASSERT(value <= Smi::kMaxValue);

0 commit comments

Comments
 (0)