qax-os / excelize

Go language library for reading and writing Microsoft Excel™ (XLAM / XLSM / XLSX / XLTM / XLTX) spreadsheets
https://xuri.me/excelize
BSD 3-Clause "New" or "Revised" License
18.41k stars 1.72k forks source link

Use a faster deepcopy library #2030

Closed Juneezee closed 4 days ago

Juneezee commented 4 days ago

PR Details

Description

Related Issue

Closes https://github.com/qax-os/excelize/issues/2029.

Motivation and Context

Benchmark:

package excelize

import (
    "testing"

    olddeepcopy "github.com/mohae/deepcopy"
    newdeepcopy "github.com/tiendc/go-deepcopy"
)

func BenchmarkCopyXlsxCol(b *testing.B) {
    src := xlsxCol{}

    b.Run("old", func(b *testing.B) {
        for i := 0; i < b.N; i++ {
            olddeepcopy.Copy(src)
        }
    })

    b.Run("new", func(b *testing.B) {
        for i := 0; i < b.N; i++ {
            var dst xlsxCol
            newdeepcopy.Copy(&dst, src)
        }
    })
}

func BenchmarkCopyXlsxRow(b *testing.B) {
    src := xlsxRow{}

    b.Run("old", func(b *testing.B) {
        for i := 0; i < b.N; i++ {
            olddeepcopy.Copy(src)
        }
    })

    b.Run("new", func(b *testing.B) {
        for i := 0; i < b.N; i++ {
            var dst xlsxRow
            newdeepcopy.Copy(&dst, src)
        }
    })
}

func BenchmarkCopyXlsxWorksheet(b *testing.B) {
    f, err := prepareTestBook1()
    if err != nil {
        b.Fatal(err)
    }

    idx, err := f.NewSheet("CopySheet")
    if err != nil {
        b.Fatal(err)
    }

    fromSheet := f.GetSheetName(idx)
    src, err := f.workSheetReader(fromSheet)
    if err != nil {
        b.Fatal(err)
    }

    b.Run("old", func(b *testing.B) {
        for i := 0; i < b.N; i++ {
            olddeepcopy.Copy(src)
        }
    })

    b.Run("new", func(b *testing.B) {
        for i := 0; i < b.N; i++ {
            var dst xlsxWorksheet
            newdeepcopy.Copy(&dst, src)
        }
    })
}

Result:

goos: linux
goarch: amd64
pkg: github.com/xuri/excelize/v2
cpu: AMD Ryzen 7 PRO 4750U with Radeon Graphics
BenchmarkCopyXlsxCol/old-16           446193          2483 ns/op         224 B/op         13 allocs/op
BenchmarkCopyXlsxCol/new-16           963056          1148 ns/op         128 B/op          3 allocs/op
BenchmarkCopyXlsxRow/old-16           344492          3135 ns/op         344 B/op         16 allocs/op
BenchmarkCopyXlsxRow/new-16           909142          1371 ns/op         192 B/op          3 allocs/op
BenchmarkCopyXlsxWorksheet/old-16              49108         23097 ns/op        3656 B/op        128 allocs/op
BenchmarkCopyXlsxWorksheet/new-16             176534          7085 ns/op         808 B/op          6 allocs/op
PASS
ok      github.com/xuri/excelize/v2 7.377s

How Has This Been Tested

Types of changes

Checklist

codecov[bot] commented 4 days ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 99.20%. Comparing base (5f446f2) to head (1e5744d). Report is 1 commits behind head on master.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## master #2030 +/- ## ======================================= Coverage 99.20% 99.20% ======================================= Files 32 32 Lines 29952 29958 +6 ======================================= + Hits 29714 29720 +6 Misses 158 158 Partials 80 80 ``` | [Flag](https://app.codecov.io/gh/qax-os/excelize/pull/2030/flags?src=pr&el=flags&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=qax-os) | Coverage Δ | | |---|---|---| | [unittests](https://app.codecov.io/gh/qax-os/excelize/pull/2030/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=qax-os) | `99.20% <100.00%> (+<0.01%)` | :arrow_up: | Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=qax-os#carryforward-flags-in-the-pull-request-comment) to find out more.

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.


🚨 Try these New Features: