From 1642620a7d9fef7c1dca3bb0d7b172debe3b3d4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erik=20Spr=C3=A5ng?= Date: Thu, 16 Jun 2016 15:54:44 +0200 Subject: [PATCH] Performance fix for H264 RBSP parsing. An rtc::Buffer is used to write output of RBSP parsing, usually one byte at a time. It turns out that container will then expand its capacity one byte at a time, for each byte reallocating the whole buffer and copying the contents over, turning this into an O(n^2) operation. Fix is for now only to preallocate the container storage. Longer term, I think we should mull over if we really need custom containers... R=pbos@webrtc.org TBR=mflodman@webrtc.org BUG= Review URL: https://codereview.webrtc.org/2073763002 . Cr-Commit-Position: refs/heads/master@{#13167} --- webrtc/base/buffer.h | 1 + webrtc/common_video/h264/h264_common.cc | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/webrtc/base/buffer.h b/webrtc/base/buffer.h index 2bb241d9e5..cdaab544a7 100644 --- a/webrtc/base/buffer.h +++ b/webrtc/base/buffer.h @@ -11,6 +11,7 @@ #ifndef WEBRTC_BASE_BUFFER_H_ #define WEBRTC_BASE_BUFFER_H_ +#include #include #include #include diff --git a/webrtc/common_video/h264/h264_common.cc b/webrtc/common_video/h264/h264_common.cc index efdaded1e0..c17b118ce0 100644 --- a/webrtc/common_video/h264/h264_common.cc +++ b/webrtc/common_video/h264/h264_common.cc @@ -59,7 +59,7 @@ NaluType ParseNaluType(uint8_t data) { } std::unique_ptr ParseRbsp(const uint8_t* data, size_t length) { - std::unique_ptr rbsp_buffer(new rtc::Buffer()); + std::unique_ptr rbsp_buffer(new rtc::Buffer(0, length)); const char* sps_bytes = reinterpret_cast(data); for (size_t i = 0; i < length;) { // Be careful about over/underflow here. byte_length_ - 3 can underflow, and @@ -84,6 +84,7 @@ void WriteRbsp(const uint8_t* bytes, size_t length, rtc::Buffer* destination) { static const uint8_t kZerosInStartSequence = 2; static const uint8_t kEmulationByte = 0x03u; size_t num_consecutive_zeros = 0; + destination->EnsureCapacity(destination->size() + length); for (size_t i = 0; i < length; ++i) { uint8_t byte = bytes[i];