-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reduce memory usage in image processing pipeline (#14)
* Implement a cached kernel scaler Optimized for scaling a lot of images in a few fixed destination and source sizes. * test: Add benchmark for entire conversion pipeline * Update processing pipeline to use new scaler * test: Avoid extra copying in Grayscale benchmarks * Remove unnecessary mutex pointer from cache scaler * Decouple grayscale func * Implement an image pool * Utilize image pool in processing pipeline * Prevent new allocations if decoded image is already gray
- Loading branch information
Showing
18 changed files
with
308 additions
and
182 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package imgutil | ||
|
||
import ( | ||
"image" | ||
"sync" | ||
) | ||
|
||
// NewImagePool creates an ImagePool. | ||
func NewImagePool() *ImagePool { | ||
return &ImagePool{ | ||
cache: make(map[int]*sync.Pool), | ||
} | ||
} | ||
|
||
// ImagePool maintains a sync.Pool of pixel arrays for each image resolution gotten from it. | ||
type ImagePool struct { | ||
cache map[int]*sync.Pool | ||
mu sync.Mutex | ||
} | ||
|
||
// GetFromImage converts an image into a grayscale image with pixel slice taken from the pool. | ||
func (p *ImagePool) GetFromImage(img image.Image) *image.Gray { | ||
if i, ok := img.(*image.Gray); ok { | ||
return i | ||
} | ||
dst := p.Get(img.Bounds().Dx(), img.Bounds().Dy()) | ||
grayscale(dst, img) | ||
return dst | ||
} | ||
|
||
func (p *ImagePool) getPool(pixLen int) *sync.Pool { | ||
p.mu.Lock() | ||
pool, ok := p.cache[pixLen] | ||
if !ok { | ||
pool = &sync.Pool{ | ||
New: func() interface{} { | ||
tmp := make([]uint8, pixLen) | ||
return &tmp | ||
}, | ||
} | ||
p.cache[pixLen] = pool | ||
} | ||
p.mu.Unlock() | ||
return pool | ||
} | ||
|
||
// Get gets a grayscale image of specified width and height with pixel slice taken from the pool. | ||
func (p *ImagePool) Get(width, height int) *image.Gray { | ||
tmp := p.getPool(width * height).Get().(*[]uint8) | ||
return &image.Gray{ | ||
Pix: *tmp, | ||
Stride: width, | ||
Rect: image.Rect(0, 0, width, height), | ||
} | ||
} | ||
|
||
// Put puts an images pixel slice back into the pool. | ||
func (p *ImagePool) Put(img *image.Gray) { | ||
p.getPool(len(img.Pix)).Put(&img.Pix) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.