mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-24 08:52:18 +01:00
Add a custom gif_mimic_DGifSlurp function.
This avoids some malloc's of intermediate buffers when using giflib, i.e. for mimic benchmarks. The numbers don't show a huge difference: name old speed new speed delta gif_mimic_decode_1k/clang 158MB/s ± 0% 158MB/s ± 2% ~ (p=0.421 n=5+5) gif_mimic_decode_10k/clang 94.5MB/s ± 0% 93.8MB/s ± 1% -0.70% (p=0.016 n=5+5) gif_mimic_decode_100k/clang 100MB/s ± 0% 100MB/s ± 0% -0.42% (p=0.008 n=5+5) gif_mimic_decode_1000k/clang 102MB/s ± 0% 102MB/s ± 0% -0.29% (p=0.016 n=5+5) gif_mimic_decode_1k/gcc 158MB/s ± 1% 160MB/s ± 0% +1.34% (p=0.008 n=5+5) gif_mimic_decode_10k/gcc 94.3MB/s ± 0% 94.6MB/s ± 0% +0.36% (p=0.008 n=5+5) gif_mimic_decode_100k/gcc 100MB/s ± 0% 101MB/s ± 0% +0.46% (p=0.008 n=5+5) gif_mimic_decode_1000k/gcc 102MB/s ± 0% 103MB/s ± 0% +0.70% (p=0.008 n=5+5) This commit will immediately be followed by a rollback, but it is committed anyway so that we can refer to these numbers in the git log.
This commit is contained in:
1 parent
c06580a4f9
commit
ab7e0ae795
1 file changed
+103
-12
+103
-12
@@ -1,6 +1,8 @@
|
|||||||
// Use of this source code is governed by a BSD-style license that can be found
|
// Use of this source code is governed by a BSD-style license that can be found
|
||||||
// in the LICENSE file.
|
// in the LICENSE file.
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
#include "gif_lib.h"
|
#include "gif_lib.h"
|
||||||
|
|
||||||
int gif_mimic_read_func(GifFileType* f, GifByteType* ptr, int len) {
|
int gif_mimic_read_func(GifFileType* f, GifByteType* ptr, int len) {
|
||||||
@@ -18,6 +20,100 @@ int gif_mimic_read_func(GifFileType* f, GifByteType* ptr, int len) {
|
|||||||
return n;
|
return n;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// gif_mimic_DGifSlurp is based on the DGifSlurp function in giflib-4.1.6 (the
|
||||||
|
// version in Ubunty 14.04 LTS "Trusty"). It is modified to avoid malloc'ing a
|
||||||
|
// temporary buffer. Instead, the destination buffer is an additional argument.
|
||||||
|
int gif_mimic_DGifSlurp(GifFileType* GifFile, puffs_base_buf1* dst) {
|
||||||
|
int ImageSize;
|
||||||
|
GifRecordType RecordType;
|
||||||
|
SavedImage* sp;
|
||||||
|
GifByteType* ExtData;
|
||||||
|
SavedImage temp_save;
|
||||||
|
|
||||||
|
temp_save.ExtensionBlocks = NULL;
|
||||||
|
temp_save.ExtensionBlockCount = 0;
|
||||||
|
|
||||||
|
do {
|
||||||
|
if (DGifGetRecordType(GifFile, &RecordType) == GIF_ERROR)
|
||||||
|
return (GIF_ERROR);
|
||||||
|
|
||||||
|
switch (RecordType) {
|
||||||
|
case IMAGE_DESC_RECORD_TYPE:
|
||||||
|
if (DGifGetImageDesc(GifFile) == GIF_ERROR)
|
||||||
|
return (GIF_ERROR);
|
||||||
|
|
||||||
|
sp = &GifFile->SavedImages[GifFile->ImageCount - 1];
|
||||||
|
ImageSize = sp->ImageDesc.Width * sp->ImageDesc.Height;
|
||||||
|
|
||||||
|
// Puffs modification: avoid a memmove by decompressing straight into
|
||||||
|
// the destination buffer.
|
||||||
|
//
|
||||||
|
// Original DGifSlurp code:
|
||||||
|
// sp->RasterBits =
|
||||||
|
// (unsigned char*)malloc(ImageSize * sizeof(GifPixelType));
|
||||||
|
size_t num_src =
|
||||||
|
(size_t)(sp->ImageDesc.Width) * (size_t)(sp->ImageDesc.Height);
|
||||||
|
size_t num_dst = dst->len - dst->wi;
|
||||||
|
if (num_dst < num_src) {
|
||||||
|
return (GIF_ERROR);
|
||||||
|
}
|
||||||
|
sp->RasterBits = dst->ptr + dst->wi;
|
||||||
|
dst->wi += num_src;
|
||||||
|
// End Puffs modification.
|
||||||
|
|
||||||
|
if (sp->RasterBits == NULL) {
|
||||||
|
return GIF_ERROR;
|
||||||
|
}
|
||||||
|
if (DGifGetLine(GifFile, sp->RasterBits, ImageSize) == GIF_ERROR)
|
||||||
|
return (GIF_ERROR);
|
||||||
|
if (temp_save.ExtensionBlocks) {
|
||||||
|
sp->ExtensionBlocks = temp_save.ExtensionBlocks;
|
||||||
|
sp->ExtensionBlockCount = temp_save.ExtensionBlockCount;
|
||||||
|
|
||||||
|
temp_save.ExtensionBlocks = NULL;
|
||||||
|
temp_save.ExtensionBlockCount = 0;
|
||||||
|
|
||||||
|
/* FIXME: The following is wrong. It is left in only for
|
||||||
|
* backwards compatibility. Someday it should go away. Use
|
||||||
|
* the sp->ExtensionBlocks->Function variable instead. */
|
||||||
|
sp->Function = sp->ExtensionBlocks[0].Function;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case EXTENSION_RECORD_TYPE:
|
||||||
|
if (DGifGetExtension(GifFile, &temp_save.Function, &ExtData) ==
|
||||||
|
GIF_ERROR)
|
||||||
|
return (GIF_ERROR);
|
||||||
|
while (ExtData != NULL) {
|
||||||
|
/* Create an extension block with our data */
|
||||||
|
if (AddExtensionBlock(&temp_save, ExtData[0], &ExtData[1]) ==
|
||||||
|
GIF_ERROR)
|
||||||
|
return (GIF_ERROR);
|
||||||
|
|
||||||
|
if (DGifGetExtensionNext(GifFile, &ExtData) == GIF_ERROR)
|
||||||
|
return (GIF_ERROR);
|
||||||
|
temp_save.Function = 0;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case TERMINATE_RECORD_TYPE:
|
||||||
|
break;
|
||||||
|
|
||||||
|
default: /* Should be trapped by DGifGetRecordType */
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} while (RecordType != TERMINATE_RECORD_TYPE);
|
||||||
|
|
||||||
|
/* Just in case the Gif has an extension block without an associated
|
||||||
|
* image... (Should we save this into a savefile structure with no image
|
||||||
|
* instead? Have to check if the present writing code can handle that as
|
||||||
|
* well.... */
|
||||||
|
if (temp_save.ExtensionBlocks)
|
||||||
|
FreeExtension(&temp_save);
|
||||||
|
|
||||||
|
return (GIF_OK);
|
||||||
|
}
|
||||||
|
|
||||||
const char* gif_mimic_decode(puffs_base_buf1* dst, puffs_base_buf1* src) {
|
const char* gif_mimic_decode(puffs_base_buf1* dst, puffs_base_buf1* src) {
|
||||||
const char* ret = NULL;
|
const char* ret = NULL;
|
||||||
|
|
||||||
@@ -27,7 +123,8 @@ const char* gif_mimic_decode(puffs_base_buf1* dst, puffs_base_buf1* src) {
|
|||||||
ret = "DGifOpen failed";
|
ret = "DGifOpen failed";
|
||||||
goto cleanup0;
|
goto cleanup0;
|
||||||
}
|
}
|
||||||
if (DGifSlurp(f) != GIF_OK) {
|
|
||||||
|
if (gif_mimic_DGifSlurp(f, dst) != GIF_OK) {
|
||||||
ret = "DGifSlurp failed";
|
ret = "DGifSlurp failed";
|
||||||
goto cleanup1;
|
goto cleanup1;
|
||||||
}
|
}
|
||||||
@@ -38,19 +135,13 @@ const char* gif_mimic_decode(puffs_base_buf1* dst, puffs_base_buf1* src) {
|
|||||||
goto cleanup1;
|
goto cleanup1;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct SavedImage* si = &f->SavedImages[0];
|
cleanup1:;
|
||||||
size_t num_src =
|
// See "Puffs modification" in gif_mimic_DGifSlurp above.
|
||||||
(size_t)(si->ImageDesc.Width) * (size_t)(si->ImageDesc.Height);
|
SavedImage* sp;
|
||||||
size_t num_dst = dst->len - dst->wi;
|
for (sp = f->SavedImages; sp < f->SavedImages + f->ImageCount; sp++) {
|
||||||
if (num_dst < num_src) {
|
sp->RasterBits = NULL;
|
||||||
ret = "GIF image's pixel data won't fit in the dst buffer";
|
|
||||||
goto cleanup1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
memmove(dst->ptr + dst->wi, si->RasterBits, num_src);
|
|
||||||
dst->wi += num_src;
|
|
||||||
|
|
||||||
cleanup1:;
|
|
||||||
if ((DGifCloseFile(f) != GIF_OK) && !ret) {
|
if ((DGifCloseFile(f) != GIF_OK) && !ret) {
|
||||||
ret = "DGifCloseFile failed";
|
ret = "DGifCloseFile failed";
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user