SDWebImage / SDWebImageWebPCoder

A WebP coder plugin for SDWebImage, use libwebp
MIT License
217 stars 84 forks source link

SDImageWebPCoder extremely slow #111

Open BuffBu opened 3 months ago

BuffBu commented 3 months ago

I want to use WebP format for animations instead of GIFs. However, I've noticed that SDImageWebPCoder is significantly slower, with a performance gap of tens of times compared to GIFs and the system decoder available on iOS 14.

Below are my test cases and their results. Since we still have a large user base on iOS versions below 14, could you please advise on whether SDImageWebPCoder can be optimized?

final class ImagePerformanceTestCase: XCTestCase {

    var testAnimView = SDAnimatedImageView()

    override func setUpWithError() throws {
        super.setUp()
        SDImageCodersManager.shared.addCoder(SDImageWebPCoder.shared)
    }

    @available(iOS 14.0, *)
    func testAWebpDecodingPerformance() throws {
        guard let webPUrl = Bundle.main.url(forResource: "cubew", withExtension: "webp"),
              let webPData = try? Data(contentsOf: webPUrl) else {
            XCTFail("Failed to load Webp image data.")
            return
        }
        print("webp图片大小\(webPData.count)")
        // 测试解码性能
        measure {
            for _ in 0 ..< 10 {
                var img = SDImageAWebPCoder.shared.decodedImage(with: webPData, options: nil)
            }
            //testAnimView.sd_setImage(with:URL(string:"https://isparta.github.io/compare-webp/image/gif_webp/gif/1.gif"))
        }
    }

    func testWebPDecodingPerformance() throws {
        guard let webPUrl = Bundle.main.url(forResource: "cubew", withExtension: "webp"),
              let webPData = try? Data(contentsOf: webPUrl) else {
            XCTFail("Failed to load WebP image data.")
            return
        }
        print("webp图片大小\(webPData.count)")
        // 测试解码性能
        measure {
            for _ in 0 ..< 10 {
                var img = SDImageWebPCoder.shared.decodedImage(with: webPData, options: nil)
            }
            //testAnimView.sd_setImage(with:URL(string: "https://isparta.github.io/compare-webp/image/gif_webp/webp/1.webp"))
        }
    }

    func testGIFDecodingPerformance() throws {
        guard let gifUrl = Bundle.main.url(forResource: "cube", withExtension: "gif"),
              let gifData = try? Data(contentsOf: gifUrl) else {
            XCTFail("Failed to load GIF image data.")
            return
        }
        print("gif图片大小\(gifData.count)")
        // 测试解码性能
        measure {
            for _ in 0 ..< 10 {
                 var img = SDImageGIFCoder.shared.decodedImage(with: gifData, options: nil)
            }
            //testAnimView.sd_setImage(with:URL(string:"https://isparta.github.io/compare-webp/image/gif_webp/gif/1.gif"))
        }
    }
}

Test Case '-[image_Tests.ImagePerformanceTestCase testAWebpDecodingPerformance]' started. webp图片大小380850 /Users/100010/test/.demo/features/test_ios/11.1.8/common/image_ios/Example/image/ImagePerformanceTestCase.swift:37: Test Case '-[image_Tests.ImagePerformanceTestCase testAWebpDecodingPerformance]' measured [Time, seconds] average: 0.034, relative standard deviation: 124.017%, values: [0.159663, 0.025849, 0.020243, 0.018986, 0.019030, 0.018984, 0.018927, 0.018926, 0.019007, 0.018933], performanceMetricID:com.apple.XCTPerformanceMetric_WallClockTime, baselineName: "", baselineAverage: , polarity: prefers smaller, maxPercentRegression: 10.000%, maxPercentRelativeStandardDeviation: 10.000%, maxRegression: 0.100, maxStandardDeviation: 0.100 Test Case '-[image_Tests.ImagePerformanceTestCase testAWebpDecodingPerformance]' passed (0.623 seconds). Test Case '-[image_Tests.ImagePerformanceTestCase testGIFDecodingPerformance]' started. gif图片大小890847 /Users/100010/test/.demo/features/test_ios/11.1.8/common/image_ios/Example/image/ImagePerformanceTestCase.swift:70: Test Case '-[image_Tests.ImagePerformanceTestCase testGIFDecodingPerformance]' measured [Time, seconds] average: 0.032, relative standard deviation: 41.455%, values: [0.071205, 0.030790, 0.027024, 0.027092, 0.027007, 0.027131, 0.027023, 0.026939, 0.026912, 0.026888], performanceMetricID:com.apple.XCTPerformanceMetric_WallClockTime, baselineName: "", baselineAverage: , polarity: prefers smaller, maxPercentRegression: 10.000%, maxPercentRelativeStandardDeviation: 10.000%, maxRegression: 0.100, maxStandardDeviation: 0.100 Test Case '-[image_Tests.ImagePerformanceTestCase testGIFDecodingPerformance]' passed (0.572 seconds). Test Case '-[image_Tests.ImagePerformanceTestCase testWebPDecodingPerformance]' started. webp图片大小380850

/Users/100010/test/.demo/features/test_ios/11.1.8/common/image_ios/Example/image/ImagePerformanceTestCase.swift:54: Test Case '-[image_Tests.ImagePerformanceTestCase testWebPDecodingPerformance]' measured [Time, seconds] average: 2.249, relative standard deviation: 1.178%, values: [2.328468, 2.235940, 2.237927, 2.241540, 2.241171, 2.239154, 2.238738, 2.246966, 2.241613, 2.242533], performanceMetricID:com.apple.XCTPerformanceMetric_WallClockTime, baselineName: "", baselineAverage: , polarity: prefers smaller, maxPercentRegression: 10.000%, maxPercentRelativeStandardDeviation: 10.000%, maxRegression: 0.100, maxStandardDeviation: 0.100 Test Case '-[image_Tests.ImagePerformanceTestCase testWebPDecodingPerformance]' passed (22.747 seconds). Test Suite 'ImagePerformanceTestCase' passed at 2024-06-12 21:01:16.642. Executed 3 tests, with 0 failures (0 unexpected) in 23.942 (23.942) seconds Test Suite 'image_Tests.xctest' passed at 2024-06-12 21:01:16.643. Executed 3 tests, with 0 failures (0 unexpected) in 23.942 (23.942) seconds Test Suite 'Selected tests' passed at 2024-06-12 21:01:16.643. Executed 3 tests, with 0 failures (0 unexpected) in 23.942 (23.943) seconds