Skip to content

Commit 69f76f1

Browse files
rmacnak-googleCommit Queue
authored andcommitted
[vm] Only include predefined symbols C strings in gen_snapshot.
Saves about 29k of binary size and kNumPredefinedSymbols map lookups at startup. TEST=ci Change-Id: I186c17a839ebfc6a800a35f08ca2f2c0776984ad Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/433862 Reviewed-by: Alexander Aprelev <[email protected]> Commit-Queue: Ryan Macnak <[email protected]>
1 parent 67c6da2 commit 69f76f1

File tree

5 files changed

+47
-92
lines changed

5 files changed

+47
-92
lines changed

runtime/bin/dartutils.cc

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -218,11 +218,6 @@ bool DartUtils::IsDartSchemeURL(const char* url_name) {
218218
return (strncmp(url_name, kDartScheme, kDartSchemeLen) == 0);
219219
}
220220

221-
bool DartUtils::IsHttpSchemeURL(const char* url_name) {
222-
static const intptr_t kHttpSchemeLen = strlen(kHttpScheme);
223-
return (strncmp(url_name, kHttpScheme, kHttpSchemeLen) == 0);
224-
}
225-
226221
bool DartUtils::IsDartIOLibURL(const char* url_name) {
227222
return (strcmp(url_name, kIOLibURL) == 0);
228223
}
@@ -239,15 +234,6 @@ bool DartUtils::IsDartBuiltinLibURL(const char* url_name) {
239234
return (strcmp(url_name, kBuiltinLibURL) == 0);
240235
}
241236

