Skip to content

Commit 4e8d517

Browse files
alexmarkovcommit-bot@chromium.org
authored andcommitted
[vm,bytecode] Store offsets in bytecode declarations, remove putIfAbsent
Instead of keeping offsets of bytecode declarations in a map, offset is now stored in a field of a base class for all declarations. This saves certain number of map lookups while serializing bytecode. Also, calls to a rather expensive Map.putIfAbsent are removed. Change-Id: I036fbb0e1d8afc6e402709a0a1eafb0e50a2c25a Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/122170 Reviewed-by: Ryan Macnak <rmacnak@google.com> Commit-Queue: Alexander Markov <alexmarkov@google.com>
1 parent a170fc5 commit 4e8d517

File tree

6 files changed

+43
-30
lines changed

6 files changed

+43
-30
lines changed

pkg/vm/lib/bytecode/bytecode_serialization.dart

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -229,12 +229,13 @@ class BufferedReader {
229229
return result;
230230
}
231231

232-
T readLinkOffset<T>() {
232+
T readLinkOffset<T extends BytecodeDeclaration>() {
233233
final offset = readPackedUInt30();
234234
return linkReader.get<T>(offset);
235235
}
236236

237-
ForwardReference<T> readLinkOffsetAsForwardReference<T>() {
237+
ForwardReference<T>
238+
readLinkOffsetAsForwardReference<T extends BytecodeDeclaration>() {
238239
final offset = readPackedUInt30();
239240
return new ForwardReference<T>(offset, linkReader);
240241
}
@@ -429,24 +430,26 @@ class SLEB128DeltaDecoder {
429430
}
430431
}
431432

432-
class LinkWriter {
433-
final _map = <Object, int>{};
433+
class BytecodeDeclaration {
434+
int _offset;
435+
}
434436

435-
void put(Object target, int offset) {
436-
_map[target] = offset;
437+
class LinkWriter {
438+
void put(BytecodeDeclaration target, int offset) {
439+
target._offset = offset;
437440
}
438441

439-
int getOffset(Object target) {
440-
return _map[target] ??
442+
int getOffset(BytecodeDeclaration target) {
443+
return target._offset ??
441444
(throw 'Offset of ${target.runtimeType} $target is not set');
442445
}
443446
}
444447

445448
class LinkReader {
446-
final _map = <Type, Map<int, Object>>{};
449+
final _map = <Type, Map<int, BytecodeDeclaration>>{};
447450

448-
void setOffset<T>(T target, int offset) {
449-
final offsetToObject = (_map[T] ??= <int, Object>{});
451+
void setOffset<T>(BytecodeDeclaration target, int offset) {
452+
final offsetToObject = (_map[T] ??= <int, BytecodeDeclaration>{});
450453
final previous = offsetToObject[offset];
451454
if (previous != null) {
452455
throw 'Unable to associate offset $T/$offset with ${target.runtimeType} $target.'
@@ -455,13 +458,13 @@ class LinkReader {
455458
offsetToObject[offset] = target;
456459
}
457460

458-
T get<T>(int offset) {
461+
T get<T extends BytecodeDeclaration>(int offset) {
459462
return _map[T][offset] ?? (throw 'No object at offset $T/$offset');
460463
}
461464
}
462465

463466
// Placeholder for an object which will be read in future.
464-
class ForwardReference<T> {
467+
class ForwardReference<T extends BytecodeDeclaration> {
465468
final int offset;
466469
final LinkReader linkReader;
467470

pkg/vm/lib/bytecode/constant_pool.dart

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -829,14 +829,16 @@ class ConstantPool {
829829
isSetter: invocationKind == InvocationKind.setter)));
830830

831831
int _add(ConstantPoolEntry entry) {
832-
return _canonicalizationCache.putIfAbsent(entry, () {
833-
int index = entries.length;
832+
int index = _canonicalizationCache[entry];
833+
if (index == null) {
834+
index = entries.length;
834835
if (index >= constantPoolIndexLimit) {
835836
throw new ConstantPoolIndexOverflowException();
836837
}
837838
_addEntry(entry);
838-
return index;
839-
});
839+
_canonicalizationCache[entry] = index;
840+
}
841+
return index;
840842
}
841843

842844
void _addEntry(ConstantPoolEntry entry) {

pkg/vm/lib/bytecode/declarations.dart

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@ library vm.bytecode.declarations;
66

77
import 'package:kernel/ast.dart';
88
import 'bytecode_serialization.dart'
9-
show BufferedWriter, BufferedReader, BytecodeSizeStatistics, StringTable;
9+
show
10+
BufferedWriter,
11+
BufferedReader,
12+
BytecodeDeclaration,
13+
BytecodeSizeStatistics,
14+
StringTable;
1015
import 'constant_pool.dart' show ConstantPool;
1116
import 'dbc.dart' show currentBytecodeFormatVersion;
1217
import 'disassembler.dart' show BytecodeDisassembler;
@@ -15,7 +20,7 @@ import 'local_variable_table.dart' show LocalVariableTable;
1520
import 'object_table.dart' show ObjectTable, ObjectHandle, NameAndType;
1621
import 'source_positions.dart' show LineStarts, SourcePositions;
1722

18-
class LibraryDeclaration {
23+
class LibraryDeclaration extends BytecodeDeclaration {
1924
static const usesDartMirrorsFlag = 1 << 0;
2025
static const usesDartFfiFlag = 1 << 1;
2126
static const hasExtensionsFlag = 1 << 2;
@@ -89,7 +94,7 @@ class LibraryDeclaration {
8994
}
9095
}
9196

92-
class ClassDeclaration {
97+
class ClassDeclaration extends BytecodeDeclaration {
9398
static const isAbstractFlag = 1 << 0;
9499
static const isEnumFlag = 1 << 1;
95100
static const hasTypeParamsFlag = 1 << 2;
@@ -220,7 +225,7 @@ class ClassDeclaration {
220225
}
221226
}
222227

223-
class SourceFile {
228+
class SourceFile extends BytecodeDeclaration {
224229
static const hasLineStartsFlag = 1 << 0;
225230
static const hasSourceFlag = 1 << 1;
226231

@@ -274,7 +279,7 @@ class SourceFile {
274279
}
275280
}
276281

277-
class Members {
282+
class Members extends BytecodeDeclaration {
278283
final List<FieldDeclaration> fields;
279284
final List<FunctionDeclaration> functions;
280285

@@ -789,7 +794,7 @@ class ParameterDeclaration {
789794
String toString() => '$type $name';
790795
}
791796

792-
class Code {
797+
class Code extends BytecodeDeclaration {
793798
static const hasExceptionsTableFlag = 1 << 0;
794799
static const hasSourcePositionsFlag = 1 << 1;
795800
static const hasNullableFieldsFlag = 1 << 2;
@@ -1190,7 +1195,7 @@ class ClosureCode {
11901195
}
11911196
}
11921197

1193-
class AnnotationsDeclaration {
1198+
class AnnotationsDeclaration extends BytecodeDeclaration {
11941199
final ObjectHandle object;
11951200

11961201
AnnotationsDeclaration(this.object);

pkg/vm/lib/bytecode/local_variable_table.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ class ContextVariable extends LocalVariableEntry {
128128
}
129129

130130
/// Keeps information about declared local variables.
131-
class LocalVariableTable {
131+
class LocalVariableTable extends BytecodeDeclaration {
132132
final scopes = <Scope>[];
133133
final activeScopes = <Scope>[];
134134
ContextVariable contextVariable;

pkg/vm/lib/bytecode/object_table.dart

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1877,11 +1877,13 @@ class ObjectTable implements ObjectWriter, ObjectReader {
18771877

18781878
ObjectHandle getOrAddObject(ObjectHandle obj) {
18791879
assert(obj._useCount == 0);
1880-
ObjectHandle canonical = _canonicalizationCache.putIfAbsent(obj, () {
1880+
ObjectHandle canonical = _canonicalizationCache[obj];
1881+
if (canonical == null) {
18811882
assert(_indexTable == null);
18821883
_objects.add(obj);
1883-
return obj;
1884-
});
1884+
_canonicalizationCache[obj] = obj;
1885+
canonical = obj;
1886+
}
18851887
++canonical._useCount;
18861888
return canonical;
18871889
}

pkg/vm/lib/bytecode/source_positions.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@ import 'bytecode_serialization.dart'
88
show
99
BufferedWriter,
1010
BufferedReader,
11+
BytecodeDeclaration,
1112
PackedUInt30DeltaEncoder,
1213
PackedUInt30DeltaDecoder,
1314
SLEB128DeltaEncoder,
1415
SLEB128DeltaDecoder;
1516

1617
/// Maintains mapping between bytecode instructions and source positions.
17-
class SourcePositions {
18+
class SourcePositions extends BytecodeDeclaration {
1819
// Special value of fileOffset which marks synthetic code without source
1920
// position.
2021
static const syntheticCodeMarker = -1;
@@ -99,7 +100,7 @@ class SourcePositions {
99100

100101
/// Keeps file offsets of line starts. This information is used to
101102
/// decode source positions to line/column.
102-
class LineStarts {
103+
class LineStarts extends BytecodeDeclaration {
103104
final List<int> lineStarts;
104105

105106
LineStarts(this.lineStarts);

0 commit comments

Comments
 (0)