Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(48)

Side by Side Diff: pkg/front_end/lib/src/fasta/scanner/string_canonicalizer.dart

Issue 2702753003: Relanding: avoiding creating strings for canonicalization (Closed)
Patch Set: s/canonicalizer/string_canonicalizer/, style Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « pkg/compiler/lib/src/compiler.dart ('k') | pkg/front_end/lib/src/fasta/scanner/token.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file.
4
5 library fasta.scanner.string_canonicalizer;
6
7 import 'dart:convert';
8
9 class Node {
10 var /* String | List<int> */ data;
11 int start;
12 int end;
13 String payload;
14 Node next;
15 Node(this.data, this.start, this.end, this.payload, this.next);
16 }
17
18 /// A hash table for triples:
19 /// (list of bytes, start, end) --> canonicalized string
20 /// Using triples avoids allocating string slices before checking if they
21 /// are canonical.
22 ///
23 /// Gives about 3% speedup on dart2js.
24 class StringCanonicalizer {
25 /// Mask away top bits to keep hash calculation within 32-bit SMI range.
26 static const int MASK = 16 * 1024 * 1024 - 1;
27
28 static const int INITIAL_SIZE = 8 * 1024;
29
30 /// Linear size of a hash table.
31 int _size = INITIAL_SIZE;
32 /// Items in a hash table.
33 int _count = 0;
34 /// The table itself.
35 List<Node> _nodes = new List<Node>(INITIAL_SIZE);
36
37 static String decode(List<int> data, int start, int end, bool asciiOnly) {
38 var s;
39 if (asciiOnly) {
40 s = new String.fromCharCodes(data, start, end);
41 } else {
42 s = new Utf8Decoder(allowMalformed: true).convert(data, start, end);
43 }
44 return s;
45 }
46
47 static int hashBytes(List<int> data, int start, int end) {
48 int h = 5381;
49 for (int i = start; i < end; i++) {
50 h = ((h << 5) + h + data[i]) & MASK;
51 }
52 return h;
53 }
54
55 static int hashString(String data, int start, int end) {
56 int h = 5381;
57 for (int i = start; i < end; i++) {
58 h = ((h << 5) + h + data.codeUnitAt(i)) & MASK;
59 }
60 return h;
61 }
62
63 rehash() {
64 var newSize = _size * 2;
65 var newNodes = new List<Node>(newSize);
66 for (int i = 0; i < _size; i++) {
67 Node t = _nodes[i];
68 while (t != null) {
69 Node n = t.next;
70 int newIndex = t.data is String ?
71 hashString(t.data, t.start, t.end) & (newSize - 1)
72 : hashBytes(t.data, t.start, t.end) & (newSize - 1);
73 Node s = newNodes[newIndex];
74 t.next = s;
75 newNodes[newIndex] = t;
76 t = n;
77 }
78 }
79 _size = newSize;
80 _nodes = newNodes;
81 }
82
83 String canonicalize(data, int start, int end, bool asciiOnly) {
84 if (_count > _size) rehash();
85 int index = data is String
86 ? hashString(data, start, end)
87 : hashBytes(data, start, end);
88 index = index & (_size - 1);
89 Node s = _nodes[index];
90 Node t = s;
91 int len = end - start;
92 while (t != null) {
93 if (t.end - t.start == len) {
94 int i = start, j = t.start;
95 while (i < end && data[i] == t.data[j]) {
96 i++;
97 j++;
98 }
99 if (i == end) {
100 return t.payload;
101 }
102 }
103 t = t.next;
104 }
105 String payload;
106 if (data is String)
107 payload = data.substring(start, end);
108 else
109 payload = decode(data, start, end, asciiOnly);
110 _nodes[index] = new Node(data, start, end, payload, s);
111 _count++;
112 return payload;
113 }
114
115 clear() {
116 _nodes = new List<Node>(_size);
117 }
118 }
OLDNEW
« no previous file with comments | « pkg/compiler/lib/src/compiler.dart ('k') | pkg/front_end/lib/src/fasta/scanner/token.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698