Agamnentzar / ag-psd

Javascript library for reading and writing PSD files
Other
504 stars 66 forks source link

Why is it much slower than the PSD library? #171

Open Sunge8081 opened 6 months ago

Sunge8081 commented 6 months ago
import { readPsd } from 'ag-psd';
import * as fs from 'fs';
const PSD = require('psd');

let file="/Users/xiaobaozi/Downloads/模板/批量模板/模板1/主图.psd" //size≈196MB
let buf=fs.readFileSync(file)

console.time("ag-psd:readPsd")
//@ts-ignore
const psd = readPsd(buf.buffer,{ skipLayerImageData: true, skipCompositeImageData: true, skipThumbnail: true });
console.timeEnd("ag-psd:readPsd")

console.time("PSD:readPsd")
const psd2 = PSD.fromFile(file);
psd2.parse();
console.timeEnd("PSD:readPsd")

console: ag-psd:readPsd: 2.453s PSD:readPsd: 909.677ms

Agamnentzar commented 6 months ago

I think it's because ag-psd is parsing the whole file in readPsd call, and then you have entire structure available to you, psd library is only doing initial parsing and then it's processing the file when you're calling api functions on it, so the processing time is spread around in all of the functions. In this example you're not reading anything from the file, so the psd library is skipping most of the work.

That said 2.453s is a lot of time, if it's possible for you to share the file I could look into performance issues and maybe improve it.

826327700 commented 6 months ago

Is it possible to set ReadOptions and discard some options based on user customization to make reading psd faster?

826327700 commented 6 months ago

我认为这是因为ag-psd在 readPsd 调用中解析整个文件,然后你就有了可用的整个结构,psd库只进行初始解析,然后当你调用 api 函数时它正在处理文件,所以处理时间是分布在所有功能中。在此示例中,您没有从文件中读取任何内容,因此psd库将跳过大部分工作。

也就是说 2.453 秒是很多时间,如果您可以共享该文件,我可以研究性能问题并可能改进它。

This is my test psd file: https://gallery-dev-1318352346.cos.ap-guangzhou.myqcloud.com/upload/dev/gallery/public/2024-01-10/%E4%B8%BB%E5%9B%BE.psd

Agamnentzar commented 6 months ago

Just released version 20.1.0 with fix for the issue.

It was actually mostly decoding that huge 46MB xmpMetadata field, I improved utf-8 decoding/encoding, and now it should be really fast.

826327700 commented 6 months ago

Just released version 20.1.0 with fix for the issue.

It was actually mostly decoding that huge 46MB xmpMetadata field, I improved utf-8 decoding/encoding, and now it should be really fast.

Your improvement was so timely, and now the reading time has been reduced to within 100ms. It's great, thank you! It is very helpful for my current work!