mirror of
https://github.com/SerenityOS/serenity.git
synced 2025-01-22 09:21:57 -05:00
base64: Map input files into memory for reading
We do the same thing with the gzip utility for performance. This reduces the runtime of `./bin/base64 enwik8 >/dev/null` from 0.428s to 0.303s. This reduces the runtime of `./bin/base64 -d enwik8.base64 >/dev/null` from 0.632s to 0.469s. (enwik8 is a 100MB test file from http://mattmahoney.net/dc/enwik8.zip)
This commit is contained in:
parent
81ad6de41b
commit
aad110ec7e
1 changed files with 14 additions and 4 deletions
|
@ -7,6 +7,7 @@
|
|||
#include <AK/Base64.h>
|
||||
#include <LibCore/ArgsParser.h>
|
||||
#include <LibCore/File.h>
|
||||
#include <LibCore/MappedFile.h>
|
||||
#include <LibCore/System.h>
|
||||
#include <LibMain/Main.h>
|
||||
|
||||
|
@ -36,18 +37,27 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
args_parser.add_positional_argument(filepath, "", "file", Core::ArgsParser::Required::No);
|
||||
args_parser.parse(arguments);
|
||||
|
||||
auto file = TRY(Core::File::open_file_or_standard_stream(filepath, Core::File::OpenMode::Read));
|
||||
ByteBuffer buffer = TRY(file->read_until_eof());
|
||||
Variant<Empty, ByteBuffer, NonnullOwnPtr<Core::MappedFile>> buffer_or_file;
|
||||
ReadonlyBytes input_bytes;
|
||||
|
||||
if (filepath.is_empty() || filepath == "-"sv) {
|
||||
auto file = TRY(Core::File::standard_input());
|
||||
buffer_or_file = TRY(file->read_until_eof());
|
||||
input_bytes = buffer_or_file.get<ByteBuffer>();
|
||||
} else if (TRY(Core::System::stat(filepath)).st_size > 0) {
|
||||
buffer_or_file = TRY(Core::MappedFile::map(filepath));
|
||||
input_bytes = buffer_or_file.get<NonnullOwnPtr<Core::MappedFile>>()->bytes();
|
||||
}
|
||||
|
||||
TRY(Core::System::pledge("stdio"));
|
||||
|
||||
if (decode) {
|
||||
auto decoded = TRY(decode_base64(buffer));
|
||||
auto decoded = TRY(decode_base64(input_bytes));
|
||||
out("{}", StringView(decoded.bytes()));
|
||||
return 0;
|
||||
}
|
||||
|
||||
auto encoded = TRY(encode_base64(buffer));
|
||||
auto encoded = TRY(encode_base64(input_bytes));
|
||||
|
||||
if (maybe_column.has_value() && *maybe_column > 0) {
|
||||
print_wrapped_output(*maybe_column, encoded);
|
||||
|
|
Loading…
Reference in a new issue