From db36ddc7630ef57d9e546610d4de6f95330c4d92 Mon Sep 17 00:00:00 2001 From: Aziz Berkay Yesilyurt Date: Sun, 11 Jul 2021 19:00:32 +0200 Subject: LibGfx: Prevent a copy in PNGWriter by storing type data at start --- Userland/Libraries/LibGfx/PNGWriter.cpp | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) (limited to 'Userland/Libraries/LibGfx') diff --git a/Userland/Libraries/LibGfx/PNGWriter.cpp b/Userland/Libraries/LibGfx/PNGWriter.cpp index 6e5978a24b..e9aa6cf693 100644 --- a/Userland/Libraries/LibGfx/PNGWriter.cpp +++ b/Userland/Libraries/LibGfx/PNGWriter.cpp @@ -28,6 +28,8 @@ public: void add_u8(u8); + void store_type(); + private: template requires(IsUnsigned) void add(T); @@ -56,6 +58,14 @@ private: PNGChunk::PNGChunk(String type) : m_type(move(type)) { + store_type(); +} + +void PNGChunk::store_type() +{ + for (auto character : type()) { + m_data.append(&character, sizeof(character)); + } } template @@ -121,19 +131,12 @@ void NonCompressibleBlock::update_adler(u8 data) void PNGWriter::add_chunk(PNGChunk const& png_chunk) { - ByteBuffer combined; - for (auto character : png_chunk.type()) { - combined.append(&character, sizeof(character)); - } - - combined.append(png_chunk.data().data(), png_chunk.data().size()); - - auto crc = BigEndian(Crypto::Checksum::CRC32({ (const u8*)combined.data(), combined.size() }).digest()); - auto data_len = BigEndian(png_chunk.data().size()); + auto crc = BigEndian(Crypto::Checksum::CRC32({ (const u8*)png_chunk.data().data(), png_chunk.data().size() }).digest()); + auto data_len = BigEndian(png_chunk.data().size() - png_chunk.type().length()); ByteBuffer buf; buf.append(&data_len, sizeof(u32)); - buf.append(combined.data(), combined.size()); + buf.append(png_chunk.data().data(), png_chunk.data().size()); buf.append(&crc, sizeof(u32)); m_data.append(buf.data(), buf.size()); -- cgit v1.2.3