benchmark Uint32Array to node Buffer.writeUInt32BE
This commit is contained in:
parent
6974435a46
commit
0fba279f64
|
@ -161,11 +161,11 @@ export async function incrementialBackupRedis(
|
||||||
|
|
||||||
const { abgr } = palette;
|
const { abgr } = palette;
|
||||||
const compLength = Math.min(oldChunk.length, curChunk.length);
|
const compLength = Math.min(oldChunk.length, curChunk.length);
|
||||||
tileBuffer = Buffer.allocUnsafe(TILE_SIZE ** 2 * 4);
|
tileBuffer = new Uint32Array(TILE_SIZE ** 2);
|
||||||
for (let i = 0; i < compLength; i += 1) {
|
for (let i = 0; i < compLength; i += 1) {
|
||||||
const curPxl = curChunk[i];
|
const curPxl = curChunk[i];
|
||||||
if (oldChunk[i] !== curPxl) {
|
if (oldChunk[i] !== curPxl) {
|
||||||
tileBuffer.writeUInt32BE(abgr[curPxl & 0x3F], i * 4);
|
tileBuffer[i] = abgr[curPxl & 0x3F];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -173,22 +173,17 @@ export async function incrementialBackupRedis(
|
||||||
const curChunkLength = curChunk.length;
|
const curChunkLength = curChunk.length;
|
||||||
|
|
||||||
for (let i = oldChunkLength; i < curChunkLength; i += 1) {
|
for (let i = oldChunkLength; i < curChunkLength; i += 1) {
|
||||||
tileBuffer.writeUInt32BE(abgr[curChunk[i] & 0x3F], i * 4);
|
tileBuffer[i] = abgr[curChunk[i] & 0x3F];
|
||||||
}
|
}
|
||||||
|
|
||||||
if (oldChunkLength > curChunkLength) {
|
if (oldChunkLength > curChunkLength) {
|
||||||
const blank = abgr[0];
|
const blank = abgr[0];
|
||||||
for (let i = curChunkLength; i < oldChunkLength; i += 1) {
|
for (let i = curChunkLength; i < oldChunkLength; i += 1) {
|
||||||
if (oldChunk[i] !== 0) {
|
if (oldChunk[i] !== 0) {
|
||||||
tileBuffer.writeUInt32BE(blank, i * 4);
|
tileBuffer[i] = blank;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const curLength = Math.max(oldChunkLength, curChunkLength) * 4;
|
|
||||||
if (curLength < tileBuffer.length) {
|
|
||||||
tileBuffer.fill(0, curLength);
|
|
||||||
}
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(
|
console.error(
|
||||||
// eslint-disable-next-line max-len
|
// eslint-disable-next-line max-len
|
||||||
|
@ -205,7 +200,7 @@ export async function incrementialBackupRedis(
|
||||||
const filename = `${xBackupDir}/${y}.png`;
|
const filename = `${xBackupDir}/${y}.png`;
|
||||||
// eslint-disable-next-line no-await-in-loop
|
// eslint-disable-next-line no-await-in-loop
|
||||||
await sharp(
|
await sharp(
|
||||||
tileBuffer, {
|
Buffer.from(tileBuffer.buffer), {
|
||||||
raw: {
|
raw: {
|
||||||
width: TILE_SIZE,
|
width: TILE_SIZE,
|
||||||
height: TILE_SIZE,
|
height: TILE_SIZE,
|
||||||
|
@ -284,26 +279,18 @@ export async function createPngBackup(
|
||||||
}
|
}
|
||||||
if (chunk && chunk.length) {
|
if (chunk && chunk.length) {
|
||||||
try {
|
try {
|
||||||
const tileBuffer = Buffer.allocUnsafe(TILE_SIZE ** 2 * 4);
|
const tileBuffer = new Uint32Array(TILE_SIZE ** 2);
|
||||||
const chunkLength = chunk.length;
|
const chunkLength = chunk.length;
|
||||||
const { abgr } = palette;
|
const { abgr } = palette;
|
||||||
for (let i = 0; i < chunkLength; i += 1) {
|
for (let i = 0; i < chunkLength; i += 1) {
|
||||||
tileBuffer.writeUInt32BE(
|
tileBuffer[i] = abgr[chunk[i] & 0x3F];
|
||||||
abgr[chunk[i] & 0x3F],
|
|
||||||
i * 4,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const curLength = chunkLength * 4;
|
|
||||||
if (curLength < tileBuffer.length) {
|
|
||||||
tileBuffer.fill(0, curLength);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const filename = `${xBackupDir}/${y}.png`;
|
const filename = `${xBackupDir}/${y}.png`;
|
||||||
|
|
||||||
// eslint-disable-next-line no-await-in-loop
|
// eslint-disable-next-line no-await-in-loop
|
||||||
await sharp(
|
await sharp(
|
||||||
tileBuffer, {
|
Buffer.from(tileBuffer.buffer), {
|
||||||
raw: {
|
raw: {
|
||||||
width: TILE_SIZE,
|
width: TILE_SIZE,
|
||||||
height: TILE_SIZE,
|
height: TILE_SIZE,
|
||||||
|
|
Loading…
Reference in New Issue
Block a user