From 6fb5754c5b5198e4e825d4bf91683ba080eee8d3 Mon Sep 17 00:00:00 2001 From: Rui Ueyama Date: Tue, 4 May 2021 14:41:32 +0900 Subject: [PATCH] Use u8[] instead of std::vector to avoid zero-initialization cost --- main.cc | 10 +++++----- mold.h | 2 +- object_file.cc | 8 ++++---- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/main.cc b/main.cc index e22250d5..7ec5a66d 100644 --- a/main.cc +++ b/main.cc @@ -20,11 +20,11 @@ static bool is_text_file(Context &ctx, MemoryMappedFile *mb) { template std::string_view save_string(Context &ctx, const std::string &str) { - std::vector *buf = new std::vector(str.size() + 1); - memcpy(buf->data(), str.data(), str.size()); - (*buf)[str.size()] = '\0'; - ctx.owning_bufs.push_back(std::unique_ptr>(buf)); - return {(char *)buf->data(), str.size()}; + u8 *buf = new u8[str.size() + 1]; + memcpy(buf, str.data(), str.size()); + buf[str.size()] = '\0'; + ctx.owning_bufs.push_back(std::unique_ptr(buf)); + return {(char *)buf, str.size()}; } std::string get_version_string() { diff --git a/mold.h b/mold.h index 82939fde..4a34f57f 100644 --- a/mold.h +++ b/mold.h @@ -1434,7 +1434,7 @@ struct Context { tbb::concurrent_vector>> owning_objs; tbb::concurrent_vector>> owning_dsos; - tbb::concurrent_vector>> owning_bufs; + tbb::concurrent_vector> owning_bufs; tbb::concurrent_vector>> owning_shdrs; tbb::concurrent_vector>> owning_mbs; diff --git a/object_file.cc b/object_file.cc index 5e3bf99a..0ae17792 100644 --- a/object_file.cc +++ b/object_file.cc @@ -193,15 +193,15 @@ std::pair *> ObjectFile::uncompress_contents(Context &ctx, const ElfShdr &shdr, std::string_view name) { auto do_uncompress = [&](std::string_view data, u64 size) { - std::vector *buf = new std::vector(size); - ctx.owning_bufs.push_back(std::unique_ptr>(buf)); + u8 *buf = new u8[size]; + ctx.owning_bufs.push_back(std::unique_ptr(buf)); unsigned long size2 = size; - if (uncompress(buf->data(), &size2, (u8 *)&data[0], data.size()) != Z_OK) + if (uncompress(buf, &size2, (u8 *)&data[0], data.size()) != Z_OK) Fatal(ctx) << *this << ": " << name << ": uncompress failed"; if (size != size2) Fatal(ctx) << *this << ": " << name << ": uncompress: invalid size"; - return std::string_view((char *)buf->data(), size); + return std::string_view((char *)buf, size); }; if (name.starts_with(".zdebug")) {