From: Andreas Rheinhardt <andreas.rheinhardt@outlook.com> To: ffmpeg-devel@ffmpeg.org Cc: Andreas Rheinhardt <andreas.rheinhardt@outlook.com> Subject: [FFmpeg-devel] [PATCH 8/8] avutil/fifo: Grow FIFO faster when growing automatically Date: Tue, 5 Jul 2022 22:26:50 +0200 Message-ID: <DB6PR0101MB221436BE93DAF8B1A44449198F819@DB6PR0101MB2214.eurprd01.prod.exchangelabs.com> (raw) In-Reply-To: <DB6PR0101MB2214034DCAA45C10037497958F819@DB6PR0101MB2214.eurprd01.prod.exchangelabs.com> Up until now, when the FIFO is grown automatically, it would be resized by double the amount needed (if possible, i.e. if compatible with the auto-grow limit). This implies that if e.g. the user always writes a single element to the FIFO, the FIFO will be reallocated once for every two writes (presuming no reads happen inbetween). This is potentially quadratic (depending upon the realloc implementation). This commit changes this by using av_fast_realloc_array to realloc the buffer. Its ability to not overallocate beyond a given size allows to honour the user-specified auto-grow limit. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com> --- libavutil/fifo.c | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/libavutil/fifo.c b/libavutil/fifo.c index 53359a2112..04f4d057ca 100644 --- a/libavutil/fifo.c +++ b/libavutil/fifo.c @@ -96,6 +96,19 @@ size_t av_fifo_can_write(const AVFifo *f) return f->nb_elems - av_fifo_can_read(f); } +static void fifo_readjust_after_growing(AVFifo *f, size_t old_size) +{ + const size_t inc = f->nb_elems - old_size; + // move the data from the end of the ring buffer + // to the end of the newly allocated space + if (f->offset_w <= f->offset_r && !f->is_empty) { + memmove(f->buffer + (f->offset_r + inc) * f->elem_size, + f->buffer + f->offset_r * f->elem_size, + (old_size - f->offset_r) * f->elem_size); + f->offset_r += inc; + } +} + int av_fifo_grow2(AVFifo *f, size_t inc) { uint8_t *tmp; @@ -107,16 +120,8 @@ int av_fifo_grow2(AVFifo *f, size_t inc) if (!tmp) return AVERROR(ENOMEM); f->buffer = tmp; - - // move the data from the end of the ring buffer - // to the end of the newly allocated space - if (f->offset_w <= f->offset_r && !f->is_empty) { - memmove(tmp + (f->offset_r + inc) * f->elem_size, tmp + f->offset_r * f->elem_size, - (f->nb_elems - f->offset_r) * f->elem_size); - f->offset_r += inc; - } - f->nb_elems += inc; + fifo_readjust_after_growing(f, f->nb_elems - inc); return 0; } @@ -133,9 +138,15 @@ static int fifo_check_space(AVFifo *f, size_t to_write) can_grow = f->auto_grow_limit > f->nb_elems ? f->auto_grow_limit - f->nb_elems : 0; if ((f->flags & AV_FIFO_FLAG_AUTO_GROW) && need_grow <= can_grow) { - // allocate a bit more than necessary, if we can - const size_t inc = (need_grow < can_grow / 2 ) ? need_grow * 2 : can_grow; - return av_fifo_grow2(f, inc); + // Use av_fast_realloc_array() to allocate in a fast way + // while respecting the auto_grow_limit + const size_t old_size = f->nb_elems; + int ret = av_fast_realloc_array(&f->buffer, &f->nb_elems, f->nb_elems + need_grow, + f->auto_grow_limit, f->elem_size); + if (ret < 0) + return ret; + fifo_readjust_after_growing(f, old_size); + return 0; } return AVERROR(ENOSPC); -- 2.34.1 _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
next prev parent reply other threads:[~2022-07-05 20:29 UTC|newest] Thread overview: 38+ messages / expand[flat|nested] mbox.gz Atom feed top 2022-07-05 20:09 [FFmpeg-devel] [PATCH 1/8] avutil/mem: Handle fast allocations near UINT_MAX properly Andreas Rheinhardt 2022-07-05 20:26 ` [FFmpeg-devel] [PATCH 2/8] avformat/flvenc: Add deinit function Andreas Rheinhardt 2022-07-06 2:28 ` Steven Liu 2022-07-05 20:26 ` [FFmpeg-devel] [PATCH 3/8] avutil/mem: Add av_fast_realloc_array() Andreas Rheinhardt 2022-07-06 14:40 ` Tomas Härdin 2022-07-06 14:46 ` Andreas Rheinhardt 2022-07-06 14:54 ` Tomas Härdin 2022-07-12 14:12 ` Andreas Rheinhardt 2022-07-14 8:14 ` Anton Khirnov 2022-07-14 12:51 ` Andreas Rheinhardt 2022-07-17 8:30 ` Anton Khirnov 2022-09-26 12:25 ` Andreas Rheinhardt 2022-09-26 14:21 ` Andreas Rheinhardt 2022-09-26 14:24 ` Tomas Härdin 2022-09-27 15:23 ` Tomas Härdin 2022-09-28 9:35 ` Tomas Härdin 2022-09-28 11:06 ` Andreas Rheinhardt 2022-09-28 11:41 ` Tomas Härdin 2022-07-21 21:23 ` Tomas Härdin 2022-08-17 15:29 ` Anton Khirnov 2022-07-05 20:26 ` [FFmpeg-devel] [PATCH 4/8] avformat/flvenc: Use array instead of linked list for index Andreas Rheinhardt 2022-07-06 14:58 ` Tomas Härdin 2022-07-06 15:03 ` Andreas Rheinhardt 2022-07-05 20:26 ` [FFmpeg-devel] [PATCH 5/8] avformat/matroskaenc: Use av_fast_realloc_array for index entries Andreas Rheinhardt 2022-07-06 15:03 ` Tomas Härdin 2022-07-06 15:10 ` Andreas Rheinhardt 2022-07-06 15:21 ` Tomas Härdin 2022-07-05 20:26 ` [FFmpeg-devel] [PATCH 6/8] avcodec/movtextenc: Use av_fast_realloc_array Andreas Rheinhardt 2022-07-06 15:06 ` Tomas Härdin 2022-07-05 20:26 ` [FFmpeg-devel] [PATCH 7/8] avutil/fifo: Simplify growing FIFO Andreas Rheinhardt 2022-07-05 20:26 ` Andreas Rheinhardt [this message] 2022-07-06 13:02 ` [FFmpeg-devel] [PATCH 1/8] avutil/mem: Handle fast allocations near UINT_MAX properly Anton Khirnov 2022-07-06 13:08 ` Andreas Rheinhardt 2022-07-06 13:17 ` Anton Khirnov 2022-07-06 14:24 ` Tomas Härdin 2022-07-06 14:40 ` Andreas Rheinhardt 2022-08-17 14:31 ` Tomas Härdin 2022-09-26 11:50 ` Tomas Härdin
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=DB6PR0101MB221436BE93DAF8B1A44449198F819@DB6PR0101MB2214.eurprd01.prod.exchangelabs.com \ --to=andreas.rheinhardt@outlook.com \ --cc=ffmpeg-devel@ffmpeg.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: link
Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel This inbox may be cloned and mirrored by anyone: git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git # If you have public-inbox 1.1+ installed, you may # initialize and index your mirror using the following commands: public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \ ffmpegdev@gitmailbox.com public-inbox-index ffmpegdev Example config snippet for mirrors. AGPL code for this site: git clone https://public-inbox.org/public-inbox.git