Skip to content

Commit b70544e

Browse files
authored
perf: smaller metadata, mapped instead of copied (#2112)
* refactor: remove SetMissingBaseMethods It was meant to copy the instance methods of a base class that the device's class hierarchy lacks onto the class being built. Since extension functions were added to the metadata, the count it reads right after a class value's head is the extension function count, which is zero for every type that has none, so it has added nothing for years. For a type that does have extension functions it would read their entries, which are two bytes longer, as instance methods. The list of instance method data that SetInstanceMembers returned only fed this function, so it goes as well. * perf: pack the metadata value stream The value stream, half of an app's metadata, drops by about a quarter (1,635 KB to 1,209 KB for the test app; the three streams together go from 3.26 MB to 2.84 MB). Node ids keep their width. - Signatures are stored once. 84,677 method records shared 18,833 distinct signatures, each written out in full every time. They now live in a region at the head of the stream and a method refers to its signature by offset. - A method record is a fixed 7 bytes, the name offset and a 3-byte signature reference whose low bit is the resolved flag, down from 7 bytes plus 2 per type. The runtime's MethodInfo is a pointer to the record: it no longer copies each signature into a vector when a class is materialized, and reads it only when the method is first resolved. - A class value says which of its six member sections it has in one byte, instead of a 2-byte count for each. 79% of those counts were 0. - A static field names its declaring type only when that is not the class listing it, which was the case for 15% of them. - An interface no longer carries the offset of the "com/tns/gen/" prefix, a constant the runtime already has. - An array's element node no longer gets a class value that nothing could reach. The stream now opens with a format word, and the runtime refuses to start on any other, naming the mismatch. The generator and the runtime ship together, so this only fires on stale or damaged metadata, which used to be misread. A signature reference has 23 bits, so the signature region can be at most 8 MB (it is 127 KB here); the generator fails the build beyond it. * perf: map the metadata instead of reading it into the heap The names and values streams were read into heap buffers kept for the life of the process, each with 512 KB of slack for records appended at run time. They are now mapped read-only, so only the pages an app touches are loaded, and those are clean, file-backed pages the system can drop under memory pressure instead of private heap. The node stream is mapped only while the tree is built from it. Nothing appends to those buffers any more. The names buffer was never written, and a type resolved at run time put a three-byte record in the value buffer only so its node had an offset; it now uses a fixed offset no value can start at, and the one read that followed such an offset (the element type of an array) goes through the node's memoized type. The asset extractor replaces these files by renaming a new one over them, so a live mapping keeps the contents it was made from. * perf: read the metadata ahead and map it up front Mapping the metadata lazily moved the cost of loading it onto the first access of each class, as page faults, and made those accesses noisier. Populating the mapping removes that, and asking for the whole file to be read ahead first keeps a cold start from paying for it one read-ahead window at a time: on the API 36 emulator with the page cache dropped, loading the metadata takes 3.1 ms read into the heap, 3.8 ms populated without the read-ahead and 2.2 ms with it. * fix: log a metadata format mismatch The exception thrown for metadata in another format is raised before anything is set up to report it, so on its own it never reached logcat.
1 parent 3c798e2 commit b70544e

9 files changed

Lines changed: 438 additions & 410 deletions

File tree

‎test-app/build-tools/android-metadata-generator/src/src/com/telerik/metadata/Writer.java‎

Lines changed: 190 additions & 91 deletions
Large diffs are not rendered by default.

‎test-app/runtime/src/main/cpp/MetadataEntry.cpp‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ std::string &MetadataEntry::getSig() {
3838
} else if (type == NodeType::StaticField) {
3939
sig = reader->ReadTypeName(sfi->nodeId);
4040
} else if (type == NodeType::Method) {
41-
uint8_t sigLength = mi.GetSignatureLength();
41+
auto sigLength = mi.GetSignatureLength();
4242
if (sigLength > 0)
4343
sig = mi.GetSignature();
4444

@@ -82,7 +82,9 @@ std::string &MetadataEntry::getDeclaringType() {
8282
auto reader = MetadataNode::getMetadataReader();
8383

8484
if (type == NodeType::StaticField) {
85-
declaringType = reader->ReadTypeName(sfi->declaringType);
85+
declaringType = (sfi->flags & FieldInfo::HAS_DECLARING_TYPE)
86+
? reader->ReadTypeName(sfi->declaringType)
87+
: reader->ReadTypeName(treeNode);
8688
} else if (type == NodeType::Method && isStatic) {
8789
declaringType = mi.GetDeclaringType();
8890
}
@@ -111,9 +113,9 @@ bool MetadataEntry::getIsFinal() {
111113
if (isFinalSet) return isFinal;
112114

113115
if (type == NodeType::Field) {
114-
isFinal = fi->finalModifier == MetadataTreeNode::FINAL;
116+
isFinal = (fi->flags & FieldInfo::FINAL) != 0;
115117
} else if (type == NodeType::StaticField) {
116-
isFinal = sfi->finalModifier == MetadataTreeNode::FINAL;
118+
isFinal = (sfi->flags & FieldInfo::FINAL) != 0;
117119
}
118120

119121
isFinalSet = true;

‎test-app/runtime/src/main/cpp/MetadataFieldInfo.h‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
11
#ifndef METADATAFIELDINFO_H_
22
#define METADATAFIELDINFO_H_
33

4+
#include <cstdint>
5+
46
namespace tns {
57
struct __attribute__ ((__packed__)) FieldInfo {
8+
static const uint8_t FINAL = 1;
9+
// On a static field: the id of its declaring type follows. A static field
10+
// without it is declared by the class whose value lists it.
11+
static const uint8_t HAS_DECLARING_TYPE = 1 << 1;
12+
613
FieldInfo()
714
:
8-
nameOffset(0), nodeId(0), finalModifier(0) {
15+
nameOffset(0), nodeId(0), flags(0) {
916
}
1017

1118
uint32_t nameOffset;
1219
uint16_t nodeId;
13-
uint8_t finalModifier;
20+
uint8_t flags;
1421
};
1522

1623
struct __attribute__ ((__packed__)) StaticFieldInfo: FieldInfo {
@@ -19,6 +26,7 @@ struct __attribute__ ((__packed__)) StaticFieldInfo: FieldInfo {
1926
FieldInfo(), declaringType(0) {
2027
}
2128

29+
// Part of the record only when HAS_DECLARING_TYPE is set.
2230
uint16_t declaringType;
2331
};
2432
}
Lines changed: 37 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,57 @@
11
#include "MetadataMethodInfo.h"
22
#include "MetadataNode.h"
3+
#include <cstring>
34

45

56
using namespace tns;
67

8+
namespace {
9+
// The value stream is packed, so nothing in it is aligned.
10+
inline uint16_t ReadUInt16(const uint8_t *data) {
11+
uint16_t value;
12+
memcpy(&value, data, sizeof(value));
13+
return value;
14+
}
15+
16+
inline uint32_t ReadUInt32(const uint8_t *data) {
17+
uint32_t value;
18+
memcpy(&value, data, sizeof(value));
19+
return value;
20+
}
21+
}
22+
723
std::string MethodInfo::GetName() {
8-
string methodName = MetadataNode::getMetadataReader()->ReadName(nameOffset);
24+
string methodName = MetadataNode::getMetadataReader()->ReadName(ReadUInt32(m_record));
925
return methodName;
1026
}
1127

1228
uint8_t MethodInfo::CheckIsResolved() {
13-
return resolvedData;
29+
return m_record[sizeof(uint32_t)] & 1;
30+
}
31+
32+
const uint8_t *MethodInfo::Signature() {
33+
// Read a byte at a time: the record can be the last thing in the stream,
34+
// and a four-byte load would run past it.
35+
const uint8_t *reference = m_record + sizeof(uint32_t);
36+
uint32_t offset = (reference[0] | (reference[1] << 8) | (reference[2] << 16)) >> 1;
37+
38+
return MetadataNode::getMetadataReader()->GetSignatureData() + offset;
1439
}
1540

1641
uint16_t MethodInfo::GetSignatureLength() {
17-
return m_signatureLength;
42+
return Signature()[0] + 1;
1843
}
1944

2045
std::string MethodInfo::GetSignature() { //use nodeId's to read the whole signature
2146
auto m_reader = MetadataNode::getMetadataReader();
47+
const uint8_t *signatureRecord = Signature();
48+
int signatureLength = signatureRecord[0] + 1;
49+
const uint8_t *nodeIds = signatureRecord + 1;
50+
2251
string signature = "(";
2352
string ret;
24-
for (int i = 0; i < m_signatureLength; i++) {
25-
uint16_t nodeId = nodeIds[i];
53+
for (int i = 0; i < signatureLength; i++) {
54+
uint16_t nodeId = ReadUInt16(nodeIds + i * sizeof(uint16_t));
2655
string curArgTypeName = m_reader->ReadTypeName(nodeId);
2756
MetadataTreeNode* node = m_reader->GetNodeById(nodeId);
2857

@@ -57,43 +86,9 @@ std::string MethodInfo::GetSignature() { //use nodeId's to read the whole signat
5786
std::string MethodInfo::GetDeclaringType() {
5887
auto m_reader = MetadataNode::getMetadataReader();
5988

60-
return m_reader->ReadTypeName(declaringNodeId);
89+
return m_reader->ReadTypeName(ReadUInt16(m_record + RECORD_SIZE));
6190
}
6291

6392
int MethodInfo::GetSizeOfReadMethodInfo() {
64-
65-
if (!sizeMeasured) {
66-
sizeMeasured = true;
67-
// name
68-
nameOffset = *reinterpret_cast<uint32_t*>(m_pData);
69-
m_pData += sizeof(uint32_t);
70-
// resolved data
71-
resolvedData = *reinterpret_cast<uint8_t*>(m_pData);
72-
m_pData += sizeof(uint8_t);
73-
// sig length
74-
m_signatureLength = *reinterpret_cast<uint16_t*>(m_pData);
75-
m_pData += sizeof(uint16_t);
76-
77-
// signature
78-
if (m_signatureLength > 0) {
79-
uint16_t* nodeIdPtr = reinterpret_cast<uint16_t*>(m_pData);
80-
nodeIds.resize(m_signatureLength);
81-
for (int i = 0; i < m_signatureLength; i++) {
82-
nodeIds[i] = *nodeIdPtr++;
83-
}
84-
m_pData += m_signatureLength * sizeof(uint16_t);
85-
}
86-
87-
// declaring type
88-
if (isStatic) {
89-
auto declaringTypePtr = reinterpret_cast<uint16_t*>(m_pData);
90-
declaringNodeId = *declaringTypePtr;
91-
m_pData += sizeof(uint16_t);
92-
}
93-
94-
95-
96-
}
97-
98-
return m_pData - m_pStartData;
99-
}
93+
return RECORD_SIZE + (isStatic ? sizeof(uint16_t) : 0);
94+
}

‎test-app/runtime/src/main/cpp/MetadataMethodInfo.h‎

Lines changed: 16 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,34 @@
11
#ifndef METHODINFOSMARTPOINTER_H_
22
#define METHODINFOSMARTPOINTER_H_
33

4+
#include <cstdint>
45
#include <iostream>
56
#include <string>
67
#include <vector>
78

89
using namespace std;
910

1011
namespace tns {
12+
/*
13+
* A view of one method record in the metadata's value stream. The record is
14+
* the offset of the method's name (4 bytes) and a reference to its signature
15+
* (3 bytes); a static method or an extension function is followed by the id
16+
* of its declaring type. Nothing is copied out of the record, and the
17+
* signature itself is only read when the method is first resolved.
18+
*/
1119
class MethodInfo {
1220
public:
21+
static const int RECORD_SIZE = sizeof(uint32_t) + 3;
1322

14-
MethodInfo(uint8_t *pValue)
15-
: isStatic(false), m_pData(pValue), m_pStartData(pValue), m_signatureLength(0),
16-
sizeMeasured(false), nameOffset(0), resolvedData(0),
17-
declaringNodeId(0){
18-
}
19-
20-
MethodInfo(const MethodInfo& other) = default;
21-
22-
MethodInfo& operator=(const MethodInfo& other) {
23-
if (this != &other) {
24-
isStatic = other.isStatic;
25-
m_pData = other.m_pData;
26-
m_pStartData = other.m_pStartData;
27-
m_signatureLength = other.m_signatureLength;
28-
sizeMeasured = other.sizeMeasured;
29-
nameOffset = other.nameOffset;
30-
resolvedData = other.resolvedData;
31-
declaringNodeId = other.declaringNodeId;
32-
nodeIds = other.nodeIds;
33-
}
34-
return *this;
23+
MethodInfo(uint8_t *record)
24+
: isStatic(false), m_record(record) {
3525
}
3626

3727
std::string GetName();
3828

3929
uint8_t CheckIsResolved();
4030

31+
// The return type and the parameter types.
4132
uint16_t GetSignatureLength();
4233

4334
std::string GetSignature();
@@ -49,17 +40,11 @@ namespace tns {
4940
bool isStatic;
5041

5142
private:
52-
uint8_t *m_pData; //where we currently read
53-
uint8_t *m_pStartData; // pointer to the beginning
54-
uint16_t m_signatureLength;
55-
bool sizeMeasured;
56-
57-
uint32_t nameOffset;
58-
uint8_t resolvedData;
59-
uint16_t declaringNodeId;
60-
std::vector<uint16_t> nodeIds;
61-
43+
// The signature's record in the signature region: a byte holding the
44+
// number of type ids less one, then the ids.
45+
const uint8_t *Signature();
6246

47+
uint8_t *m_record;
6348
};
6449
}
6550

0 commit comments

Comments
 (0)