Optimize memory usage

This commit is contained in:
DarthSim
2017-09-28 23:18:01 +06:00
parent 816f6a8e07
commit aa4e6731dc
3 changed files with 31 additions and 15 deletions

View File

@@ -13,7 +13,7 @@ import (
func main() { func main() {
// Force garbage collection // Force garbage collection
go func() { go func() {
for _ = range time.Tick(time.Second) { for _ = range time.Tick(10 * time.Second) {
debug.FreeOSMemory() debug.FreeOSMemory()
} }
}() }()

View File

@@ -88,7 +88,6 @@ func initVips() {
log.Fatalln("unable to start vips!") log.Fatalln("unable to start vips!")
} }
C.vips_concurrency_set(1)
C.vips_cache_set_max_mem(100 * 1024 * 1024) // 100Mb C.vips_cache_set_max_mem(100 * 1024 * 1024) // 100Mb
C.vips_cache_set_max(500) C.vips_cache_set_max(500)
@@ -100,6 +99,13 @@ func initVips() {
vipsSupportSmartcrop = C.vips_support_smartcrop() == 1 vipsSupportSmartcrop = C.vips_support_smartcrop() == 1
} }
func randomAccessRequired(po processingOptions) int {
if po.gravity == SMART {
return 1
}
return 0
}
func vipsTypeSupportedLoad(imgtype imageType) bool { func vipsTypeSupportedLoad(imgtype imageType) bool {
switch imgtype { switch imgtype {
case JPEG: case JPEG:
@@ -173,6 +179,10 @@ func calcCrop(width, height int, po processingOptions) (left, top int) {
func processImage(data []byte, imgtype imageType, po processingOptions) ([]byte, error) { func processImage(data []byte, imgtype imageType, po processingOptions) ([]byte, error) {
defer keepAlive(data) defer keepAlive(data)
if po.gravity == SMART && !vipsSupportSmartcrop {
return nil, errors.New("Smart crop is not supported by used version of libvips")
}
err := C.int(0) err := C.int(0)
var img *C.struct__VipsImage var img *C.struct__VipsImage
@@ -183,13 +193,13 @@ func processImage(data []byte, imgtype imageType, po processingOptions) ([]byte,
// Load the image // Load the image
switch imgtype { switch imgtype {
case JPEG: case JPEG:
err = C.vips_jpegload_buffer_go(unsafe.Pointer(&data[0]), C.size_t(len(data)), &img) err = C.vips_jpegload_buffer_go(unsafe.Pointer(&data[0]), C.size_t(len(data)), &img, C.int(randomAccessRequired(po)))
case PNG: case PNG:
err = C.vips_pngload_buffer_go(unsafe.Pointer(&data[0]), C.size_t(len(data)), &img) err = C.vips_pngload_buffer_go(unsafe.Pointer(&data[0]), C.size_t(len(data)), &img, C.int(randomAccessRequired(po)))
case GIF: case GIF:
err = C.vips_gifload_buffer_go(unsafe.Pointer(&data[0]), C.size_t(len(data)), &img) err = C.vips_gifload_buffer_go(unsafe.Pointer(&data[0]), C.size_t(len(data)), &img, C.int(randomAccessRequired(po)))
case WEBP: case WEBP:
err = C.vips_webpload_buffer_go(unsafe.Pointer(&data[0]), C.size_t(len(data)), &img) err = C.vips_webpload_buffer_go(unsafe.Pointer(&data[0]), C.size_t(len(data)), &img, C.int(randomAccessRequired(po)))
} }
if err != 0 { if err != 0 {
return nil, vipsError() return nil, vipsError()
@@ -228,7 +238,7 @@ func processImage(data []byte, imgtype imageType, po processingOptions) ([]byte,
pCrop = 1 pCrop = 1
pWidth, pHeight = po.width, po.height pWidth, pHeight = po.width, po.height
if po.gravity == SMART && vipsSupportSmartcrop { if po.gravity == SMART {
pSmart = 1 pSmart = 1
} else { } else {
pLeft, pTop = calcCrop(round(float64(imgWidth)*pScale), round(float64(imgHeight)*pScale), po) pLeft, pTop = calcCrop(round(float64(imgWidth)*pScale), round(float64(imgHeight)*pScale), po)

22
vips.h
View File

@@ -26,6 +26,12 @@ clear_image(VipsImage **in) {
g_clear_object(in); g_clear_object(in);
} }
VipsAccess
access_mode(int random) {
if (random > 0) return VIPS_ACCESS_RANDOM;
return VIPS_ACCESS_SEQUENTIAL;
}
void void
swap_and_clear(VipsImage **in, VipsImage *out) { swap_and_clear(VipsImage **in, VipsImage *out) {
clear_image(in); clear_image(in);
@@ -64,31 +70,31 @@ vips_type_find_save_go(int imgtype) {
} }
int int
vips_jpegload_buffer_go(void *buf, size_t len, VipsImage **out) vips_jpegload_buffer_go(void *buf, size_t len, VipsImage **out, int random)
{ {
return vips_jpegload_buffer(buf, len, out, "access", VIPS_ACCESS_RANDOM, NULL); return vips_jpegload_buffer(buf, len, out, "access", access_mode(random), NULL);
} }
int int
vips_pngload_buffer_go(void *buf, size_t len, VipsImage **out) vips_pngload_buffer_go(void *buf, size_t len, VipsImage **out, int random)
{ {
return vips_pngload_buffer(buf, len, out, "access", VIPS_ACCESS_RANDOM, NULL); return vips_pngload_buffer(buf, len, out, "access", access_mode(random), NULL);
} }
int int
vips_gifload_buffer_go(void *buf, size_t len, VipsImage **out) vips_gifload_buffer_go(void *buf, size_t len, VipsImage **out, int random)
{ {
#if VIPS_SUPPORT_GIF #if VIPS_SUPPORT_GIF
return vips_gifload_buffer(buf, len, out, "access", VIPS_ACCESS_RANDOM, NULL); return vips_gifload_buffer(buf, len, out, "access", access_mode(random), NULL);
#else #else
return 0; return 0;
#endif #endif
} }
int int
vips_webpload_buffer_go(void *buf, size_t len, VipsImage **out) vips_webpload_buffer_go(void *buf, size_t len, VipsImage **out, int random)
{ {
return vips_webpload_buffer(buf, len, out, "access", VIPS_ACCESS_RANDOM, NULL); return vips_webpload_buffer(buf, len, out, "access", access_mode(random), NULL);
} }
int int