Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Performance in debug builds #26

Open
jnordberg opened this issue Jul 4, 2022 · 5 comments
Open

Performance in debug builds #26

jnordberg opened this issue Jul 4, 2022 · 5 comments

Comments

@jnordberg
Copy link

Using this library in an application test suite where debug builds are enabled encoding a single 1024x1024 image takes several minutes pegging the cpu (M1) at 100% and using 300mb+ of memory.

Is there some workaround for this? I don't mind png coding being a bit slower in tests but several minutes is a showstopper. I can't seem to find any way to selectively enable optimizations for a swift package included via Xcode's "Package Dependencies" (setting the whole test target to -Os doesn't seem to help either for some reason...)

@tayloraswift
Copy link
Owner

tayloraswift commented Jul 6, 2022

can you share the test image?

have you tried configuring the compression level? keep in mind swift-png uses the highest compression setting by default, which is significantly more aggressive than libpng's maximum setting, so it's not surprising that it's very slow encoding, since it is optimizing for file size, not speed. the higher levels have diminishing returns, so dropping down to say, L5 or so should not severely increase the file size

@ospfranco
Copy link

ospfranco commented Jul 30, 2022

I see the same behavior, the PNG I'm using is just a screenshot from my desktop. It only happens on debug build though, on release it seems to be very speedy.

Screenshot 000804

I'm on a m2 air, on Monterrey 12.5

@philipcolejohnson
Copy link

I wanted to pop in late and say I'm also seeing this in v4.0.2, running on a M2 pro, Ventura 13.0.1.

I am using the compression level 0, which I assume is little to none. Release builds run this very quickly, but debug builds take nearly 2 seconds for a 1280x720 image. Here's what I am doing:

public func writePng(pngData: PNG.Data.Rectangular, to path: URL) throws {
    var pngFile: PngFile = .init(filePath: path)
    try pngData.compress(stream: &pngFile, level: 0)
}

Unfortunately, I am unable to share the images, as they are private. But here is where I'm seeing the CPU usage in the profiler:

CleanShot 2023-01-06 at 14 47 40@2x

@kellyo-cricut
Copy link

I'm encountering the same issue in 4.0.2/M1/Ventura 13.4.1 with 'normal' sized pngs (~100k, ~1000x1000). Profiling it shows most of the time is spent in the SIMD16 extension find function. Is there any work towards solving this or any suggestions for adjusting that call?

@stackotter
Copy link
Contributor

I've been working on optimising Swift PNG's debug mode performance. I'm using a 3440x1440 image for my profiling which originally took 28.1 seconds to decode on average in debug mode. After a few hours of profiling and optimisation I've gotten that down to 4.16 seconds (6.75x faster! 🎉). It's not ready to PR yet, but in case anyone wants to test it out it's here: https://github.com/stackotter/swift-png/tree/debug_mode_optimization

I'm testing with Swift 6.0 btw. Some of these changes don't seem to have as much of an effect on debug mode performance in older toolchains.

The biggest initial gains were replacing integer size conversions with gross unsafeBitCast code using tuples, and replacing for loops over iterators with while loops. For some reason these two operations have terrible debug mode performance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants