OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "platform/text/CompressibleString.h" | |
6 | |
7 #include "public/platform/Platform.h" | |
8 #include "wtf/Assertions.h" | |
9 #include "wtf/LinkedStack.h" | |
haraken
2016/01/15 12:46:21
This is not necessary.
hajimehoshi
2016/01/18 09:42:26
Done.
| |
10 #include "wtf/Partitions.h" | |
haraken
2016/01/15 12:46:21
Ditto.
hajimehoshi
2016/01/18 09:42:27
Done.
| |
11 #include "wtf/PassOwnPtr.h" | |
haraken
2016/01/15 12:46:21
Ditto.
hajimehoshi
2016/01/18 09:42:26
Done.
| |
12 #include "wtf/WTFThreadData.h" | |
13 #include "wtf/text/WTFString.h" | |
14 | |
15 namespace blink { | |
16 | |
17 class CompressibleStringTable { | |
18 WTF_MAKE_NONCOPYABLE(CompressibleStringTable); | |
19 public: | |
20 static CompressibleStringTable* create(WTFThreadData& data) | |
21 { | |
22 data.m_compressibleStringTable = new CompressibleStringTable; | |
23 data.m_compressibleStringTableDestructor = CompressibleStringTable::dest roy; | |
24 return data.m_compressibleStringTable; | |
25 } | |
26 | |
27 void add(CompressibleStringImpl* string) | |
28 { | |
29 m_table.add(string); | |
haraken
2016/01/15 12:46:21
Can we add:
ASSERT(!m_table.contains(string));
hajimehoshi
2016/01/18 09:42:27
Done.
| |
30 } | |
31 | |
32 bool contains(CompressibleStringImpl* string) const | |
33 { | |
34 return m_table.contains(string); | |
35 } | |
36 | |
37 void remove(CompressibleStringImpl* string) | |
38 { | |
39 m_table.remove(string); | |
haraken
2016/01/15 12:46:21
Can we add:
ASSERT(m_table.contains(string));
hajimehoshi
2016/01/18 09:42:27
Done.
| |
40 } | |
41 | |
42 void compressAll() | |
43 { | |
44 HashSet<CompressibleStringImpl*>::iterator end = m_table.end(); | |
45 for (HashSet<CompressibleStringImpl*>::iterator iter = m_table.begin(); iter != end; ++iter) { | |
46 CompressibleStringImpl* string = *iter; | |
47 if (!string->isCompressed()) | |
48 string->compressString(); | |
49 } | |
50 } | |
51 | |
52 private: | |
53 CompressibleStringTable() { } | |
54 | |
55 static void destroy(CompressibleStringTable* table) | |
56 { | |
57 delete table; | |
58 } | |
59 | |
60 HashSet<CompressibleStringImpl*> m_table; | |
61 }; | |
62 | |
63 static const int CompressibleStringImplSizeThrehold = 100000; | |
64 | |
65 static inline CompressibleStringTable& compressibleStringTable() | |
haraken
2016/01/15 12:46:21
How about creating the table in Platform::initiali
hajimehoshi
2016/01/18 09:42:27
Done (I added initializeCompressibleStringTable fu
| |
66 { | |
67 // Once possible we should make this non-lazy (constructed in WTFThreadData' s constructor). | |
haraken
2016/01/15 12:46:21
Once possible => remove the word
hajimehoshi
2016/01/18 09:42:27
Done.
| |
68 WTFThreadData& data = wtfThreadData(); | |
69 CompressibleStringTable* table = data.compressibleStringTable(); | |
70 if (UNLIKELY(!table)) | |
71 table = CompressibleStringTable::create(data); | |
72 return *table; | |
73 } | |
74 | |
75 bool CompressibleStringImpl::s_isPageBackground = false; | |
76 | |
77 void CompressibleStringImpl::compressAll() | |
78 { | |
79 compressibleStringTable().compressAll(); | |
80 } | |
81 | |
82 void CompressibleStringImpl::setPageBackground(bool isPageBackground) | |
83 { | |
84 s_isPageBackground = isPageBackground; | |
85 } | |
86 | |
87 CompressibleStringImpl::CompressibleStringImpl(PassRefPtr<StringImpl> impl) | |
88 : m_impl(impl) | |
89 , m_isCompressed(false) | |
90 { | |
91 ASSERT(m_impl); | |
92 if (originalContentSizeInBytes() > CompressibleStringImplSizeThrehold) | |
93 compressibleStringTable().add(this); | |
94 } | |
95 | |
96 CompressibleStringImpl::~CompressibleStringImpl() | |
97 { | |
98 if (compressibleStringTable().contains(this)) | |
99 compressibleStringTable().remove(this); | |
100 } | |
101 | |
102 unsigned CompressibleStringImpl::originalContentSizeInBytes() const | |
103 { | |
104 if (is8Bit()) | |
105 return originalLength() * sizeof(LChar); | |
106 return originalLength() * sizeof(UChar); | |
107 } | |
108 | |
109 unsigned CompressibleStringImpl::currentSizeInBytes() const | |
110 { | |
111 return originalContentSizeInBytes(); | |
haraken
2016/01/15 12:46:21
Add a TODO to update this function once we enable
hajimehoshi
2016/01/18 09:42:27
Done.
| |
112 } | |
113 | |
114 String CompressibleStringImpl::toString() | |
115 { | |
116 if (UNLIKELY(isCompressed())) | |
117 decompressString(); | |
118 return m_impl.get(); | |
119 } | |
120 | |
121 const LChar* CompressibleStringImpl::characters8() | |
122 { | |
123 return toString().characters8(); | |
124 } | |
125 | |
126 const UChar* CompressibleStringImpl::characters16() | |
127 { | |
128 return toString().characters16(); | |
129 } | |
130 | |
131 enum { | |
132 CompressingInBackgroundTab, | |
133 DecompressingInBackgroundTab, | |
134 }; | |
135 | |
136 static const char* histogramName() | |
137 { | |
138 return "Memory.CompressibleStringCount"; | |
139 } | |
140 | |
141 // compressString does nothing but collect UMA so far. | |
142 // TODO(hajimehoshi): Implement this. | |
143 void CompressibleStringImpl::compressString() | |
144 { | |
145 if (s_isPageBackground) | |
haraken
2016/01/15 12:46:21
Why do we need the 'if (s_isPageBackground)' check
hajimehoshi
2016/01/18 09:42:26
See the below comment.
| |
146 Platform::current()->histogramCustomCounts(histogramName(), CompressingI nBackgroundTab, 0, 10000, 50); | |
147 | |
148 ASSERT(m_impl); | |
149 ASSERT(!isCompressed()); | |
150 m_isCompressed = true; | |
151 } | |
152 | |
153 // decompressString does nothing but collect UMA so far. | |
154 // TODO(hajimehoshi): Implement this. | |
155 void CompressibleStringImpl::decompressString() | |
156 { | |
157 if (s_isPageBackground) | |
haraken
2016/01/15 12:46:21
Ditto.
hajimehoshi
2016/01/18 09:42:27
It's because it would not make sense to count deco
haraken
2016/01/18 09:58:25
Makes sense. Can we also introduce:
else
Pl
hajimehoshi
2016/01/18 10:31:51
Done.
| |
158 Platform::current()->histogramCustomCounts(histogramName(), Decompressin gInBackgroundTab, 0, 10000, 50); | |
159 | |
160 ASSERT(isCompressed()); | |
161 m_isCompressed = false; | |
162 } | |
163 | |
164 CompressibleString::CompressibleString() | |
165 : m_impl(nullptr) | |
166 { | |
167 } | |
168 | |
169 CompressibleString::CompressibleString(const CompressibleString& rhs) | |
170 : m_impl(rhs.m_impl) | |
171 { | |
172 } | |
173 | |
174 CompressibleString::CompressibleString(PassRefPtr<StringImpl> impl) | |
175 : m_impl(impl ? adoptRef(new CompressibleStringImpl(impl)) : nullptr) | |
176 { | |
177 } | |
178 | |
179 void CompressibleString::compressString() const | |
180 { | |
181 m_impl->compressString(); | |
182 } | |
183 | |
184 void CompressibleString::decompressString() const | |
185 { | |
186 m_impl->decompressString(); | |
187 } | |
188 | |
189 } // namespace blink | |
OLD | NEW |