Skip to content

Commit 5e8d774

Browse files
committed
Use fmt::format() function instead of ""_format literal
This replaces most uses of the ""_format literal with function calls to fmt::format(). The latter form is available in all versions of the fmt library, while the ""_format literal is deprecated in version 8 and removed in version 9. This might cost us some performance and early error detection, because in at least some versions of the fmt library the _format() construct was used to compile the format patterns at run time. That's why there are some uses of the literal left in places where performance might be more critical, they need a closer look. See #1704
1 parent 3d4bb81 commit 5e8d774

21 files changed

+106
-92
lines changed

src/flex-index.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ flex_index_t::create_index(std::string const &qualified_table_name) const
4949

5050
if (m_fillfactor != 0) {
5151
joiner.add("WITH");
52-
joiner.add("(fillfactor = {})"_format(m_fillfactor));
52+
joiner.add(fmt::format("(fillfactor = {})", m_fillfactor));
5353
}
5454

5555
if (!m_tablespace.empty()) {

src/flex-table-column.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -146,21 +146,21 @@ std::string flex_table_column_t::sql_type_name() const
146146
case table_column_type::direction:
147147
return "int2";
148148
case table_column_type::geometry:
149-
return "Geometry(GEOMETRY, {})"_format(m_srid);
149+
return fmt::format("Geometry(GEOMETRY, {})", m_srid);
150150
case table_column_type::point:
151-
return "Geometry(POINT, {})"_format(m_srid);
151+
return fmt::format("Geometry(POINT, {})", m_srid);
152152
case table_column_type::linestring:
153-
return "Geometry(LINESTRING, {})"_format(m_srid);
153+
return fmt::format("Geometry(LINESTRING, {})", m_srid);
154154
case table_column_type::polygon:
155-
return "Geometry(POLYGON, {})"_format(m_srid);
155+
return fmt::format("Geometry(POLYGON, {})", m_srid);
156156
case table_column_type::multipoint:
157-
return "Geometry(MULTIPOINT, {})"_format(m_srid);
157+
return fmt::format("Geometry(MULTIPOINT, {})", m_srid);
158158
case table_column_type::multilinestring:
159-
return "Geometry(MULTILINESTRING, {})"_format(m_srid);
159+
return fmt::format("Geometry(MULTILINESTRING, {})", m_srid);
160160
case table_column_type::multipolygon:
161-
return "Geometry(MULTIPOLYGON, {})"_format(m_srid);
161+
return fmt::format("Geometry(MULTIPOLYGON, {})", m_srid);
162162
case table_column_type::geometrycollection:
163-
return "Geometry(GEOMETRYCOLLECTION, {})"_format(m_srid);
163+
return fmt::format("Geometry(GEOMETRYCOLLECTION, {})", m_srid);
164164
case table_column_type::area:
165165
return "real";
166166
case table_column_type::id_type:
@@ -188,5 +188,6 @@ std::string flex_table_column_t::sql_modifiers() const
188188

189189
std::string flex_table_column_t::sql_create() const
190190
{
191-
return R"("{}" {} {})"_format(m_name, sql_type_name(), sql_modifiers());
191+
return fmt::format(R"("{}" {} {})", m_name, sql_type_name(),
192+
sql_modifiers());
192193
}

src/flex-table.cpp

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -150,15 +150,16 @@ flex_table_column_t &flex_table_t::add_column(std::string const &name,
150150
std::string flex_table_t::build_sql_prepare_get_wkb() const
151151
{
152152
if (has_multicolumn_id_index()) {
153-
return "PREPARE get_wkb(char(1), bigint) AS"
154-
" SELECT \"{}\" FROM {} WHERE \"{}\" = $1 AND \"{}\" = $2"_format(
155-
geom_column().name(), full_name(), m_columns[0].name(),
156-
m_columns[1].name());
153+
return fmt::format(
154+
"PREPARE get_wkb(char(1), bigint) AS"
155+
" SELECT \"{}\" FROM {} WHERE \"{}\" = $1 AND \"{}\" = $2",
156+
geom_column().name(), full_name(), m_columns[0].name(),
157+
m_columns[1].name());
157158
}
158159

159-
return "PREPARE get_wkb(bigint) AS"
160-
" SELECT \"{}\" FROM {} WHERE \"{}\" = $1"_format(
161-
geom_column().name(), full_name(), id_column_names());
160+
return fmt::format("PREPARE get_wkb(bigint) AS"
161+
" SELECT \"{}\" FROM {} WHERE \"{}\" = $1",
162+
geom_column().name(), full_name(), id_column_names());
162163
}
163164

164165
std::string
@@ -167,8 +168,9 @@ flex_table_t::build_sql_create_table(table_type ttype,
167168
{
168169
assert(!m_columns.empty());
169170

170-
std::string sql = "CREATE {} TABLE IF NOT EXISTS {} ("_format(
171-
ttype == table_type::interim ? "UNLOGGED" : "", table_name);
171+
std::string sql =
172+
fmt::format("CREATE {} TABLE IF NOT EXISTS {} (",
173+
ttype == table_type::interim ? "UNLOGGED" : "", table_name);
172174

173175
util::string_joiner_t joiner{','};
174176
for (auto const &column : m_columns) {
@@ -208,8 +210,9 @@ std::string flex_table_t::build_sql_column_list() const
208210

209211
std::string flex_table_t::build_sql_create_id_index() const
210212
{
211-
return "CREATE INDEX ON {} USING BTREE ({}) {}"_format(
212-
full_name(), id_column_names(), tablespace_clause(index_tablespace()));
213+
return fmt::format("CREATE INDEX ON {} USING BTREE ({}) {}", full_name(),
214+
id_column_names(),
215+
tablespace_clause(index_tablespace()));
213216
}
214217

215218
flex_index_t &flex_table_t::add_index(std::string method)
@@ -232,9 +235,9 @@ enable_check_trigger(pg_conn_t *db_connection, flex_table_t const &table)
232235

233236
for (auto const &column : table) {
234237
if (column.is_geometry_column() && column.needs_isvalid()) {
235-
checks.append(
236-
R"((NEW."{0}" IS NULL OR ST_IsValid(NEW."{0}")) AND )"_format(
237-
column.name()));
238+
checks.append(fmt::format(
239+
R"((NEW."{0}" IS NULL OR ST_IsValid(NEW."{0}")) AND )",
240+
column.name()));
238241
}
239242
}
240243

@@ -303,8 +306,9 @@ void table_connection_t::stop(bool updateable, bool append)
303306
flex_table_t::table_type::permanent, table().full_tmp_name()));
304307

305308
std::string const columns = table().build_sql_column_list();
306-
std::string sql = "INSERT INTO {} ({}) SELECT {} FROM {}"_format(
307-
table().full_tmp_name(), columns, columns, table().full_name());
309+
std::string sql = fmt::format("INSERT INTO {} ({}) SELECT {} FROM {}",
310+
table().full_tmp_name(), columns, columns,
311+
table().full_name());
308312

309313
auto const postgis_version = get_postgis_version();
310314

@@ -316,11 +320,11 @@ void table_connection_t::stop(bool updateable, bool append)
316320
log_debug("Using GeoHash for clustering table '{}'",
317321
table().name());
318322
if (table().geom_column().srid() == 4326) {
319-
sql += "ST_GeoHash({},10)"_format(geom_column_name);
323+
sql += fmt::format("ST_GeoHash({},10)", geom_column_name);
320324
} else {
321-
sql +=
322-
"ST_GeoHash(ST_Transform(ST_Envelope({}),4326),10)"_format(
323-
geom_column_name);
325+
sql += fmt::format(
326+
"ST_GeoHash(ST_Transform(ST_Envelope({}),4326),10)",
327+
geom_column_name);
324328
}
325329
sql += " COLLATE \"C\"";
326330
} else {

src/flex-write.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ static void write_null(db_copy_mgr_t<db_deleter_by_type_and_id_t> *copy_mgr,
3636
{
3737
if (column.not_null()) {
3838
throw not_null_exception{
39-
"Can not add NULL to column '{}' declared NOT NULL."_format(
40-
column.name()),
39+
fmt::format("Can not add NULL to column '{}' declared NOT NULL.",
40+
column.name()),
4141
&column};
4242
}
4343
copy_mgr->add_null_column();

src/node-persistent-cache.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ node_persistent_cache::node_persistent_cache(std::string file_name,
3838
if (m_fd < 0) {
3939
throw std::system_error{
4040
errno, std::system_category(),
41-
"Unable to open flatnode file '{}'"_format(m_file_name)};
41+
fmt::format("Unable to open flatnode file '{}'", m_file_name)};
4242
}
4343

4444
m_index = std::make_unique<index_t>(m_fd);

src/output-flex.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -739,7 +739,7 @@ int output_flex_t::table_tostring()
739739
{
740740
auto const &table = get_table_from_param();
741741

742-
std::string const str{"osm2pgsql.Table[{}]"_format(table.name())};
742+
std::string const str{fmt::format("osm2pgsql.Table[{}]", table.name())};
743743
lua_pushstring(lua_state(), str.c_str());
744744

745745
return 1;

src/output-gazetteer.cpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,17 @@ void output_gazetteer_t::start()
5757
/* Create the new table */
5858

5959
std::string const sql =
60-
"CREATE TABLE place ("
61-
" osm_id int8 NOT NULL,"
62-
" osm_type char(1) NOT NULL,"
63-
" class text NOT NULL,"
64-
" type text NOT NULL,"
65-
" name hstore,"
66-
" admin_level smallint,"
67-
" address hstore,"
68-
" extratags hstore," +
69-
" geometry Geometry(Geometry,{}) NOT NULL"_format(srid) + ")" +
60+
fmt::format("CREATE TABLE place ("
61+
" osm_id int8 NOT NULL,"
62+
" osm_type char(1) NOT NULL,"
63+
" class text NOT NULL,"
64+
" type text NOT NULL,"
65+
" name hstore,"
66+
" admin_level smallint,"
67+
" address hstore,"
68+
" extratags hstore,"
69+
" geometry Geometry(Geometry,{}) NOT NULL)",
70+
srid) +
7071
tablespace_clause(get_options()->tblsmain_data);
7172

7273
conn.exec(sql);

src/pgsql-capabilities.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ static void init_set_from_query(std::set<std::string> *set,
3232
char const *table, char const *column,
3333
char const *condition = "true")
3434
{
35-
auto const res = db_connection.exec(
36-
"SELECT {} FROM {} WHERE {}"_format(column, table, condition));
35+
auto const res = db_connection.exec("SELECT {} FROM {} WHERE {}", column,
36+
table, condition);
3737
for (int i = 0; i < res.num_tuples(); ++i) {
3838
set->emplace(res.get(i, 0));
3939
}

src/pgsql-helper.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,9 @@ void analyze_table(pg_conn_t const &db_connection, std::string const &schema,
8282
bool has_table(pg_conn_t const &db_connection, std::string const &schema,
8383
std::string const &table)
8484
{
85-
auto const sql = "SELECT count(*) FROM pg_tables"
86-
" WHERE schemaname='{}' AND tablename='{}'"_format(
87-
schema.empty() ? "public" : schema, table);
85+
auto const sql = fmt::format("SELECT count(*) FROM pg_tables"
86+
" WHERE schemaname='{}' AND tablename='{}'",
87+
schema.empty() ? "public" : schema, table);
8888
auto const res = db_connection.exec(sql);
8989
char const *const num = res.get_value(0, 0);
9090

src/progress-display.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ static std::string cps_display(std::size_t count, uint64_t elapsed)
3030
double const cps = count_per_second(count, elapsed);
3131

3232
if (cps >= 1000.0) {
33-
return "{:.0f}k/s"_format(cps / 1000);
33+
return fmt::format("{:.0f}k/s", cps / 1000);
3434
}
35-
return "{:.0f}/s"_format(cps);
35+
return fmt::format("{:.0f}/s", cps);
3636
}
3737

3838
void progress_display_t::print_summary() const

0 commit comments

Comments
 (0)