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

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

Issue 2696803003: Avoiding creating strings for canonicalization (Closed)
Patch Set: Fasta: avoiding allocating strings for canonicalization 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
OLDNEW
(Empty)
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
ahe 2017/02/14 13:52:46 That comment is so last last last last last year ;
Dmitry Olshansky 2017/02/14 14:31:09 Done.
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.abstract_scanner;
6
7 import 'dart:convert';
8
9 class _Node {
ahe 2017/02/14 13:52:46 No need for privacy on classes.
Dmitry Olshansky 2017/02/14 14:31:09 Done.
10 var data; // String or List<int>
ahe 2017/02/14 13:52:46 When I can't give a precise type, I usually do thi
Dmitry Olshansky 2017/02/14 14:31:09 Done.
11 int start, end;
ahe 2017/02/14 13:52:45 Could you split this declaration in two?
Dmitry Olshansky 2017/02/14 14:31:09 Done.
12 String payload;
13 _Node next;
14 _Node(this.data, this.start, this.end, this.payload, this.next);
15 }
16
17 class HashCanonicalizer {
ahe 2017/02/14 13:52:45 I suggest you rename this to StringCanonicalizer.
Dmitry Olshansky 2017/02/14 14:31:09 Done.
18 // Mask away top bits to keep hash calculation within 32-bit SMI range.
ahe 2017/02/14 13:52:46 This is a documentation comment, it should have th
Dmitry Olshansky 2017/02/14 14:31:09 Done.
19 static const int MASK = 16 * 1024 * 1024 - 1;
20
21 int _size; // Linear size of hash table.
ahe 2017/02/14 13:52:46 No need for privacy.
ahe 2017/02/14 13:52:46 This is also a documentation comment, so it should
Dmitry Olshansky 2017/02/14 14:31:09 On the contrary I believe that direct access to _s
22 int _count; // Items in a hash table.
23 _Node _nodes; // The table itself.
24
25 HashCanonicalizer() {
26 _count = 0;
ahe 2017/02/14 13:52:46 Move the field initializers to the field declarati
Dmitry Olshansky 2017/02/14 14:31:09 Done.
27 _size = 8 * 1024;
28 _nodes = new List<_Node>(_size);
29 }
30
31 static String decode(List<int> data, int start, int end, bool asciiOnly) {
32 var s;
33 if (asciiOnly) {
34 s = new String.fromCharCodes(data, start, end);
35 } else {
36 s = UTF8.decoder.convert(data, start, end);
ahe 2017/02/14 13:52:46 You need to change this to: new Utf8Decoder(allow
Dmitry Olshansky 2017/02/14 14:31:09 Done.
37 }
38 return s;
39 }
40
41 static int hashBytes(List<int> data, int start, int end) {
42 int h = 5381;
43 for (int i = start; i < end; i++) {
44 h = ((h << 5) + h + data[i]) & MASK;
45 }
46 return h;
47 }
48
49 static int hashString(String data, int start, int end) {
50 int h = 5381;
51 for (int i = start; i < end; i++) {
52 h = ((h << 5) + h + data.codeUnitAt(i)) & MASK;
53 }
54 return h;
55 }
56
57 _rehash() {
ahe 2017/02/14 13:52:46 No need for privacy.
Dmitry Olshansky 2017/02/14 14:31:09 Done.
58 var newSize = _size * 2;
59 var newNodes = new List<_Node>(newSize);
60 for (int i = 0; i < _size; i++) {
61 _Node t = _nodes[i];
62 while (t != null) {
63 _Node n = t.next;
64 int newIndex = hashBytes(t.data, t.start, t.end) & (newSize - 1);
65 _Node s = newNodes[newIndex];
66 t.next = s;
67 newNodes[newIndex] = t;
68 t = n;
69 }
70 }
71 _size = newSize;
72 _nodes = newNodes;
73 }
74
75 String canonicalize(data, int start, int end, bool asciiOnly) {
76 if (_count > _size) _rehash();
77 int index = data is String
78 ? hashString(data, start, end)
79 : hashBytes(data, start, end);
80 index = index & (_size - 1);
81 _Node s = _nodes[index];
82 _Node t = s;
83 int len = end - start;
84 while (t != null) {
85 if (t.end - t.start == len) {
86 int i = start, j = t.start;
87 while (i < end && data[i] == t.data[j]) {
88 i++;
89 j++;
90 }
91 if (i == end) {
92 return t.payload;
93 }
94 }
95 t = t.next;
96 }
97 String payload;
98 if (data is String)
99 payload = data.substring(start, end);
100 else
101 payload = decode(data, start, end, asciiOnly);
102 _nodes[index] = new _Node(data, start, end, payload, s);
103 _count++;
104 return payload;
105 }
106
107 clear() {
108 _nodes = new List<_Node>(size);
109 }
110 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698