Skip to content

Commit ff25b2c

Browse files
committed
get_max: do not parse every symbol name in the symbol table
The typical case is that we have a small number of entries with the same prefix. Just perform that small number of name lookups (which are constant time after the log-time conversion from a string to an irep_idt). This speeds up function pointer removal on a build of the Linux kernel from 4610 seconds to just 86 seconds. Also rename get_max to smallest_unused_suffix to disambiguate the case of a symbol not being present, which in turn simplifies the use.
1 parent b603703 commit ff25b2c

File tree

3 files changed

+21
-31
lines changed

3 files changed

+21
-31
lines changed

src/util/namespace.cpp

+13-23
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,14 @@ Author: Daniel Kroening, [email protected]
2121
#include "string2int.h"
2222
#include "symbol_table.h"
2323

24-
unsigned get_max(
24+
static std::size_t smallest_unused_suffix(
2525
const std::string &prefix,
2626
const symbol_tablet::symbolst &symbols)
2727
{
28-
unsigned max_nr=0;
28+
std::size_t max_nr = 0;
2929

30-
for(const auto &symbol_pair : symbols)
31-
{
32-
if(has_prefix(id2string(symbol_pair.first), prefix))
33-
{
34-
max_nr = std::max(
35-
unsafe_string2unsigned(
36-
id2string(symbol_pair.first).substr(prefix.size())),
37-
max_nr);
38-
}
39-
}
30+
while(symbols.find(prefix + std::to_string(max_nr)) != symbols.end())
31+
++max_nr;
4032

4133
return max_nr;
4234
}
@@ -122,15 +114,15 @@ void namespace_baset::follow_macros(exprt &expr) const
122114
follow_macros(*it);
123115
}
124116

125-
unsigned namespacet::get_max(const std::string &prefix) const
117+
std::size_t namespacet::smallest_unused_suffix(const std::string &prefix) const
126118
{
127-
unsigned m=0;
119+
std::size_t m = 0;
128120

129121
if(symbol_table1!=nullptr)
130-
m=std::max(m, ::get_max(prefix, symbol_table1->symbols));
122+
m = std::max(m, ::smallest_unused_suffix(prefix, symbol_table1->symbols));
131123

132124
if(symbol_table2!=nullptr)
133-
m=std::max(m, ::get_max(prefix, symbol_table2->symbols));
125+
m = std::max(m, ::smallest_unused_suffix(prefix, symbol_table2->symbols));
134126

135127
return m;
136128
}
@@ -166,15 +158,13 @@ bool namespacet::lookup(
166158
return true;
167159
}
168160

169-
unsigned multi_namespacet::get_max(const std::string &prefix) const
161+
std::size_t
162+
multi_namespacet::smallest_unused_suffix(const std::string &prefix) const
170163
{
171-
unsigned m=0;
164+
std::size_t m = 0;
172165

173-
for(symbol_table_listt::const_iterator
174-
it=symbol_table_list.begin();
175-
it!=symbol_table_list.end();
176-
it++)
177-
m=std::max(m, ::get_max(prefix, (*it)->symbols));
166+
for(const auto &st : symbol_table_list)
167+
m = std::max(m, ::smallest_unused_suffix(prefix, st->symbols));
178168

179169
return m;
180170
}

src/util/namespace.h

+7-7
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,12 @@ class namespace_baset
5353
const typet &follow_tag(const struct_tag_typet &) const;
5454
const typet &follow_tag(const c_enum_tag_typet &) const;
5555

56-
/// Returns the maximum integer n such that there is a symbol (in some of the
57-
/// symbol tables) whose name is of the form "AB" where A is \p prefix and B
58-
/// is n. Symbols where B is not a number count as if B was equal to 0.
56+
/// Returns the minimal integer n such that there is no symbol (in any of the
57+
/// symbol tables) whose name is of the form "An" where A is \p prefix.
5958
/// The intended use case is finding the next available symbol name for a
6059
/// sequence of auto-generated symbols.
61-
virtual unsigned get_max(const std::string &prefix) const=0;
60+
virtual std::size_t
61+
smallest_unused_suffix(const std::string &prefix) const = 0;
6262

6363
/// Searches for a symbol named \p name. Iff found, set \p symbol to point to
6464
/// the symbol and return false; else \p symbol is unmodified and `true` is
@@ -100,8 +100,8 @@ class namespacet:public namespace_baset
100100
/// tables.
101101
bool lookup(const irep_idt &name, const symbolt *&symbol) const override;
102102

103-
/// See documentation for namespace_baset::get_max().
104-
unsigned get_max(const std::string &prefix) const override;
103+
/// See documentation for namespace_baset::smallest_unused_suffix().
104+
std::size_t smallest_unused_suffix(const std::string &prefix) const override;
105105

106106
const symbol_tablet &get_symbol_table() const
107107
{
@@ -130,7 +130,7 @@ class multi_namespacet:public namespacet
130130
using namespace_baset::lookup;
131131

132132
bool lookup(const irep_idt &name, const symbolt *&symbol) const override;
133-
unsigned get_max(const std::string &prefix) const override;
133+
std::size_t smallest_unused_suffix(const std::string &prefix) const override;
134134

135135
void add(const symbol_tablet &symbol_table)
136136
{

src/util/rename.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@ void get_new_name(irep_idt &new_name, const namespacet &ns, char delimiter)
3232

3333
std::string prefix = id2string(new_name) + delimiter;
3434

35-
new_name=prefix+std::to_string(ns.get_max(prefix)+1);
35+
new_name = prefix + std::to_string(ns.smallest_unused_suffix(prefix));
3636
}

0 commit comments

Comments
 (0)