mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-22 16:47:28 +01:00
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.
151 lines
4.4 KiB
C
151 lines
4.4 KiB
C
// Use of this source code is governed by a BSD-style license that can be found
|
|
// in the LICENSE file.
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include "gif_lib.h"
|
|
|
|
int gif_mimic_read_func(GifFileType* f, GifByteType* ptr, int len) {
|
|
puffs_base_buf1* src = (puffs_base_buf1*)(f->UserData);
|
|
if (len < 0) {
|
|
return 0;
|
|
}
|
|
size_t n = (size_t)(len);
|
|
size_t num_src = src->wi - src->ri;
|
|
if (n > num_src) {
|
|
n = num_src;
|
|
}
|
|
memmove(ptr, src->ptr + src->ri, n);
|
|
src->ri += 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* ret = NULL;
|
|
|
|
// TODO: update API calls for libgif version 5 vs version 4?
|
|
GifFileType* f = DGifOpen(src, gif_mimic_read_func);
|
|
if (!f) {
|
|
ret = "DGifOpen failed";
|
|
goto cleanup0;
|
|
}
|
|
|
|
if (gif_mimic_DGifSlurp(f, dst) != GIF_OK) {
|
|
ret = "DGifSlurp failed";
|
|
goto cleanup1;
|
|
}
|
|
|
|
// TODO: have Puffs accept multi-frame (animated) GIFs.
|
|
if (f->ImageCount != 1) {
|
|
ret = "GIF image has more than one frame";
|
|
goto cleanup1;
|
|
}
|
|
|
|
cleanup1:;
|
|
// See "Puffs modification" in gif_mimic_DGifSlurp above.
|
|
SavedImage* sp;
|
|
for (sp = f->SavedImages; sp < f->SavedImages + f->ImageCount; sp++) {
|
|
sp->RasterBits = NULL;
|
|
}
|
|
|
|
if ((DGifCloseFile(f) != GIF_OK) && !ret) {
|
|
ret = "DGifCloseFile failed";
|
|
}
|
|
cleanup0:;
|
|
return ret;
|
|
}
|