From 22f6f0fc9e51b3285ed7741d2c78f1d987ad4e42 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sun, 13 Feb 2022 12:27:07 +0100 Subject: [PATCH] AK: Don't call memcpy() in ByteBuffer::append(u8) We can emit way nicer code for the just-append-a-single-byte case. --- AK/ByteBuffer.h | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/AK/ByteBuffer.h b/AK/ByteBuffer.h index 91617d1bfb..d68a21b396 100644 --- a/AK/ByteBuffer.h +++ b/AK/ByteBuffer.h @@ -192,7 +192,7 @@ public: return MUST(get_bytes_for_writing(length)); } - void append(char byte) + void append(u8 byte) { MUST(try_append(byte)); } @@ -204,9 +204,14 @@ public: void append(void const* data, size_t data_size) { append({ data, data_size }); } - ErrorOr try_append(char byte) + ErrorOr try_append(u8 byte) { - return try_append(&byte, 1); + auto old_size = size(); + auto new_size = old_size + 1; + VERIFY(new_size > old_size); + TRY(try_resize(new_size)); + data()[old_size] = byte; + return {}; } ErrorOr try_append(ReadonlyBytes bytes)