Skip to content

Commit abd69a9

Browse files
committed
Strings: Avoid unnecessary std::string allocation churn in sorters
1 parent 63345b2 commit abd69a9

File tree

5 files changed

+37
-23
lines changed

5 files changed

+37
-23
lines changed

src/build_vehicle_gui.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -275,18 +275,20 @@ static EngineID _last_engine[2] = { INVALID_ENGINE, INVALID_ENGINE };
275275
*/
276276
static bool EngineNameSorter(const GUIEngineListItem &a, const GUIEngineListItem &b, const GUIEngineListSortCache &cache)
277277
{
278-
static std::string last_name[2] = { {}, {} };
278+
static format_buffer last_name[2] = { {}, {} };
279279

280280
if (a.engine_id != _last_engine[0]) {
281281
_last_engine[0] = a.engine_id;
282282
SetDParam(0, PackEngineNameDParam(a.engine_id, EngineNameContext::PurchaseList));
283-
last_name[0] = GetString(STR_ENGINE_NAME);
283+
last_name[0].clear();
284+
AppendStringInPlace(last_name[0], STR_ENGINE_NAME);
284285
}
285286

286287
if (b.engine_id != _last_engine[1]) {
287288
_last_engine[1] = b.engine_id;
288289
SetDParam(0, PackEngineNameDParam(b.engine_id, EngineNameContext::PurchaseList));
289-
last_name[1] = GetString(STR_ENGINE_NAME);
290+
last_name[1].clear();
291+
AppendStringInPlace(last_name[1], STR_ENGINE_NAME);
290292
}
291293

292294
int r = StrNaturalCompare(last_name[0], last_name[1]); // Sort by name (natural sorting).

src/cargotype.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include "stdafx.h"
1111
#include "cargotype.h"
12+
#include "core/format.hpp"
1213
#include "core/geometry_func.hpp"
1314
#include "gfx_func.h"
1415
#include "newgrf_cargo.h"
@@ -199,8 +200,10 @@ std::span<const CargoSpec *> _sorted_standard_cargo_specs; ///< Standard cargo s
199200
/** Sort cargo specifications by their name. */
200201
static bool CargoSpecNameSorter(const CargoSpec * const &a, const CargoSpec * const &b)
201202
{
202-
std::string a_name = GetString(a->name);
203-
std::string b_name = GetString(b->name);
203+
format_buffer a_name;
204+
format_buffer b_name;
205+
AppendStringInPlace(a_name, a->name);
206+
AppendStringInPlace(b_name, b->name);
204207

205208
int res = StrNaturalCompare(a_name, b_name); // Sort by name (natural sorting).
206209

src/core/format.hpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,9 @@ struct format_buffer_base : public format_to_buffer {
226226
* Includes convenience wrappers to access the buffer.
227227
* Can be used as a fmt argument.
228228
*/
229-
struct format_buffer final : public format_buffer_base<fmt::inline_buffer_size> {};
229+
struct format_buffer final : public format_buffer_base<fmt::inline_buffer_size> {
230+
format_buffer() {}
231+
};
230232

231233
/**
232234
* format_to_buffer subtype where the fmt::memory_buffer is built-in.
@@ -236,7 +238,9 @@ struct format_buffer final : public format_buffer_base<fmt::inline_buffer_size>
236238
* Can be used as a fmt argument.
237239
*/
238240
template <size_t SIZE>
239-
struct format_buffer_sized final : public format_buffer_base<SIZE> {};
241+
struct format_buffer_sized final : public format_buffer_base<SIZE> {
242+
format_buffer_sized() {}
243+
};
240244

241245
template <typename T>
242246
struct format_by_string_view_cast : public fmt::formatter<std::string_view> {

src/group_gui.cpp

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -172,18 +172,19 @@ void BuildGuiGroupList(GUIGroupList &dst, bool fold, Owner owner, VehicleType ve
172172
list.ForceResort();
173173

174174
/* Sort the groups by their name */
175-
std::array<std::pair<const Group *, std::string>, 2> last_group{};
175+
std::array<std::pair<const Group *, format_buffer>, 2> last_group{};
176176

177177
list.Sort([&last_group](const GUIGroupListItem &a, const GUIGroupListItem &b) -> bool {
178-
if (a.group != last_group[0].first) {
179-
SetDParam(0, a.group->index);
180-
last_group[0] = {a.group, GetString(STR_GROUP_NAME)};
181-
}
182-
183-
if (b.group != last_group[1].first) {
184-
SetDParam(0, b.group->index);
185-
last_group[1] = {b.group, GetString(STR_GROUP_NAME)};
186-
}
178+
auto process_group = [&](size_t index, const Group *group) {
179+
if (group != last_group[index].first) {
180+
last_group[index].first = group;
181+
last_group[index].second.clear();
182+
SetDParam(0, group->index);
183+
AppendStringInPlace(last_group[index].second, STR_GROUP_NAME);
184+
}
185+
};
186+
process_group(0, a.group);
187+
process_group(1, b.group);
187188

188189
int r = StrNaturalCompare(last_group[0].second, last_group[1].second); // Sort by name (natural sorting).
189190
if (r == 0) return a.group->number < b.group->number;
@@ -197,18 +198,20 @@ void SortGUIGroupOnlyList(GUIGroupOnlyList &list)
197198
{
198199
/* Sort the groups by their name */
199200
const Group *last_group[2] = { nullptr, nullptr };
200-
std::string last_name[2] = { {}, {} };
201+
format_buffer last_name[2] = { {}, {} };
201202
list.Sort([&](const Group * const &a, const Group * const &b) {
202203
if (a != last_group[0]) {
203204
last_group[0] = a;
204205
SetDParam(0, a->index | GROUP_NAME_HIERARCHY);
205-
last_name[0] = GetString(STR_GROUP_NAME);
206+
last_name[0].clear();
207+
AppendStringInPlace(last_name[0], STR_GROUP_NAME);
206208
}
207209

208210
if (b != last_group[1]) {
209211
last_group[1] = b;
210212
SetDParam(0, b->index | GROUP_NAME_HIERARCHY);
211-
last_name[1] = GetString(STR_GROUP_NAME);
213+
last_name[1].clear();
214+
AppendStringInPlace(last_name[1], STR_GROUP_NAME);
212215
}
213216

214217
int r = StrNaturalCompare(last_name[0], last_name[1]); // Sort by name (natural sorting).

src/vehicle_gui.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1624,18 +1624,20 @@ static bool VehicleNumberSorter(const Vehicle * const &a, const Vehicle * const
16241624
/** Sort vehicles by their name */
16251625
static bool VehicleNameSorter(const Vehicle * const &a, const Vehicle * const &b)
16261626
{
1627-
static std::string last_name[2] = { {}, {} };
1627+
static format_buffer last_name[2] = { {}, {} };
16281628

16291629
if (a != _last_vehicle[0]) {
16301630
_last_vehicle[0] = a;
16311631
SetDParam(0, a->index);
1632-
last_name[0] = GetString(STR_VEHICLE_NAME);
1632+
last_name[0].clear();
1633+
AppendStringInPlace(last_name[0], STR_VEHICLE_NAME);
16331634
}
16341635

16351636
if (b != _last_vehicle[1]) {
16361637
_last_vehicle[1] = b;
16371638
SetDParam(0, b->index);
1638-
last_name[1] = GetString(STR_VEHICLE_NAME);
1639+
last_name[1].clear();
1640+
AppendStringInPlace(last_name[1], STR_VEHICLE_NAME);
16391641
}
16401642

16411643
int r = StrNaturalCompare(last_name[0], last_name[1]); // Sort by name (natural sorting).

0 commit comments

Comments
 (0)