mirror of
https://github.com/ml-explore/mlx.git
synced 2025-06-25 01:41:17 +08:00
Do not store iostream in shared_ptr (#872)
There is no need to store iostream in shared_ptr, doing so adds the cost of a heap allocation.
This commit is contained in:
parent
f0ae00da12
commit
9663c22fe9
@ -14,7 +14,7 @@ class Reader {
|
|||||||
public:
|
public:
|
||||||
virtual bool is_open() const = 0;
|
virtual bool is_open() const = 0;
|
||||||
virtual bool good() const = 0;
|
virtual bool good() const = 0;
|
||||||
virtual size_t tell() const = 0;
|
virtual size_t tell() = 0; // tellp is non-const in iostream
|
||||||
virtual void seek(
|
virtual void seek(
|
||||||
int64_t off,
|
int64_t off,
|
||||||
std::ios_base::seekdir way = std::ios_base::beg) = 0;
|
std::ios_base::seekdir way = std::ios_base::beg) = 0;
|
||||||
@ -26,7 +26,7 @@ class Writer {
|
|||||||
public:
|
public:
|
||||||
virtual bool is_open() const = 0;
|
virtual bool is_open() const = 0;
|
||||||
virtual bool good() const = 0;
|
virtual bool good() const = 0;
|
||||||
virtual size_t tell() const = 0;
|
virtual size_t tell() = 0;
|
||||||
virtual void seek(
|
virtual void seek(
|
||||||
int64_t off,
|
int64_t off,
|
||||||
std::ios_base::seekdir way = std::ios_base::beg) = 0;
|
std::ios_base::seekdir way = std::ios_base::beg) = 0;
|
||||||
@ -36,31 +36,31 @@ class Writer {
|
|||||||
|
|
||||||
class FileReader : public Reader {
|
class FileReader : public Reader {
|
||||||
public:
|
public:
|
||||||
explicit FileReader(const std::shared_ptr<std::ifstream>& is)
|
explicit FileReader(std::ifstream is)
|
||||||
: is_(is), label_("stream") {}
|
: is_(std::move(is)), label_("stream") {}
|
||||||
explicit FileReader(const std::string& file_path)
|
explicit FileReader(std::string file_path)
|
||||||
: is_(std::make_shared<std::ifstream>(file_path, std::ios::binary)),
|
: is_(std::ifstream(file_path, std::ios::binary)),
|
||||||
label_(file_path) {}
|
label_(std::move(file_path)) {}
|
||||||
|
|
||||||
bool is_open() const override {
|
bool is_open() const override {
|
||||||
return is_->is_open();
|
return is_.is_open();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool good() const override {
|
bool good() const override {
|
||||||
return is_->good();
|
return is_.good();
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t tell() const override {
|
size_t tell() override {
|
||||||
return is_->tellg();
|
return is_.tellg();
|
||||||
}
|
}
|
||||||
|
|
||||||
void seek(int64_t off, std::ios_base::seekdir way = std::ios_base::beg)
|
void seek(int64_t off, std::ios_base::seekdir way = std::ios_base::beg)
|
||||||
override {
|
override {
|
||||||
is_->seekg(off, way);
|
is_.seekg(off, way);
|
||||||
}
|
}
|
||||||
|
|
||||||
void read(char* data, size_t n) override {
|
void read(char* data, size_t n) override {
|
||||||
is_->read(data, n);
|
is_.read(data, n);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string label() const override {
|
std::string label() const override {
|
||||||
@ -68,37 +68,37 @@ class FileReader : public Reader {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::shared_ptr<std::ifstream> is_;
|
std::ifstream is_;
|
||||||
std::string label_;
|
std::string label_;
|
||||||
};
|
};
|
||||||
|
|
||||||
class FileWriter : public Writer {
|
class FileWriter : public Writer {
|
||||||
public:
|
public:
|
||||||
explicit FileWriter(const std::shared_ptr<std::ofstream>& is)
|
explicit FileWriter(std::ofstream os)
|
||||||
: os_(is), label_("stream") {}
|
: os_(std::move(os)), label_("stream") {}
|
||||||
explicit FileWriter(const std::string& file_path)
|
explicit FileWriter(std::string file_path)
|
||||||
: os_(std::make_shared<std::ofstream>(file_path, std::ios::binary)),
|
: os_(std::ofstream(file_path, std::ios::binary)),
|
||||||
label_(file_path) {}
|
label_(std::move(file_path)) {}
|
||||||
|
|
||||||
bool is_open() const override {
|
bool is_open() const override {
|
||||||
return os_->is_open();
|
return os_.is_open();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool good() const override {
|
bool good() const override {
|
||||||
return os_->good();
|
return os_.good();
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t tell() const override {
|
size_t tell() override {
|
||||||
return os_->tellp();
|
return os_.tellp();
|
||||||
}
|
}
|
||||||
|
|
||||||
void seek(int64_t off, std::ios_base::seekdir way = std::ios_base::beg)
|
void seek(int64_t off, std::ios_base::seekdir way = std::ios_base::beg)
|
||||||
override {
|
override {
|
||||||
os_->seekp(off, way);
|
os_.seekp(off, way);
|
||||||
}
|
}
|
||||||
|
|
||||||
void write(const char* data, size_t n) override {
|
void write(const char* data, size_t n) override {
|
||||||
os_->write(data, n);
|
os_.write(data, n);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string label() const override {
|
std::string label() const override {
|
||||||
@ -106,7 +106,7 @@ class FileWriter : public Writer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::shared_ptr<std::ofstream> os_;
|
std::ofstream os_;
|
||||||
std::string label_;
|
std::string label_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -121,7 +121,7 @@ class PyFileReader : public io::Reader {
|
|||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t tell() const override {
|
size_t tell() override {
|
||||||
size_t out;
|
size_t out;
|
||||||
{
|
{
|
||||||
nb::gil_scoped_acquire gil;
|
nb::gil_scoped_acquire gil;
|
||||||
@ -334,7 +334,7 @@ class PyFileWriter : public io::Writer {
|
|||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t tell() const override {
|
size_t tell() override {
|
||||||
size_t out;
|
size_t out;
|
||||||
{
|
{
|
||||||
nb::gil_scoped_acquire gil;
|
nb::gil_scoped_acquire gil;
|
||||||
|
Loading…
Reference in New Issue
Block a user