Rework Stringpool to not compute the hash code twice when adding a new
[deliverable/binutils-gdb.git] / gold / stringpool.cc
1 // stringpool.cc -- a string pool for gold
2
3 // Copyright 2006, 2007 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
5
6 // This file is part of gold.
7
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
12
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
17
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
22
23 #include "gold.h"
24
25 #include <cstring>
26 #include <algorithm>
27 #include <vector>
28
29 #include "output.h"
30 #include "parameters.h"
31 #include "stringpool.h"
32
33 namespace gold
34 {
35
36 template<typename Stringpool_char>
37 Stringpool_template<Stringpool_char>::Stringpool_template()
38 : string_set_(), strings_(), strtab_size_(0), next_index_(1),
39 next_uncopied_key_(-1), zero_null_(true)
40 {
41 }
42
43 template<typename Stringpool_char>
44 void
45 Stringpool_template<Stringpool_char>::clear()
46 {
47 for (typename std::list<Stringdata*>::iterator p = this->strings_.begin();
48 p != this->strings_.end();
49 ++p)
50 delete[] reinterpret_cast<char*>(*p);
51 this->strings_.clear();
52 this->string_set_.clear();
53 }
54
55 template<typename Stringpool_char>
56 Stringpool_template<Stringpool_char>::~Stringpool_template()
57 {
58 this->clear();
59 }
60
61 // Return the length of a string of arbitrary character type.
62
63 template<typename Stringpool_char>
64 size_t
65 Stringpool_template<Stringpool_char>::string_length(const Stringpool_char* p)
66 {
67 size_t len = 0;
68 for (; *p != 0; ++p)
69 ++len;
70 return len;
71 }
72
73 // Specialize string_length for char. Maybe we could just use
74 // std::char_traits<>::length?
75
76 template<>
77 inline size_t
78 Stringpool_template<char>::string_length(const char* p)
79 {
80 return strlen(p);
81 }
82
83 // Compare two strings of arbitrary character type for equality.
84
85 template<typename Stringpool_char>
86 bool
87 Stringpool_template<Stringpool_char>::string_equal(const Stringpool_char* s1,
88 const Stringpool_char* s2)
89 {
90 while (*s1 != 0)
91 if (*s1++ != *s2++)
92 return false;
93 return *s2 == 0;
94 }
95
96 // Specialize string_equal for char.
97
98 template<>
99 inline bool
100 Stringpool_template<char>::string_equal(const char* s1, const char* s2)
101 {
102 return strcmp(s1, s2) == 0;
103 }
104
105 // Equality comparison function for the hash table.
106
107 template<typename Stringpool_char>
108 inline bool
109 Stringpool_template<Stringpool_char>::Stringpool_eq::operator()(
110 const Hashkey& h1,
111 const Hashkey& h2) const
112 {
113 return (h1.hash_code == h2.hash_code
114 && h1.length == h2.length
115 && memcmp(h1.string, h2.string,
116 h1.length * sizeof(Stringpool_char)) == 0);
117 }
118
119 // Hash function. The length is in characters, not bytes.
120
121 template<typename Stringpool_char>
122 size_t
123 Stringpool_template<Stringpool_char>::string_hash(const Stringpool_char* s,
124 size_t length)
125 {
126 // Fowler/Noll/Vo (FNV) hash (type FNV-1a).
127 const unsigned char* p = reinterpret_cast<const unsigned char*>(s);
128 if (sizeof(size_t) > 4)
129 {
130 size_t result = static_cast<size_t>(14695981039346656037ULL);
131 for (size_t i = 0; i < length * sizeof(Stringpool_char); ++i)
132 {
133 result ^= static_cast<size_t>(*p++);
134 result *= 1099511628211ULL;
135 }
136 return result;
137 }
138 else
139 {
140 size_t result = 2166136261UL;
141 for (size_t i = 0; i < length * sizeof(Stringpool_char); ++i)
142 {
143 result ^= static_cast<size_t>(*p++);
144 result *= 16777619UL;
145 }
146 return result;
147 }
148 }
149
150 // Add the string S to the list of canonical strings. Return a
151 // pointer to the canonical string. If PKEY is not NULL, set *PKEY to
152 // the key. LENGTH is the length of S in characters. Note that S may
153 // not be NUL terminated.
154
155 template<typename Stringpool_char>
156 const Stringpool_char*
157 Stringpool_template<Stringpool_char>::add_string(const Stringpool_char* s,
158 size_t len,
159 Key* pkey)
160 {
161 // We are in trouble if we've already computed the string offsets.
162 gold_assert(this->strtab_size_ == 0);
163
164 // The size we allocate for a new Stringdata.
165 const size_t buffer_size = 1000;
166 // The amount we multiply the Stringdata index when calculating the
167 // key.
168 const size_t key_mult = 1024;
169 gold_assert(key_mult >= buffer_size);
170
171 // Convert len to the number of bytes we need to allocate, including
172 // the null character.
173 len = (len + 1) * sizeof(Stringpool_char);
174
175 size_t alc;
176 bool front = true;
177 if (len > buffer_size)
178 {
179 alc = sizeof(Stringdata) + len;
180 front = false;
181 }
182 else if (this->strings_.empty())
183 alc = sizeof(Stringdata) + buffer_size;
184 else
185 {
186 Stringdata *psd = this->strings_.front();
187 if (len > psd->alc - psd->len)
188 alc = sizeof(Stringdata) + buffer_size;
189 else
190 {
191 char* ret = psd->data + psd->len;
192 memcpy(ret, s, len - sizeof(Stringpool_char));
193 memset(ret + len - sizeof(Stringpool_char), 0,
194 sizeof(Stringpool_char));
195
196 if (pkey != NULL)
197 *pkey = psd->index * key_mult + psd->len;
198
199 psd->len += len;
200
201 return reinterpret_cast<const Stringpool_char*>(ret);
202 }
203 }
204
205 Stringdata *psd = reinterpret_cast<Stringdata*>(new char[alc]);
206 psd->alc = alc - sizeof(Stringdata);
207 memcpy(psd->data, s, len - sizeof(Stringpool_char));
208 memset(psd->data + len - sizeof(Stringpool_char), 0,
209 sizeof(Stringpool_char));
210 psd->len = len;
211 psd->index = this->next_index_;
212 ++this->next_index_;
213
214 if (pkey != NULL)
215 *pkey = psd->index * key_mult;
216
217 if (front)
218 this->strings_.push_front(psd);
219 else
220 this->strings_.push_back(psd);
221
222 return reinterpret_cast<const Stringpool_char*>(psd->data);
223 }
224
225 // Add a string to a string pool.
226
227 template<typename Stringpool_char>
228 const Stringpool_char*
229 Stringpool_template<Stringpool_char>::add(const Stringpool_char* s, bool copy,
230 Key* pkey)
231 {
232 typedef std::pair<typename String_set_type::iterator, bool> Insert_type;
233
234 if (!copy)
235 {
236 // When we don't need to copy the string, we can call insert
237 // directly.
238
239 const Key k = this->next_uncopied_key_;
240 const off_t ozero = 0;
241 std::pair<Hashkey, Hashval> element(Hashkey(s),
242 std::make_pair(k, ozero));
243
244 Insert_type ins = this->string_set_.insert(element);
245
246 typename String_set_type::const_iterator p = ins.first;
247
248 if (ins.second)
249 {
250 // We just added the string. The key value has now been
251 // used.
252 --this->next_uncopied_key_;
253 }
254 else
255 {
256 gold_assert(k != p->second.first);
257 }
258
259 if (pkey != NULL)
260 *pkey = p->second.first;
261 return p->first.string;
262 }
263
264 return this->add_prefix(s, string_length(s), pkey);
265 }
266
267 // Add a prefix of a string to a string pool.
268
269 template<typename Stringpool_char>
270 const Stringpool_char*
271 Stringpool_template<Stringpool_char>::add_prefix(const Stringpool_char* s,
272 size_t len,
273 Key* pkey)
274 {
275 // When adding an entry, this will look it up twice in the hash
276 // table. The problem is that we can't insert S before we
277 // canonicalize it by copying it into the canonical list. The hash
278 // code will only be computed once, so this isn't all that
279 // expensive.
280
281 Hashkey hk(s, len);
282 typename String_set_type::const_iterator p = this->string_set_.find(hk);
283 if (p != this->string_set_.end())
284 {
285 if (pkey != NULL)
286 *pkey = p->second.first;
287 return p->first.string;
288 }
289
290 Key k;
291 hk.string = this->add_string(s, len, &k);
292 // The contents of the string stay the same, so we don't need to
293 // adjust hk.hash_code or hk.length.
294
295 const off_t ozero = 0;
296 std::pair<Hashkey, Hashval> element(hk, std::make_pair(k, ozero));
297
298 typedef std::pair<typename String_set_type::iterator, bool> Insert_type;
299 Insert_type ins = this->string_set_.insert(element);
300 gold_assert(ins.second);
301
302 if (pkey != NULL)
303 *pkey = k;
304 return hk.string;
305 }
306
307 template<typename Stringpool_char>
308 const Stringpool_char*
309 Stringpool_template<Stringpool_char>::find(const Stringpool_char* s,
310 Key* pkey) const
311 {
312 Hashkey hk(s);
313 typename String_set_type::const_iterator p = this->string_set_.find(hk);
314 if (p == this->string_set_.end())
315 return NULL;
316
317 if (pkey != NULL)
318 *pkey = p->second.first;
319
320 return p->first.string;
321 }
322
323 // Comparison routine used when sorting into an ELF strtab. We want
324 // to sort this so that when one string is a suffix of another, we
325 // always see the shorter string immediately after the longer string.
326 // For example, we want to see these strings in this order:
327 // abcd
328 // cd
329 // d
330 // When strings are not suffixes, we don't care what order they are
331 // in, but we need to ensure that suffixes wind up next to each other.
332 // So we do a reversed lexicographic sort on the reversed string.
333
334 template<typename Stringpool_char>
335 bool
336 Stringpool_template<Stringpool_char>::Stringpool_sort_comparison::operator()(
337 const Stringpool_sort_info& sort_info1,
338 const Stringpool_sort_info& sort_info2) const
339 {
340 const Hashkey& h1(sort_info1->first);
341 const Hashkey& h2(sort_info2->first);
342 const Stringpool_char* s1 = h1.string;
343 const Stringpool_char* s2 = h2.string;
344 const size_t len1 = h1.length;
345 const size_t len2 = h2.length;
346 const size_t minlen = len1 < len2 ? len1 : len2;
347 const Stringpool_char* p1 = s1 + len1 - 1;
348 const Stringpool_char* p2 = s2 + len2 - 1;
349 for (size_t i = minlen; i > 0; --i, --p1, --p2)
350 {
351 if (*p1 != *p2)
352 return *p1 > *p2;
353 }
354 return len1 > len2;
355 }
356
357 // Return whether s1 is a suffix of s2.
358
359 template<typename Stringpool_char>
360 bool
361 Stringpool_template<Stringpool_char>::is_suffix(const Stringpool_char* s1,
362 size_t len1,
363 const Stringpool_char* s2,
364 size_t len2)
365 {
366 if (len1 > len2)
367 return false;
368 return memcmp(s1, s2 + len2 - len1, len1 * sizeof(Stringpool_char)) == 0;
369 }
370
371 // Turn the stringpool into an ELF strtab: determine the offsets of
372 // each string in the table.
373
374 template<typename Stringpool_char>
375 void
376 Stringpool_template<Stringpool_char>::set_string_offsets()
377 {
378 if (this->strtab_size_ != 0)
379 {
380 // We've already computed the offsets.
381 return;
382 }
383
384 const size_t charsize = sizeof(Stringpool_char);
385
386 // Offset 0 may be reserved for the empty string.
387 off_t offset = this->zero_null_ ? charsize : 0;
388
389 // Sorting to find suffixes can take over 25% of the total CPU time
390 // used by the linker. Since it's merely an optimization to reduce
391 // the strtab size, and gives a relatively small benefit (it's
392 // typically rare for a symbol to be a suffix of another), we only
393 // take the time to sort when the user asks for heavy optimization.
394 if (parameters->optimization_level() < 2)
395 {
396 for (typename String_set_type::iterator curr = this->string_set_.begin();
397 curr != this->string_set_.end();
398 curr++)
399 {
400 if (this->zero_null_ && curr->first.string[0] == 0)
401 curr->second.second = 0;
402 else
403 {
404 curr->second.second = offset;
405 offset += (curr->first.length + 1) * charsize;
406 }
407 }
408 }
409 else
410 {
411 size_t count = this->string_set_.size();
412
413 std::vector<Stringpool_sort_info> v;
414 v.reserve(count);
415
416 for (typename String_set_type::iterator p = this->string_set_.begin();
417 p != this->string_set_.end();
418 ++p)
419 v.push_back(Stringpool_sort_info(p));
420
421 std::sort(v.begin(), v.end(), Stringpool_sort_comparison());
422
423 for (typename std::vector<Stringpool_sort_info>::iterator last = v.end(),
424 curr = v.begin();
425 curr != v.end();
426 last = curr++)
427 {
428 if (this->zero_null_ && (*curr)->first.string[0] == 0)
429 (*curr)->second.second = 0;
430 else if (last != v.end()
431 && is_suffix((*curr)->first.string,
432 (*curr)->first.length,
433 (*last)->first.string,
434 (*last)->first.length))
435 (*curr)->second.second = ((*last)->second.second
436 + (((*last)->first.length
437 - (*curr)->first.length)
438 * charsize));
439 else
440 {
441 (*curr)->second.second = offset;
442 offset += ((*curr)->first.length + 1) * charsize;
443 }
444 }
445 }
446
447 this->strtab_size_ = offset;
448 }
449
450 // Get the offset of a string in the ELF strtab. The string must
451 // exist.
452
453 template<typename Stringpool_char>
454 off_t
455 Stringpool_template<Stringpool_char>::get_offset(const Stringpool_char* s)
456 const
457 {
458 gold_assert(this->strtab_size_ != 0);
459 typename String_set_type::const_iterator p = this->string_set_.find(s);
460 if (p != this->string_set_.end())
461 return p->second.second;
462 gold_unreachable();
463 }
464
465 // Write the ELF strtab into the buffer.
466
467 template<typename Stringpool_char>
468 void
469 Stringpool_template<Stringpool_char>::write_to_buffer(unsigned char* buffer,
470 size_t bufsize)
471 {
472 gold_assert(this->strtab_size_ != 0);
473 // Quiet the compiler in opt mode.
474 if (bufsize < static_cast<size_t>(this->strtab_size_))
475 gold_assert(bufsize >= static_cast<size_t>(this->strtab_size_));
476 if (this->zero_null_)
477 buffer[0] = '\0';
478 for (typename String_set_type::const_iterator p = this->string_set_.begin();
479 p != this->string_set_.end();
480 ++p)
481 {
482 const int len = (p->first.length + 1) * sizeof(Stringpool_char);
483 gold_assert(p->second.second + len <= this->strtab_size_);
484 memcpy(buffer + p->second.second, p->first.string, len);
485 }
486 }
487
488 // Write the ELF strtab into the output file at the specified offset.
489
490 template<typename Stringpool_char>
491 void
492 Stringpool_template<Stringpool_char>::write(Output_file* of, off_t offset)
493 {
494 gold_assert(this->strtab_size_ != 0);
495 unsigned char* view = of->get_output_view(offset, this->strtab_size_);
496 this->write_to_buffer(view, this->strtab_size_);
497 of->write_output_view(offset, this->strtab_size_, view);
498 }
499
500 // Print statistical information to stderr. This is used for --stats.
501
502 template<typename Stringpool_char>
503 void
504 Stringpool_template<Stringpool_char>::print_stats(const char* name) const
505 {
506 #if defined(HAVE_TR1_UNORDERED_MAP) || defined(HAVE_EXT_HASH_MAP)
507 fprintf(stderr, _("%s: %s entries: %zu; buckets: %zu\n"),
508 program_name, name, this->string_set_.size(),
509 this->string_set_.bucket_count());
510 #else
511 fprintf(stderr, _("%s: %s entries: %zu\n"),
512 program_name, name, this->table_.size());
513 #endif
514 fprintf(stderr, _("%s: %s Stringdata structures: %zu\n"),
515 program_name, name, this->strings_.size());
516 }
517
518 // Instantiate the templates we need.
519
520 template
521 class Stringpool_template<char>;
522
523 template
524 class Stringpool_template<uint16_t>;
525
526 template
527 class Stringpool_template<uint32_t>;
528
529 } // End namespace gold.
This page took 0.045005 seconds and 5 git commands to generate.