242-
const char* DartUtils::RemoveScheme(const char* url) {
243-
const char* colon = strchr(url, ':');
244-
if (colon == nullptr) {
245-
return url;
246-
} else {
247-
return colon + 1;
248-
}
249-
}
250-
251237
char* DartUtils::DirName(const char* url) {
252238
const char* slash = strrchr(url, File::PathSeparator()[0]);
253239
if (slash == nullptr) {

runtime/bin/dartutils.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,6 @@ class DartUtils {
161161
static bool IsDartCLILibURL(const char* url_name);
162162
static bool IsDartHttpLibURL(const char* url_name);
163163
static bool IsDartBuiltinLibURL(const char* url_name);
164-
static bool IsHttpSchemeURL(const char* url_name);
165-
static const char* RemoveScheme(const char* url);
166164

167165
// Returns directory name including the last path separator.
168166
//

runtime/vm/app_snapshot.cc

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3718,9 +3718,6 @@ class RODataDeserializationCluster
37183718
VerifyCanonicalSet(d, refs,
37193719
WeakArray::Handle(object_store->symbol_table()));
37203720
object_store->set_symbol_table(table_);
3721-
if (d->isolate_group() == Dart::vm_isolate_group()) {
3722-
Symbols::InitFromSnapshot(d->isolate_group());
3723-
}
37243721
} else if (!is_root_unit_ && is_canonical()) {
37253722
FATAL("Cannot recanonicalize RO objects.");
37263723
}
@@ -6790,9 +6787,6 @@ class StringDeserializationCluster
67906787
VerifyCanonicalSet(d, refs,
67916788
WeakArray::Handle(object_store->symbol_table()));
67926789
object_store->set_symbol_table(table_);
6793-
if (d->isolate_group() == Dart::vm_isolate_group()) {
6794-
Symbols::InitFromSnapshot(d->isolate_group());
6795-
}
67966790
#if defined(DEBUG)
67976791
Symbols::New(Thread::Current(), ":some:new:symbol:");
67986792
ASSERT(object_store->symbol_table() == table_.ptr()); // Did not rehash.
@@ -6824,10 +6818,10 @@ class FakeSerializationCluster : public SerializationCluster {
68246818
#if !defined(DART_PRECOMPILED_RUNTIME)
68256819
class VMSerializationRoots : public SerializationRoots {
68266820
public:
6827-
explicit VMSerializationRoots(const WeakArray& symbols,
6828-
bool should_write_symbols)
6829-
: symbols_(symbols),
6830-
should_write_symbols_(should_write_symbols),
6821+
explicit VMSerializationRoots(const WeakArray& symbol_table,
6822+
bool should_write_symbol_table)
6823+
: symbol_table_(symbol_table),
6824+
should_write_symbol_table_(should_write_symbol_table),
68316825
zone_(Thread::Current()->zone()) {}
68326826

68336827
void AddBaseObjects(Serializer* s) {
@@ -6901,11 +6895,11 @@ class VMSerializationRoots : public SerializationRoots {
69016895
}
69026896

69036897
void PushRoots(Serializer* s) {
6904-
if (should_write_symbols_) {
6905-
s->Push(symbols_.ptr());
6898+
if (should_write_symbol_table_) {
6899+
s->Push(symbol_table_.ptr());
69066900
} else {
6907-
for (intptr_t i = 0; i < symbols_.Length(); i++) {
6908-
s->Push(symbols_.At(i));
6901+
for (intptr_t i = 0; i < symbol_table_.Length(); i++) {
6902+
s->Push(symbol_table_.At(i));
69096903
}
69106904
}
69116905
if (Snapshot::IncludesCode(s->kind())) {
@@ -6916,35 +6910,41 @@ class VMSerializationRoots : public SerializationRoots {
69166910
}
69176911

69186912
void WriteRoots(Serializer* s) {
6919-
s->WriteRootRef(should_write_symbols_ ? symbols_.ptr() : Object::null(),
6920-
"symbol-table");
6913+
for (intptr_t i = 1; i < Symbols::kMaxPredefinedId; i++) {
6914+
s->WriteRootRef(Symbols::Symbol(i).ptr(), "<symbol>");
6915+
}
6916+
s->WriteRootRef(
6917+
should_write_symbol_table_ ? symbol_table_.ptr() : Object::null(),
6918+
"symbol-table");
69216919
if (Snapshot::IncludesCode(s->kind())) {
69226920
for (intptr_t i = 0; i < StubCode::NumEntries(); i++) {
69236921
s->WriteRootRef(StubCode::EntryAt(i).ptr(),
69246922
zone_->PrintToString("Stub:%s", StubCode::NameAt(i)));
69256923
}
69266924
}
69276925

6928-
if (!should_write_symbols_ && s->profile_writer() != nullptr) {
6926+
if (!should_write_symbol_table_ && s->profile_writer() != nullptr) {
69296927
// If writing V8 snapshot profile create an artificial node representing
69306928
// VM isolate symbol table.
6931-
ASSERT(!s->IsReachable(symbols_.ptr()));
6932-
s->AssignArtificialRef(symbols_.ptr());
6933-
const auto& symbols_snapshot_id = s->GetProfileId(symbols_.ptr());
6934-
s->profile_writer()->SetObjectTypeAndName(symbols_snapshot_id, "Symbols",
6935-
"vm_symbols");
6936-
s->profile_writer()->AddRoot(symbols_snapshot_id);
6937-
for (intptr_t i = 0; i < symbols_.Length(); i++) {
6929+
ASSERT(!s->IsReachable(symbol_table_.ptr()));
6930+
s->AssignArtificialRef(symbol_table_.ptr());
6931+
const auto& symbol_table_snapshot_id =
6932+
s->GetProfileId(symbol_table_.ptr());
6933+
s->profile_writer()->SetObjectTypeAndName(symbol_table_snapshot_id,
6934+
"Symbols", "vm_symbols");
6935+
s->profile_writer()->AddRoot(symbol_table_snapshot_id);
6936+
for (intptr_t i = 0; i < symbol_table_.Length(); i++) {
69386937
s->profile_writer()->AttributeReferenceTo(
6939-
symbols_snapshot_id, V8SnapshotProfileWriter::Reference::Element(i),
6940-
s->GetProfileId(symbols_.At(i)));
6938+
symbol_table_snapshot_id,
6939+
V8SnapshotProfileWriter::Reference::Element(i),
6940+
s->GetProfileId(symbol_table_.At(i)));
69416941
}
69426942
}
69436943
}
69446944

69456945
private:
6946-
const WeakArray& symbols_;
6947-
const bool should_write_symbols_;
6946+
const WeakArray& symbol_table_;
6947+
const bool should_write_symbol_table_;
69486948
Zone* zone_;
69496949
};
69506950
#endif // !DART_PRECOMPILED_RUNTIME
@@ -7007,10 +7007,16 @@ class VMDeserializationRoots : public DeserializationRoots {
70077007
}
70087008

70097009
void ReadRoots(Deserializer* d) override {
7010+
for (intptr_t i = 1; i < Symbols::kMaxPredefinedId; i++) {
7011+
String* symbol = String::ReadOnlyHandle();
7012+
*symbol ^= d->ReadRef();
7013+
Symbols::InitSymbol(i, symbol);
7014+
}
70107015
symbol_table_ ^= d->ReadRef();
70117016
if (!symbol_table_.IsNull()) {
70127017
d->isolate_group()->object_store()->set_symbol_table(symbol_table_);
70137018
}
7019+
Symbols::InitFromSnapshot(d->isolate_group());
70147020
if (Snapshot::IncludesCode(d->kind())) {
70157021
for (intptr_t i = 0; i < StubCode::NumEntries(); i++) {
70167022
Code* code = Code::ReadOnlyHandle();
@@ -7026,10 +7032,6 @@ class VMDeserializationRoots : public DeserializationRoots {
70267032
// allocations (e.g., FinalizeVMIsolate) before allocating new pages.
70277033
d->heap()->old_space()->ReleaseBumpAllocation();
70287034

7029-
if (!symbol_table_.IsNull()) {
7030-
Symbols::InitFromSnapshot(d->isolate_group());
7031-
}
7032-
70337035
Object::set_vm_isolate_snapshot_object_table(refs);
70347036
}
70357037

@@ -9701,7 +9703,7 @@ ZoneGrowableArray<Object*>* FullSnapshotWriter::WriteVMSnapshot() {
97019703
VMSerializationRoots roots(
97029704
WeakArray::Handle(
97039705
Dart::vm_isolate_group()->object_store()->symbol_table()),
9704-
/*should_write_symbols=*/!Snapshot::IncludesStringsInROData(kind_));
9706+
/*should_write_symbol_table=*/!Snapshot::IncludesStringsInROData(kind_));
97059707
ZoneGrowableArray<Object*>* objects = serializer.Serialize(&roots);
97069708
serializer.FillHeader(serializer.kind());
97079709
clustered_vm_size_ = serializer.bytes_written();

runtime/vm/symbols.cc

Lines changed: 8 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ namespace dart {
2121
StringPtr Symbols::predefined_[Symbols::kNumberOfOneCharCodeSymbols];
2222
String* Symbols::symbol_handles_[Symbols::kMaxPredefinedId];
2323

24+
#if !defined(DART_PRECOMPILED_RUNTIME)
2425
static const char* const names[] = {
2526
// clang-format off
2627
nullptr,
@@ -34,6 +35,7 @@ static const char* const names[] = {
3435
#undef DEFINE_TOKEN_SYMBOL_INDEX
3536
// clang-format on
3637
};
38+
#endif
3739

3840
StringPtr StringFrom(const uint8_t* data, intptr_t len, Heap::Space space) {
3941
return String::FromLatin1(data, len, space);
@@ -63,11 +65,6 @@ StringPtr ConcatString::ToSymbol() const {
6365
return result.ptr();
6466
}
6567

66-
const char* Symbols::Name(SymbolId symbol) {
67-
ASSERT((symbol > kIllegal) && (symbol < kNullCharId));
68-
return names[symbol];
69-
}
70-
7168
const String& Symbols::Token(Token::Kind token) {
7269
const int tok_index = token;
7370
ASSERT((0 <= tok_index) && (tok_index < Token::kNumTokens));
@@ -78,6 +75,10 @@ const String& Symbols::Token(Token::Kind token) {
7875
}
7976

8077
void Symbols::Init(IsolateGroup* vm_isolate_group) {
78+
// TODO(engine): Require a snapshot when running the JIT runtime too.
79+
#if defined(DART_PRECOMPILED_RUNTIME)
80+
UNREACHABLE();
81+
#else
8182
// Should only be run by the vm isolate.
8283
ASSERT(IsolateGroup::Current() == Dart::vm_isolate_group());
8384
ASSERT(vm_isolate_group == Dart::vm_isolate_group());
@@ -122,47 +123,14 @@ void Symbols::Init(IsolateGroup* vm_isolate_group) {
122123
}
123124

124125
vm_isolate_group->object_store()->set_symbol_table(table.Release());
126+
#endif
125127
}
126128

127129
void Symbols::InitFromSnapshot(IsolateGroup* vm_isolate_group) {
128-
// Should only be run by the vm isolate.
129-
ASSERT(IsolateGroup::Current() == Dart::vm_isolate_group());
130-
ASSERT(vm_isolate_group == Dart::vm_isolate_group());
131-
Zone* zone = Thread::Current()->zone();
132-
133-
CanonicalStringSet table(zone,
134-
vm_isolate_group->object_store()->symbol_table());
135-
136-
// Lookup all the predefined string symbols and language keyword symbols
137-
// and cache them in the read only handles for fast access.
138-
for (intptr_t i = 1; i < Symbols::kNullCharId; i++) {
139-
String* str = String::ReadOnlyHandle();
140-
const unsigned char* name =
141-
reinterpret_cast<const unsigned char*>(names[i]);
142-
*str ^= table.GetOrNull(Latin1Array(name, strlen(names[i])));
143-
ASSERT(!str->IsNull());
144-
ASSERT(str->HasHash());
145-
ASSERT(str->IsCanonical());
146-
symbol_handles_[i] = str;
147-
}
148-
149-
// Lookup Latin1 character Symbols and cache them in read only handles,
150-
// so that Symbols::FromCharCode is fast.
151130
for (intptr_t c = 0; c < kNumberOfOneCharCodeSymbols; c++) {
152131
intptr_t idx = (kNullCharId + c);
153-
ASSERT(idx < kMaxPredefinedId);
154-
ASSERT(Utf::IsLatin1(c));
155-
uint8_t ch = static_cast<uint8_t>(c);
156-
String* str = String::ReadOnlyHandle();
157-
*str ^= table.GetOrNull(Latin1Array(&ch, 1));
158-
ASSERT(!str->IsNull());
159-
ASSERT(str->HasHash());
160-
ASSERT(str->IsCanonical());
161-
predefined_[c] = str->ptr();
162-
symbol_handles_[idx] = str;
132+
predefined_[c] = symbol_handles_[idx]->ptr();
163133
}
164-
165-
vm_isolate_group->object_store()->set_symbol_table(table.Release());
166134
}
167135

168136
void Symbols::SetupSymbolTable(IsolateGroup* isolate_group) {

runtime/vm/symbols.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,10 @@ class Symbols : public AllStatic {
622622
ASSERT((index > kIllegal) && (index < kMaxPredefinedId));
623623
return *(symbol_handles_[index]);
624624
}
625+
static void InitSymbol(intptr_t index, String* symbol) {
626+
ASSERT((index > kIllegal) && (index < kMaxPredefinedId));
627+
symbol_handles_[index] = symbol;
628+
}
625629

626630
// Access methods for one byte character symbols stored in the vm isolate.
627631
static const String& Dot() { return *(symbol_handles_[kNullCharId + '.']); }
@@ -778,9 +782,6 @@ class Symbols : public AllStatic {
778782
static StringPtr FromSet(Thread* thread, const String& str);
779783
static StringPtr FromDot(Thread* thread, const String& str);
780784

781-
// Returns char* of predefined symbol.
782-
static const char* Name(SymbolId symbol);
783-
784785
static StringPtr FromCharCode(Thread* thread, uint16_t char_code);
785786

786787
static StringPtr* PredefinedAddress() {

0 commit comments

Comments
 (0)