Skip to content

Commit e4edc6a

Browse files
avoid constructing strings when char* is passed as an argument
1 parent 9c6d149 commit e4edc6a

File tree

3 files changed

+24
-24
lines changed

3 files changed

+24
-24
lines changed

libs/libvtrutil/src/vtr_util.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,18 +72,18 @@ std::vector<std::string> split(std::string_view text, std::string_view delims) {
7272
}
7373

7474
///@brief Returns 'input' with the first instance of 'search' replaced with 'replace'
75-
std::string replace_first(const std::string& input, const std::string& search, const std::string& replace) {
75+
std::string replace_first(std::string_view input, std::string_view search, std::string_view replace) {
7676
auto pos = input.find(search);
7777

7878
std::string output(input, 0, pos);
7979
output += replace;
80-
output += std::string(input, pos + search.size());
80+
output += input.substr(pos + search.size());
8181

8282
return output;
8383
}
8484

8585
///@brief Returns 'input' with all instances of 'search' replaced with 'replace'
86-
std::string replace_all(const std::string& input, const std::string& search, const std::string& replace) {
86+
std::string replace_all(std::string_view input, std::string_view search, std::string_view replace) {
8787
std::string output;
8888

8989
size_t last = 0;
@@ -101,8 +101,8 @@ std::string replace_all(const std::string& input, const std::string& search, con
101101
return output;
102102
}
103103

104-
///@brief Retruns true if str starts with prefix
105-
bool starts_with(const std::string& str, const std::string& prefix) {
104+
///@brief Returns true if str starts with prefix
105+
bool starts_with(const std::string& str, std::string_view prefix) {
106106
return str.find(prefix) == 0;
107107
}
108108

@@ -195,7 +195,7 @@ char* strdup(const char* str) {
195195
* and/or correct 'unexpected' behaviour of the standard c-functions
196196
*/
197197
template<class T>
198-
T atoT(const std::string& value, const std::string& type_name) {
198+
T atoT(const std::string& value, std::string_view type_name) {
199199
//The c version of atof doesn't catch errors.
200200
//
201201
//This version uses stringstream to detect conversion errors
@@ -461,8 +461,8 @@ bool file_exists(const char* filename) {
461461
*
462462
* Returns true if the extension is correct, and false otherwise.
463463
*/
464-
bool check_file_name_extension(const std::string& file_name,
465-
const std::string& file_extension) {
464+
bool check_file_name_extension(std::string_view file_name,
465+
std::string_view file_extension) {
466466
auto ext = std::filesystem::path(file_name).extension();
467467
return ext == file_extension;
468468
}

libs/libvtrutil/src/vtr_util.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ std::vector<std::string> split(const char* text, std::string_view string_view =
1919
std::vector<std::string> split(std::string_view text, std::string_view delims = " \t\n");
2020

2121
///@brief Returns 'input' with the first instance of 'search' replaced with 'replace'
22-
std::string replace_first(const std::string& input, const std::string& search, const std::string& replace);
22+
std::string replace_first(std::string_view input, std::string_view search, std::string_view replace);
2323

2424
///@brief Returns 'input' with all instances of 'search' replaced with 'replace'
25-
std::string replace_all(const std::string& input, const std::string& search, const std::string& replace);
25+
std::string replace_all(std::string_view input, std::string_view search, std::string_view replace);
2626

2727
///@brief Retruns true if str starts with prefix
28-
bool starts_with(const std::string& str, const std::string& prefix);
28+
bool starts_with(const std::string& str, std::string_view prefix);
2929

3030
///@brief Returns a std::string formatted using a printf-style format string
3131
std::string string_fmt(const char* fmt, ...);
@@ -40,13 +40,13 @@ std::string vstring_fmt(const char* fmt, va_list args);
4040
* would return "home/user/my_files/test.blif"
4141
*/
4242
template<typename Iter>
43-
std::string join(Iter begin, Iter end, std::string delim);
43+
std::string join(Iter begin, Iter end, std::string_view delim);
4444

4545
template<typename Container>
46-
std::string join(Container container, std::string delim);
46+
std::string join(Container container, std::string_view delim);
4747

4848
template<typename T>
49-
std::string join(std::initializer_list<T> list, std::string delim);
49+
std::string join(std::initializer_list<T> list, std::string_view delim);
5050

5151
template<typename Container>
5252
void uniquify(Container container);
@@ -70,7 +70,7 @@ double atod(const std::string& value);
7070
*/
7171
int get_file_line_number_of_last_opened_file();
7272
bool file_exists(const char* filename);
73-
bool check_file_name_extension(const std::string& file_name, const std::string& file_extension);
73+
bool check_file_name_extension(std::string_view file_name, std::string_view file_extension);
7474

7575
extern std::string out_file_prefix;
7676

@@ -83,7 +83,7 @@ std::vector<std::string> ReadLineTokens(FILE* InFile, int* LineNum);
8383
* @brief Template join function implementation
8484
*/
8585
template<typename Iter>
86-
std::string join(Iter begin, Iter end, std::string delim) {
86+
std::string join(Iter begin, Iter end, std::string_view delim) {
8787
std::string joined_str;
8888
for (auto iter = begin; iter != end; ++iter) {
8989
joined_str += *iter;
@@ -95,12 +95,12 @@ std::string join(Iter begin, Iter end, std::string delim) {
9595
}
9696

9797
template<typename Container>
98-
std::string join(Container container, std::string delim) {
98+
std::string join(Container container, std::string_view delim) {
9999
return join(std::begin(container), std::end(container), delim);
100100
}
101101

102102
template<typename T>
103-
std::string join(std::initializer_list<T> list, std::string delim) {
103+
std::string join(std::initializer_list<T> list, std::string_view delim) {
104104
return join(list.begin(), list.end(), delim);
105105
}
106106

vpr/src/base/read_blif.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ struct BlifAllocCallback : public blifparse::Callback {
443443
}
444444

445445
private:
446-
const t_model* find_model(std::string name) {
446+
const t_model* find_model(std::string_view name) {
447447
const t_model* arch_model = nullptr;
448448
for (const t_model* arch_models : {user_arch_models_, library_arch_models_}) {
449449
arch_model = arch_models;
@@ -461,12 +461,12 @@ struct BlifAllocCallback : public blifparse::Callback {
461461
}
462462
if (!arch_model) {
463463
vpr_throw(VPR_ERROR_BLIF_F, filename_.c_str(), lineno_, "Failed to find matching architecture model for '%s'\n",
464-
name.c_str());
464+
name.data());
465465
}
466466
return arch_model;
467467
}
468468

469-
const t_model_ports* find_model_port(const t_model* blk_model, std::string port_name) {
469+
const t_model_ports* find_model_port(const t_model* blk_model, const std::string& port_name) {
470470
//We need to handle both single, and multi-bit port names
471471
//
472472
//By convention multi-bit port names have the bit index stored in square brackets
@@ -582,7 +582,7 @@ struct BlifAllocCallback : public blifparse::Callback {
582582
} else {
583583
VTR_ASSERT(blif_model.block_type(blk_id) == AtomBlockType::OUTPAD);
584584

585-
auto raw_output_name = blif_model.block_name(blk_id);
585+
const auto& raw_output_name = blif_model.block_name(blk_id);
586586

587587
std::string output_name = vtr::replace_first(raw_output_name, OUTPAD_NAME_PREFIX, "");
588588

@@ -618,7 +618,7 @@ struct BlifAllocCallback : public blifparse::Callback {
618618
* @brief Merges all the recorded net pairs which need to be merged
619619
*
620620
* This should only be called at the end of a .model to ensure that
621-
* all the associated driver/sink pins have been delcared and connected
621+
* all the associated driver/sink pins have been declared and connected
622622
* to their nets
623623
*/
624624
void merge_conn_nets() {
@@ -668,7 +668,7 @@ vtr::LogicValue to_vtr_logic_value(blifparse::LogicValue val) {
668668
new_val = vtr::LogicValue::UNKOWN;
669669
break;
670670
default:
671-
VTR_ASSERT_OPT_MSG(false, "Unkown logic value");
671+
VTR_ASSERT_OPT_MSG(false, "Unknown logic value");
672672
}
673673
return new_val;
674674
}

0 commit comments

Comments
 (0)