diff mbox series

[FFmpeg-devel,17/18] avcodec/pthread_slice: Reuse buffer if possible

Message ID DB6PR0101MB22140252E241AED5029D77F88FBA9@DB6PR0101MB2214.eurprd01.prod.exchangelabs.com
State Accepted
Commit 571e4055dc241f8b4556a27b49691edc02edf10b
Headers show
Series [FFmpeg-devel,v2,01/18] avcodec/pthread_slice: Don't reinitialise initialised mutex | expand

Checks

Context Check Description
yinshiyou/make_loongarch64 success Make finished
yinshiyou/make_fate_loongarch64 success Make fate finished
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished

Commit Message

Andreas Rheinhardt June 30, 2022, 10:29 p.m. UTC
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 libavcodec/pthread_slice.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

Comments

Paul B Mahol July 1, 2022, 11:33 a.m. UTC | #1
On Fri, Jul 1, 2022 at 12:32 AM Andreas Rheinhardt <
andreas.rheinhardt@outlook.com> wrote:

> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
> ---
>  libavcodec/pthread_slice.c | 6 ++++--
>  1 file changed, 4 insertions(+), 2 deletions(-)
>
> diff --git a/libavcodec/pthread_slice.c b/libavcodec/pthread_slice.c
> index 756cc59dbf..a4d31c6f4d 100644
> --- a/libavcodec/pthread_slice.c
> +++ b/libavcodec/pthread_slice.c
> @@ -242,9 +242,11 @@ int ff_slice_thread_allocz_entries(AVCodecContext
> *avctx, int count)
>      if (avctx->active_thread_type & FF_THREAD_SLICE)  {
>          SliceThreadContext *p = avctx->internal->thread_ctx;
>
> -        if (p->entries) {
> -            av_freep(&p->entries);
> +        if (p->entries_count == count) {
> +            memset(p->entries, 0, p->entries_count * sizeof(*p->entries));
> +            return 0;
>          }
> +        av_freep(&p->entries);
>
>          p->entries       = av_calloc(count, sizeof(*p->entries));
>          if (!p->entries) {
> --
> 2.34.1
>
>
LGTM

> _______________________________________________
> 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".
>
Tomas Härdin July 1, 2022, 1:21 p.m. UTC | #2
fre 2022-07-01 klockan 00:29 +0200 skrev Andreas Rheinhardt:
> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
> ---
>  libavcodec/pthread_slice.c | 6 ++++--
>  1 file changed, 4 insertions(+), 2 deletions(-)
> 
> diff --git a/libavcodec/pthread_slice.c b/libavcodec/pthread_slice.c
> index 756cc59dbf..a4d31c6f4d 100644
> --- a/libavcodec/pthread_slice.c
> +++ b/libavcodec/pthread_slice.c
> @@ -242,9 +242,11 @@ int
> ff_slice_thread_allocz_entries(AVCodecContext *avctx, int count)
>      if (avctx->active_thread_type & FF_THREAD_SLICE)  {
>          SliceThreadContext *p = avctx->internal->thread_ctx;
>  
> -        if (p->entries) {
> -            av_freep(&p->entries);
> +        if (p->entries_count == count) {
> +            memset(p->entries, 0, p->entries_count * sizeof(*p-
> >entries));
> +            return 0;

Couldn't this trivially handle p->entries_count < count also?

>          }
> +        av_freep(&p->entries);
>  
>          p->entries       = av_calloc(count, sizeof(*p->entries));
>          if (!p->entries) {

Looks like we could use an av_fast_calloc()

/Tomas
Andreas Rheinhardt July 22, 2022, 8:04 p.m. UTC | #3
Tomas Härdin:
> fre 2022-07-01 klockan 00:29 +0200 skrev Andreas Rheinhardt:
>> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
>> ---
>>  libavcodec/pthread_slice.c | 6 ++++--
>>  1 file changed, 4 insertions(+), 2 deletions(-)
>>
>> diff --git a/libavcodec/pthread_slice.c b/libavcodec/pthread_slice.c
>> index 756cc59dbf..a4d31c6f4d 100644
>> --- a/libavcodec/pthread_slice.c
>> +++ b/libavcodec/pthread_slice.c
>> @@ -242,9 +242,11 @@ int
>> ff_slice_thread_allocz_entries(AVCodecContext *avctx, int count)
>>      if (avctx->active_thread_type & FF_THREAD_SLICE)  {
>>          SliceThreadContext *p = avctx->internal->thread_ctx;
>>  
>> -        if (p->entries) {
>> -            av_freep(&p->entries);
>> +        if (p->entries_count == count) {
>> +            memset(p->entries, 0, p->entries_count * sizeof(*p-
>>> entries));
>> +            return 0;
> 
> Couldn't this trivially handle p->entries_count < count also?
> 

Yes, but then this would basically be yet another never-shrinking
buffer. And with every such never-shrinking buffer I fear that we might
allocate too much forever (e.g. because of a single invalid packet).

>>          }
>> +        av_freep(&p->entries);
>>  
>>          p->entries       = av_calloc(count, sizeof(*p->entries));
>>          if (!p->entries) {
> 
> Looks like we could use an av_fast_calloc()
> 
> /Tomas
>
diff mbox series

Patch

diff --git a/libavcodec/pthread_slice.c b/libavcodec/pthread_slice.c
index 756cc59dbf..a4d31c6f4d 100644
--- a/libavcodec/pthread_slice.c
+++ b/libavcodec/pthread_slice.c
@@ -242,9 +242,11 @@  int ff_slice_thread_allocz_entries(AVCodecContext *avctx, int count)
     if (avctx->active_thread_type & FF_THREAD_SLICE)  {
         SliceThreadContext *p = avctx->internal->thread_ctx;
 
-        if (p->entries) {
-            av_freep(&p->entries);
+        if (p->entries_count == count) {
+            memset(p->entries, 0, p->entries_count * sizeof(*p->entries));
+            return 0;
         }
+        av_freep(&p->entries);
 
         p->entries       = av_calloc(count, sizeof(*p->entries));
         if (!p->entries) {