From 6ad83ec81170e4901bae9ab2d908acab5c2a9a48 Mon Sep 17 00:00:00 2001 From: eidheim Date: Mon, 18 May 2020 09:28:56 +0200 Subject: [PATCH] Slightly optimised file reading and writing --- src/filesystem.cc | 18 +++++++++++------- src/source.cc | 10 +++------- src/source_base.cc | 20 ++++++++++++++------ src/source_language_protocol.cc | 6 ++---- 4 files changed, 30 insertions(+), 24 deletions(-) diff --git a/src/filesystem.cc b/src/filesystem.cc index 04d4a83..2ddee9c 100644 --- a/src/filesystem.cc +++ b/src/filesystem.cc @@ -7,13 +7,17 @@ //Only use on small files std::string filesystem::read(const std::string &path) { - std::stringstream ss; + std::string str; std::ifstream input(path, std::ofstream::binary); if(input) { - ss << input.rdbuf(); + input.seekg(0, std::ios::end); + auto size = input.tellg(); + input.seekg(0, std::ios::beg); + str.reserve(size); + str.assign(std::istreambuf_iterator(input), std::istreambuf_iterator()); input.close(); } - return ss.str(); + return str; } //Only use on small files @@ -21,11 +25,11 @@ std::vector filesystem::read_lines(const std::string &path) { std::vector res; std::ifstream input(path, std::ofstream::binary); if(input) { - do { - res.emplace_back(); - } while(getline(input, res.back())); + std::string line; + while(std::getline(input, line)) + res.emplace_back(std::move(line)); + input.close(); } - input.close(); return res; } diff --git a/src/source.cc b/src/source.cc index b9dda87..07beaf9 100644 --- a/src/source.cc +++ b/src/source.cc @@ -403,13 +403,9 @@ bool Source::View::save() { auto end_iter = start_iter; bool end_reached = false; while(!end_reached) { - for(size_t c = 0; c < 131072; c++) { - if(!end_iter.forward_char()) { - end_reached = true; - break; - } - } - output << get_buffer()->get_text(start_iter, end_iter).c_str(); + if(!end_iter.forward_chars(131072)) + end_reached = true; + output << get_buffer()->get_text(start_iter, end_iter).raw(); start_iter = end_iter; } output.close(); diff --git a/src/source_base.cc b/src/source_base.cc index d6ca412..2300ac2 100644 --- a/src/source_base.cc +++ b/src/source_base.cc @@ -109,9 +109,13 @@ bool Source::BaseView::load(bool not_undoable_action) { if(language) { std::ifstream input(file_path.string(), std::ofstream::binary); if(input) { - std::stringstream ss; - ss << input.rdbuf(); - Glib::ustring ustr = ss.str(); + std::string str; + input.seekg(0, std::ios::end); + auto size = input.tellg(); + input.seekg(0, std::ios::beg); + str.reserve(size); + str.assign(std::istreambuf_iterator(input), std::istreambuf_iterator()); + Glib::ustring ustr(std::move(str)); bool valid = true; Glib::ustring::iterator iter; @@ -136,9 +140,13 @@ bool Source::BaseView::load(bool not_undoable_action) { else { std::ifstream input(file_path.string(), std::ofstream::binary); if(input) { - std::stringstream ss; - ss << input.rdbuf(); - Glib::ustring ustr = ss.str(); + std::string str; + input.seekg(0, std::ios::end); + auto size = input.tellg(); + input.seekg(0, std::ios::beg); + str.reserve(size); + str.assign(std::istreambuf_iterator(input), std::istreambuf_iterator()); + Glib::ustring ustr(std::move(str)); if(ustr.validate()) { if(get_buffer()->size() == 0) diff --git a/src/source_language_protocol.cc b/src/source_language_protocol.cc index aa5dab5..37c3702 100644 --- a/src/source_language_protocol.cc +++ b/src/source_language_protocol.cc @@ -190,9 +190,7 @@ void LanguageProtocol::Client::parse_server_message() { std::string line; while(!header_read && std::getline(server_message_stream, line)) { - if(!line.empty()) { - if(line.back() == '\r') - line.pop_back(); + if(!line.empty() && line != "\r") { if(line.compare(0, 16, "Content-Length: ") == 0) { try { server_message_size = static_cast(std::stoul(line.substr(16))); @@ -201,7 +199,7 @@ void LanguageProtocol::Client::parse_server_message() { } } } - if(line.empty() && server_message_size != static_cast(-1)) { + else if(server_message_size != static_cast(-1)) { server_message_content_pos = server_message_stream.tellg(); server_message_size += server_message_content_pos; header_read = true;