mirror of
https://github.com/ml-explore/mlx.git
synced 2025-12-16 01:49:05 +08:00
Remove unnecessary string copies (#891)
1. Use string_view instead of string when there is no need for copy. 2. Otherwise move string when possible.
This commit is contained in:
@@ -114,16 +114,13 @@ void save(std::shared_ptr<io::Writer> out_stream, array a) {
|
||||
}
|
||||
|
||||
/** Save array to file in .npy format */
|
||||
void save(const std::string& file_, array a) {
|
||||
// Open and check file
|
||||
std::string file = file_;
|
||||
|
||||
void save(std::string file, array a) {
|
||||
// Add .npy to file name if it is not there
|
||||
if (file.length() < 4 || file.substr(file.length() - 4, 4) != ".npy")
|
||||
file += ".npy";
|
||||
|
||||
// Serialize array
|
||||
save(std::make_shared<io::FileWriter>(file), a);
|
||||
save(std::make_shared<io::FileWriter>(std::move(file)), a);
|
||||
}
|
||||
|
||||
/** Load array from reader in .npy format */
|
||||
@@ -227,8 +224,8 @@ array load(std::shared_ptr<io::Reader> in_stream, StreamOrDevice s) {
|
||||
}
|
||||
|
||||
/** Load array from file in .npy format */
|
||||
array load(const std::string& file, StreamOrDevice s) {
|
||||
return load(std::make_shared<io::FileReader>(file), s);
|
||||
array load(std::string file, StreamOrDevice s) {
|
||||
return load(std::make_shared<io::FileReader>(std::move(file)), s);
|
||||
}
|
||||
|
||||
} // namespace mlx::core
|
||||
|
||||
Reference in New Issue
Block a user