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

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

Issue 2702753003: Relanding: avoiding creating strings for canonicalization (Closed)
Patch Set: 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
ahe 2017/02/20 09:18:30 Consider renaming this file to string_canonicalize
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.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 /**
ahe 2017/02/20 09:18:30 FWIW, I think we're shifting towards preferring th
19 * A hash table for triples:
20 * (list of bytes, start, end) --> canonicalized string
21 * Using triples avoids allocating string slices before checking if they
22 * are canonical.
23 *
24 * Gives about 3% speedup on dart2js.
25 */
26 class StringCanonicalizer {
27 /// Mask away top bits to keep hash calculation within 32-bit SMI range.
28 static const int MASK = 16 * 1024 * 1024 - 1;
29
30 static const int INITIAL_SIZE = 8 * 1024;
31
32 /// Linear size of a hash table.
33 int _size = INITIAL_SIZE;
34 /// Items in a hash table.
35 int _count = 0;
36 /// The table itself.
37 List<Node> _nodes = new List<Node>(INITIAL_SIZE);
38
39 static String decode(List<int> data, int start, int end, bool asciiOnly) {
40 var s;
41 if (asciiOnly) {
42 s = new String.fromCharCodes(data, start, end);
43 } else {
44 s = new Utf8Decoder(allowMalformed: true).convert(data, start, end);
45 }
46 return s;
47 }
48
49 static int hashBytes(List<int> data, int start, int end) {
50 int h = 5381;
51 for (int i = start; i < end; i++) {
52 h = ((h << 5) + h + data[i]) & MASK;
53 }
54 return h;
55 }
56
57 static int hashString(String data, int start, int end) {
58 int h = 5381;
59 for (int i = start; i < end; i++) {
60 h = ((h << 5) + h + data.codeUnitAt(i)) & MASK;
61 }
62 return h;
63 }
64
65 rehash() {
66 var newSize = _size * 2;
67 var newNodes = new List<Node>(newSize);
68 for (int i = 0; i < _size; i++) {
69 Node t = _nodes[i];
70 while (t != null) {
71 Node n = t.next;
72 int newIndex = t.data is String ?
73 hashString(t.data, t.start, t.end) & (newSize - 1)
74 : hashBytes(t.data, t.start, t.end) & (newSize - 1);
75 Node s = newNodes[newIndex];
76 t.next = s;
77 newNodes[newIndex] = t;
78 t = n;
79 }
80 }
81 _size = newSize;
82 _nodes = newNodes;
83 }
84
85 String canonicalize(data, int start, int end, bool asciiOnly) {
86 if (_count > _size) rehash();
87 int index = data is String
88 ? hashString(data, start, end)
89 : hashBytes(data, start, end);
90 index = index & (_size - 1);
91 Node s = _nodes[index];
92 Node t = s;
93 int len = end - start;
94 while (t != null) {
95 if (t.end - t.start == len) {
96 int i = start, j = t.start;
97 while (i < end && data[i] == t.data[j]) {
98 i++;
99 j++;
100 }
101 if (i == end) {
102 return t.payload;
103 }
104 }
105 t = t.next;
106 }
107 String payload;
108 if (data is String)
109 payload = data.substring(start, end);
110 else
111 payload = decode(data, start, end, asciiOnly);
112 _nodes[index] = new Node(data, start, end, payload, s);
113 _count++;
114 return payload;
115 }
116
117 clear() {
118 _nodes = new List<Node>(_size);
119 }
120 }
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