Skip to content

Commit

Permalink
perf: greatly increase the amount of tiles being read so nodejs start…
Browse files Browse the repository at this point in the history
…up time affects the timing less
  • Loading branch information
blacha committed Jun 15, 2022
1 parent 997e5dd commit 3e9c0f3
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .hyperfine.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[
{
"name": "Read 300 tiles from sparse.tiff",
"name": "Read 15,000 tiles from sparse.tiff",
"command": "node packages/core/build/__benchmark__/cog.read.benchmark.js packages/core/data/sparse.tiff"
}
]
9 changes: 6 additions & 3 deletions packages/core/src/__benchmark__/cog.read.benchmark.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { SourceMemory } from '@chunkd/core';
import { readFile } from 'fs/promises';
import { CogTiff } from '../cog.tiff.js';
import { SourceFile } from '@chunkd/source-file';

/** Read a tile from every image inside of a tiff 300 tiles read */
async function main(): Promise<void> {
for (let i = 0; i < 50; i++) {
const tiff = new CogTiff(new SourceFile(process.argv[process.argv.length - 1]));
const buf = await readFile(process.argv[process.argv.length - 1]);
const source = new SourceMemory('buf', buf);
for (let i = 0; i < 5_000; i++) {
const tiff = new CogTiff(source);
await tiff.init();

// 6 images
Expand Down
13 changes: 3 additions & 10 deletions packages/core/src/cog.tiff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,7 @@ export class CogTiff {

const ghostSize = nextOffsetIfd - this.cursor.currentOffset;
// GDAL now stores metadata between the IFD inside a ghost storage area
if (ghostSize > 0 && ghostSize < 16 * 1024) {
// logger?.debug(
// { offset: toHexString(this.cursor.currentOffset), length: toHexString(ghostSize) },
// 'GhostOptions',
// );
this.options.process(view.bytes(ghostSize));
}
if (ghostSize > 0 && ghostSize < 16 * 1024) this.options.process(view.bytes(ghostSize));

return this.processIfd(nextOffsetIfd);
}
Expand Down Expand Up @@ -136,16 +130,15 @@ export class CogTiff {
}

private async processIfd(offset: number): Promise<void> {
if (!this.source.hasBytes(offset, 4096)) await this.source.loadBytes(offset, 4096);

const { image, nextOffset } = await this.readIfd(offset);
this.images.push(image);

if (nextOffset) await this.processIfd(nextOffset);
}

private async readIfd(offset: number): Promise<{ nextOffset: number; image: CogTiffImage }> {
if (!this.source.hasBytes(offset, 1024)) await this.source.loadBytes(offset, this.source.chunkSize);
if (!this.source.hasBytes(offset, 4096)) await this.source.loadBytes(offset, 4096);

const view = this.cursor.seekTo(offset);
const tagCount = view.offset();
const byteStart = offset + this.ifdConfig.offset;
Expand Down

0 comments on commit 3e9c0f3

Please sign in to comment.