From Craig Silverstein: Only sort for suffixes with -O2.
[binutils.git] / gold / stringpool.cc
blob43f63282db299f9d0e4207b1cac689d0d524dcb7
1 // stringpool.cc -- a string pool for gold
3 #include "gold.h"
5 #include <cstring>
6 #include <algorithm>
7 #include <vector>
9 #include "output.h"
10 #include "parameters.h"
11 #include "stringpool.h"
13 namespace gold
16 template<typename Stringpool_char>
17 Stringpool_template<Stringpool_char>::Stringpool_template()
18 : string_set_(), strings_(), strtab_size_(0), next_index_(1),
19 zero_null_(true)
23 template<typename Stringpool_char>
24 Stringpool_template<Stringpool_char>::~Stringpool_template()
26 for (typename std::list<Stringdata*>::iterator p = this->strings_.begin();
27 p != this->strings_.end();
28 ++p)
29 delete[] reinterpret_cast<char*>(*p);
32 // Return the length of a string of arbitrary character type.
34 template<typename Stringpool_char>
35 size_t
36 Stringpool_template<Stringpool_char>::string_length(const Stringpool_char* p)
38 size_t len = 0;
39 for (; *p != 0; ++p)
40 ++len;
41 return len;
44 // Specialize string_length for char. Maybe we could just use
45 // std::char_traits<>::length?
47 template<>
48 inline size_t
49 Stringpool_template<char>::string_length(const char* p)
51 return strlen(p);
54 // Equality comparison function.
56 template<typename Stringpool_char>
57 bool
58 Stringpool_template<Stringpool_char>::Stringpool_eq::operator()(
59 const Stringpool_char* s1,
60 const Stringpool_char* s2) const
62 while (*s1 != 0)
63 if (*s1++ != *s2++)
64 return false;
65 return *s2 == 0;
68 // Specialize equality comparison for char.
70 template<>
71 bool
72 Stringpool_template<char>::Stringpool_eq::operator()(const char* s1,
73 const char* s2) const
75 return strcmp(s1, s2) == 0;
78 // Hash function.
80 template<typename Stringpool_char>
81 size_t
82 Stringpool_template<Stringpool_char>::Stringpool_hash::operator()(
83 const Stringpool_char* s) const
85 // Fowler/Noll/Vo (FNV) hash (type FNV-1a).
86 if (sizeof(size_t) == 8)
88 size_t result = static_cast<size_t>(14695981039346656037ULL);
89 while (*s != 0)
91 const char* p = reinterpret_cast<const char*>(s);
92 for (size_t i = 0; i < sizeof(Stringpool_char); ++i)
94 result &= (size_t) *p++;
95 result *= 1099511628211ULL;
97 ++s;
99 return result;
101 else
103 size_t result = 2166136261UL;
104 while (*s != 0)
106 const char* p = reinterpret_cast<const char*>(s);
107 for (size_t i = 0; i < sizeof(Stringpool_char); ++i)
109 result ^= (size_t) *p++;
110 result *= 16777619UL;
112 ++s;
114 return result;
118 // Add a string to the list of canonical strings. Return a pointer to
119 // the canonical string. If PKEY is not NULL, set *PKEY to the key.
121 template<typename Stringpool_char>
122 const Stringpool_char*
123 Stringpool_template<Stringpool_char>::add_string(const Stringpool_char* s,
124 Key* pkey)
126 // We are in trouble if we've already computed the string offsets.
127 gold_assert(this->strtab_size_ == 0);
129 // The size we allocate for a new Stringdata.
130 const size_t buffer_size = 1000;
131 // The amount we multiply the Stringdata index when calculating the
132 // key.
133 const size_t key_mult = 1024;
134 gold_assert(key_mult >= buffer_size);
136 size_t len = (string_length(s) + 1) * sizeof(Stringpool_char);
138 size_t alc;
139 bool front = true;
140 if (len > buffer_size)
142 alc = sizeof(Stringdata) + len;
143 front = false;
145 else if (this->strings_.empty())
146 alc = sizeof(Stringdata) + buffer_size;
147 else
149 Stringdata *psd = this->strings_.front();
150 if (len > psd->alc - psd->len)
151 alc = sizeof(Stringdata) + buffer_size;
152 else
154 char* ret = psd->data + psd->len;
155 memcpy(ret, s, len);
157 if (pkey != NULL)
158 *pkey = psd->index * key_mult + psd->len;
160 psd->len += len;
162 return reinterpret_cast<const Stringpool_char*>(ret);
166 Stringdata *psd = reinterpret_cast<Stringdata*>(new char[alc]);
167 psd->alc = alc - sizeof(Stringdata);
168 memcpy(psd->data, s, len);
169 psd->len = len;
170 psd->index = this->next_index_;
171 ++this->next_index_;
173 if (pkey != NULL)
174 *pkey = psd->index * key_mult;
176 if (front)
177 this->strings_.push_front(psd);
178 else
179 this->strings_.push_back(psd);
181 return reinterpret_cast<const Stringpool_char*>(psd->data);
184 // Add a string to a string pool.
186 template<typename Stringpool_char>
187 const Stringpool_char*
188 Stringpool_template<Stringpool_char>::add(const Stringpool_char* s, Key* pkey)
190 // FIXME: This will look up the entry twice in the hash table. The
191 // problem is that we can't insert S before we canonicalize it. I
192 // don't think there is a way to handle this correctly with
193 // unordered_map, so this should be replaced with custom code to do
194 // what we need, which is to return the empty slot.
196 typename String_set_type::const_iterator p = this->string_set_.find(s);
197 if (p != this->string_set_.end())
199 if (pkey != NULL)
200 *pkey = p->second.first;
201 return p->first;
204 Key k;
205 const Stringpool_char* ret = this->add_string(s, &k);
207 const off_t ozero = 0;
208 std::pair<const Stringpool_char*, Val> element(ret,
209 std::make_pair(k, ozero));
210 std::pair<typename String_set_type::iterator, bool> ins =
211 this->string_set_.insert(element);
212 gold_assert(ins.second);
214 if (pkey != NULL)
215 *pkey = k;
217 return ret;
220 // Add a prefix of a string to a string pool.
222 template<typename Stringpool_char>
223 const Stringpool_char*
224 Stringpool_template<Stringpool_char>::add(const Stringpool_char* s, size_t len,
225 Key* pkey)
227 // FIXME: This implementation should be rewritten when we rewrite
228 // the hash table to avoid copying.
229 std::basic_string<Stringpool_char> st(s, len);
230 return this->add(st, pkey);
233 template<typename Stringpool_char>
234 const Stringpool_char*
235 Stringpool_template<Stringpool_char>::find(const Stringpool_char* s,
236 Key* pkey) const
238 typename String_set_type::const_iterator p = this->string_set_.find(s);
239 if (p == this->string_set_.end())
240 return NULL;
242 if (pkey != NULL)
243 *pkey = p->second.first;
245 return p->first;
248 // Comparison routine used when sorting into an ELF strtab. We want
249 // to sort this so that when one string is a suffix of another, we
250 // always see the shorter string immediately after the longer string.
251 // For example, we want to see these strings in this order:
252 // abcd
253 // cd
254 // d
255 // When strings are not suffixes, we don't care what order they are
256 // in, but we need to ensure that suffixes wind up next to each other.
257 // So we do a reversed lexicographic sort on the reversed string.
259 template<typename Stringpool_char>
260 bool
261 Stringpool_template<Stringpool_char>::Stringpool_sort_comparison::operator()(
262 const Stringpool_sort_info& sort_info1,
263 const Stringpool_sort_info& sort_info2) const
265 const Stringpool_char* s1 = sort_info1.it->first;
266 const Stringpool_char* s2 = sort_info2.it->first;
267 const size_t len1 = sort_info1.string_length;
268 const size_t len2 = sort_info2.string_length;
269 const size_t minlen = len1 < len2 ? len1 : len2;
270 const Stringpool_char* p1 = s1 + len1 - 1;
271 const Stringpool_char* p2 = s2 + len2 - 1;
272 for (size_t i = minlen; i > 0; --i, --p1, --p2)
274 if (*p1 != *p2)
275 return *p1 > *p2;
277 return len1 > len2;
280 // Return whether s1 is a suffix of s2.
282 template<typename Stringpool_char>
283 bool
284 Stringpool_template<Stringpool_char>::is_suffix(const Stringpool_char* s1,
285 size_t len1,
286 const Stringpool_char* s2,
287 size_t len2)
289 if (len1 > len2)
290 return false;
291 return memcmp(s1, s2 + len2 - len1, len1 * sizeof(Stringpool_char)) == 0;
294 // Turn the stringpool into an ELF strtab: determine the offsets of
295 // each string in the table.
297 template<typename Stringpool_char>
298 void
299 Stringpool_template<Stringpool_char>::set_string_offsets()
301 if (this->strtab_size_ != 0)
303 // We've already computed the offsets.
304 return;
307 const size_t charsize = sizeof(Stringpool_char);
309 // Offset 0 may be reserved for the empty string.
310 off_t offset = this->zero_null_ ? charsize : 0;
312 // Sorting to find suffixes can take over 25% of the total CPU time
313 // used by the linker. Since it's merely an optimization to reduce
314 // the strtab size, and gives a relatively small benefit (it's
315 // typically rare for a symbol to be a suffix of another), we only
316 // take the time to sort when the user asks for heavy optimization.
317 if (parameters->optimization_level() < 2)
319 for (typename String_set_type::iterator curr = this->string_set_.begin();
320 curr != this->string_set_.end();
321 curr++)
323 if (this->zero_null_ && curr->first[0] == 0)
324 curr->second.second = 0;
325 else
327 curr->second.second = offset;
328 offset += (string_length(curr->first) + 1) * charsize;
332 else
334 size_t count = this->string_set_.size();
336 std::vector<Stringpool_sort_info> v;
337 v.reserve(count);
339 for (typename String_set_type::iterator p = this->string_set_.begin();
340 p != this->string_set_.end();
341 ++p)
342 v.push_back(Stringpool_sort_info(p, string_length(p->first)));
344 std::sort(v.begin(), v.end(), Stringpool_sort_comparison());
346 for (typename std::vector<Stringpool_sort_info>::iterator last = v.end(),
347 curr = v.begin();
348 curr != v.end();
349 last = curr++)
351 if (this->zero_null_ && curr->it->first[0] == 0)
352 curr->it->second.second = 0;
353 else if (last != v.end()
354 && is_suffix(curr->it->first, curr->string_length,
355 last->it->first, last->string_length))
356 curr->it->second.second = (last->it->second.second
357 + ((last->string_length
358 - curr->string_length)
359 * charsize));
360 else
362 curr->it->second.second = offset;
363 offset += (curr->string_length + 1) * charsize;
368 this->strtab_size_ = offset;
371 // Get the offset of a string in the ELF strtab. The string must
372 // exist.
374 template<typename Stringpool_char>
375 off_t
376 Stringpool_template<Stringpool_char>::get_offset(const Stringpool_char* s)
377 const
379 gold_assert(this->strtab_size_ != 0);
380 typename String_set_type::const_iterator p = this->string_set_.find(s);
381 if (p != this->string_set_.end())
382 return p->second.second;
383 gold_unreachable();
386 // Write the ELF strtab into the output file at the specified offset.
388 template<typename Stringpool_char>
389 void
390 Stringpool_template<Stringpool_char>::write(Output_file* of, off_t offset)
392 gold_assert(this->strtab_size_ != 0);
393 unsigned char* viewu = of->get_output_view(offset, this->strtab_size_);
394 char* view = reinterpret_cast<char*>(viewu);
395 if (this->zero_null_)
396 view[0] = '\0';
397 for (typename String_set_type::const_iterator p = this->string_set_.begin();
398 p != this->string_set_.end();
399 ++p)
400 memcpy(view + p->second.second, p->first,
401 (string_length(p->first) + 1) * sizeof(Stringpool_char));
402 of->write_output_view(offset, this->strtab_size_, viewu);
405 // Instantiate the templates we need.
407 template
408 class Stringpool_template<char>;
410 template
411 class Stringpool_template<uint16_t>;
413 template
414 class Stringpool_template<uint32_t>;
416 } // End namespace gold.