Unverified Commit ce45a939 authored by Steven's avatar Steven Committed by GitHub

feat(image-optimization): add experimental flag `imgOptOperationCache` (#94246)

This PR adds a new experimental option to enable/disable operation cache
in sharp.

https://sharp.pixelplumbing.com/api-utility/#cache
parent 747466ca
...@@ -260,6 +260,7 @@ export const experimentalSchema = { ...@@ -260,6 +260,7 @@ export const experimentalSchema = {
fullySpecified: z.boolean().optional(), fullySpecified: z.boolean().optional(),
gzipSize: z.boolean().optional(), gzipSize: z.boolean().optional(),
imgOptConcurrency: z.number().int().optional().nullable(), imgOptConcurrency: z.number().int().optional().nullable(),
imgOptOperationCache: z.boolean().optional().nullable(),
imgOptTimeoutInSeconds: z.number().int().optional(), imgOptTimeoutInSeconds: z.number().int().optional(),
imgOptMaxInputPixels: z.number().int().optional(), imgOptMaxInputPixels: z.number().int().optional(),
imgOptSequentialRead: z.boolean().optional().nullable(), imgOptSequentialRead: z.boolean().optional().nullable(),
......
...@@ -534,6 +534,7 @@ export interface ExperimentalConfig { ...@@ -534,6 +534,7 @@ export interface ExperimentalConfig {
allowedRevalidateHeaderKeys?: string[] allowedRevalidateHeaderKeys?: string[]
fetchCacheKeyPrefix?: string fetchCacheKeyPrefix?: string
imgOptConcurrency?: number | null imgOptConcurrency?: number | null
imgOptOperationCache?: boolean | null
imgOptTimeoutInSeconds?: number imgOptTimeoutInSeconds?: number
imgOptMaxInputPixels?: number imgOptMaxInputPixels?: number
imgOptSequentialRead?: boolean | null imgOptSequentialRead?: boolean | null
...@@ -1995,6 +1996,7 @@ export const defaultConfig = Object.freeze({ ...@@ -1995,6 +1996,7 @@ export const defaultConfig = Object.freeze({
), ),
memoryBasedWorkersCount: false, memoryBasedWorkersCount: false,
imgOptConcurrency: null, imgOptConcurrency: null,
imgOptOperationCache: null,
imgOptTimeoutInSeconds: 7, imgOptTimeoutInSeconds: 7,
imgOptMaxInputPixels: 268_402_689, // https://sharp.pixelplumbing.com/api-constructor#:~:text=%5Boptions.limitInputPixels%5D imgOptMaxInputPixels: 268_402_689, // https://sharp.pixelplumbing.com/api-constructor#:~:text=%5Boptions.limitInputPixels%5D
imgOptSequentialRead: null, imgOptSequentialRead: null,
...@@ -2149,6 +2151,7 @@ export interface NextConfigRuntime { ...@@ -2149,6 +2151,7 @@ export interface NextConfigRuntime {
| 'hideLogsAfterAbort' | 'hideLogsAfterAbort'
| 'removeUncaughtErrorAndRejectionListeners' | 'removeUncaughtErrorAndRejectionListeners'
| 'imgOptConcurrency' | 'imgOptConcurrency'
| 'imgOptOperationCache'
| 'imgOptMaxInputPixels' | 'imgOptMaxInputPixels'
| 'imgOptSequentialRead' | 'imgOptSequentialRead'
| 'imgOptSkipMetadata' | 'imgOptSkipMetadata'
...@@ -2218,6 +2221,7 @@ export function getNextConfigRuntime( ...@@ -2218,6 +2221,7 @@ export function getNextConfigRuntime(
removeUncaughtErrorAndRejectionListeners: removeUncaughtErrorAndRejectionListeners:
ex.removeUncaughtErrorAndRejectionListeners, ex.removeUncaughtErrorAndRejectionListeners,
imgOptConcurrency: ex.imgOptConcurrency, imgOptConcurrency: ex.imgOptConcurrency,
imgOptOperationCache: ex.imgOptOperationCache,
imgOptMaxInputPixels: ex.imgOptMaxInputPixels, imgOptMaxInputPixels: ex.imgOptMaxInputPixels,
imgOptSequentialRead: ex.imgOptSequentialRead, imgOptSequentialRead: ex.imgOptSequentialRead,
imgOptSkipMetadata: ex.imgOptSkipMetadata, imgOptSkipMetadata: ex.imgOptSkipMetadata,
......
...@@ -84,13 +84,19 @@ async function initCacheEntries( ...@@ -84,13 +84,19 @@ async function initCacheEntries(
return entries.sort((a, b) => a.expireAt - b.expireAt) return entries.sort((a, b) => a.expireAt - b.expireAt)
} }
export function getSharp(concurrency: number | null | undefined) { export function getSharp(
concurrency: number | null | undefined,
operationCache: boolean | null | undefined
) {
if (_sharp) { if (_sharp) {
return _sharp return _sharp
} }
try { try {
_sharp = require('sharp') as typeof import('sharp') _sharp = require('sharp') as typeof import('sharp')
if (_sharp && _sharp.concurrency() > 1) { if (typeof operationCache === 'boolean') {
_sharp.cache(operationCache)
}
if (_sharp.concurrency() > 1) {
// Reducing concurrency should reduce the memory usage too. // Reducing concurrency should reduce the memory usage too.
// We more aggressively reduce in dev but also reduce in prod. // We more aggressively reduce in dev but also reduce in prod.
// https://sharp.pixelplumbing.com/api-utility#concurrency // https://sharp.pixelplumbing.com/api-utility#concurrency
...@@ -223,7 +229,8 @@ async function deleteFromCacheDir(cacheDir: string, cacheKey: string) { ...@@ -223,7 +229,8 @@ async function deleteFromCacheDir(cacheDir: string, cacheKey: string) {
export async function detectContentType( export async function detectContentType(
buffer: Buffer, buffer: Buffer,
skipMetadata: boolean | null | undefined, skipMetadata: boolean | null | undefined,
concurrency?: number | null | undefined concurrency?: number | null | undefined,
operationCache?: boolean | null | undefined
): Promise<string | null> { ): Promise<string | null> {
if (buffer.byteLength === 0) { if (buffer.byteLength === 0) {
return null return null
...@@ -308,7 +315,7 @@ export async function detectContentType( ...@@ -308,7 +315,7 @@ export async function detectContentType(
format = detector(buffer) format = detector(buffer)
if (!format && !skipMetadata) { if (!format && !skipMetadata) {
const sharp = getSharp(concurrency) const sharp = getSharp(concurrency, operationCache)
const meta = await sharp(buffer) const meta = await sharp(buffer)
.metadata() .metadata()
.catch((_) => null) .catch((_) => null)
...@@ -806,6 +813,7 @@ export async function optimizeImage({ ...@@ -806,6 +813,7 @@ export async function optimizeImage({
width, width,
height, height,
concurrency, concurrency,
operationCache,
limitInputPixels, limitInputPixels,
sequentialRead, sequentialRead,
timeoutInSeconds, timeoutInSeconds,
...@@ -816,11 +824,12 @@ export async function optimizeImage({ ...@@ -816,11 +824,12 @@ export async function optimizeImage({
width: number width: number
height?: number height?: number
concurrency?: number | null concurrency?: number | null
operationCache?: boolean | null | undefined
limitInputPixels?: number limitInputPixels?: number
sequentialRead?: boolean | null sequentialRead?: boolean | null
timeoutInSeconds?: number timeoutInSeconds?: number
}): Promise<Buffer> { }): Promise<Buffer> {
const sharp = getSharp(concurrency) const sharp = getSharp(concurrency, operationCache)
const transformer = sharp(buffer, { const transformer = sharp(buffer, {
limitInputPixels, limitInputPixels,
sequentialRead: sequentialRead ?? undefined, sequentialRead: sequentialRead ?? undefined,
...@@ -1054,6 +1063,7 @@ export async function imageOptimizer( ...@@ -1054,6 +1063,7 @@ export async function imageOptimizer(
experimental: Pick< experimental: Pick<
NextConfigComplete['experimental'], NextConfigComplete['experimental'],
| 'imgOptConcurrency' | 'imgOptConcurrency'
| 'imgOptOperationCache'
| 'imgOptMaxInputPixels' | 'imgOptMaxInputPixels'
| 'imgOptSequentialRead' | 'imgOptSequentialRead'
| 'imgOptSkipMetadata' | 'imgOptSkipMetadata'
...@@ -1087,7 +1097,8 @@ export async function imageOptimizer( ...@@ -1087,7 +1097,8 @@ export async function imageOptimizer(
const upstreamType = await detectContentType( const upstreamType = await detectContentType(
upstreamBuffer, upstreamBuffer,
nextConfig.experimental.imgOptSkipMetadata, nextConfig.experimental.imgOptSkipMetadata,
nextConfig.experimental.imgOptConcurrency nextConfig.experimental.imgOptConcurrency,
nextConfig.experimental.imgOptOperationCache
) )
if ( if (
...@@ -1177,6 +1188,7 @@ export async function imageOptimizer( ...@@ -1177,6 +1188,7 @@ export async function imageOptimizer(
quality, quality,
width, width,
concurrency: nextConfig.experimental.imgOptConcurrency, concurrency: nextConfig.experimental.imgOptConcurrency,
operationCache: nextConfig.experimental.imgOptOperationCache,
limitInputPixels: nextConfig.experimental.imgOptMaxInputPixels, limitInputPixels: nextConfig.experimental.imgOptMaxInputPixels,
sequentialRead: nextConfig.experimental.imgOptSequentialRead, sequentialRead: nextConfig.experimental.imgOptSequentialRead,
timeoutInSeconds: nextConfig.experimental.imgOptTimeoutInSeconds, timeoutInSeconds: nextConfig.experimental.imgOptTimeoutInSeconds,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment