{"id":13494378,"url":"https://github.com/antlabs/pcopy","last_synced_at":"2026-01-30T16:22:45.732Z","repository":{"id":39712869,"uuid":"255794355","full_name":"antlabs/pcopy","owner":"antlabs","description":"pcopy是深度拷贝库，相比上个版本(v0.0.10)，性能提升4-10倍","archived":false,"fork":false,"pushed_at":"2024-12-14T04:15:40.000Z","size":161,"stargazers_count":89,"open_issues_count":2,"forks_count":12,"subscribers_count":5,"default_branch":"master","last_synced_at":"2024-12-14T05:18:56.676Z","etag":null,"topics":["copy","deepcopy","reflect"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/antlabs.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2020-04-15T03:27:27.000Z","updated_at":"2024-12-14T04:15:39.000Z","dependencies_parsed_at":"2023-12-19T06:39:37.479Z","dependency_job_id":null,"html_url":"https://github.com/antlabs/pcopy","commit_stats":null,"previous_names":[],"tags_count":16,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/antlabs%2Fpcopy","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/antlabs%2Fpcopy/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/antlabs%2Fpcopy/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/antlabs%2Fpcopy/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/antlabs","download_url":"https://codeload.github.com/antlabs/pcopy/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246045870,"owners_count":20714863,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["copy","deepcopy","reflect"],"created_at":"2024-07-31T19:01:24.439Z","updated_at":"2026-01-30T16:22:45.691Z","avatar_url":"https://github.com/antlabs.png","language":"Go","funding_links":[],"categories":["Go"],"sub_categories":[],"readme":"## 作用\n[![Go](https://github.com/antlabs/pcopy/workflows/Go/badge.svg)](https://github.com/antlabs/pcopy/actions)\n[![codecov](https://codecov.io/gh/antlabs/pcopy/branch/master/graph/badge.svg)](https://codecov.io/gh/antlabs/pcopy)\n\n`pcopy.Copy`主要用于两个类型间的深度拷贝, 前身是[deepcopy](https://github.com/antlabs/deepcopy)\n\n新加预热函数。Copy时打开加速开关，达到性能提升4-10倍的效果。\n\n警告: \n\n高性能的同时可能会有些bug, 如果发现bug可以去掉`pcopy.WithUsePreheat()`试下， 结果不一致，可以提issue。\n\n## feature\n* 高性能, 相对第一个版本提升4-10倍的性能\n* 支持异构结构体拷贝, dst和src可以是不同的类型，会拷贝dst和src交集的部分\n* 多类型支持struct/map/slice/array/int...int64/uint...uint64/ 等等\n\n## 内容\n- [Installation](#Installation)\n- [Quick start](#quick-start)\n- [example](#example)\n    - [1.拷贝slice](#copy-slice)\n    - [2.拷贝map](#copy-map)\n    - [3.简化业务代码开发](#simplify-business-code-development)\n- [性能压测](#benchmark)\n## Installation\n```\ngo get github.com/antlabs/pcopy\n```\n\n## Quick start\n```go\npackage main\n\nimport (\n    \"fmt\"\n    \"github.com/antlabs/pcopy\"\n)\n\ntype dst struct {\n    ID int\n    Result string\n}\n\ntype src struct{\n    ID int\n    Text string\n}\nfunc main() {\n   d, s := dst{}, src{ID:3}\n   pcopy.Preheat(\u0026dst{}, \u0026src{}) // 一对类型只要预热一次\n   pcopy.Copy(\u0026d, \u0026s, pcopy.WithUsePreheat())\n   fmt.Printf(\"%#v\\n\", d)\n   \n}\n\n```\n\n## copy slice\n```go\npackage main\n\nimport (\n        \"fmt\"\n\n        \"github.com/antlabs/pcopy\"\n)\n\nfunc main() {\n        i := []int{1, 2, 3, 4, 5, 6}\n        var o []int\n\n        pcopy.Preheat(\u0026o, \u0026i)\n        pcopy.Copy(\u0026o, \u0026i, pcopy.WithUsePreheat())\n\n        fmt.Printf(\"%#v\\n\", o)\n}\n\n```\n\n## copy map\n```go\npackage main\n\nimport (\n        \"fmt\"\n\n        \"github.com/antlabs/pcopy\"\n)\n\nfunc main() {\n        i := map[string]int{\n                \"cat\":  100,\n                \"head\": 10,\n                \"tr\":   3,\n                \"tail\": 44,\n        }\n\n        var o map[string]int\n        pcopy.Preheat(\u0026o, \u0026i)\n        pcopy.Copy(\u0026o, \u0026i, pcopy.WithUsePreheat())\n\n        fmt.Printf(\"%#v\\n\", o)\n}\n\n```\n## simplify business code development\n经常看到，对同一个结构体的，有值更新操作，都是一堆手工if 然后赋值的代码。不仅容易出错，还累。快使用pcopy解放双手。\n```go\ntype option struct {\n        Int int\n        Float64 float64\n        S  string\n}\n\nfunc main() {\n        var a, b option\n        if b.Int != 0 {\n                a.Int = b.Int\n        }\n\n        if b.Float64 != 0.0 {\n                a.Float64 = b.Float64\n        }\n\n        if b.S != \"\" {\n                a.S = b.S\n        }\n\n        pcopy.Preheat(\u0026a, \u0026b) //只要预热一次\n        //可以约化成\n        pcopy.Copy(\u0026a, \u0026b, pcopy.WithUsePreheat())\n}\n```\n# benchmark\n从零实现的pcopy相比json序列化与反序列化方式拥有更好的性能\n\n[压测仓库位置](https://github.com/1whour/deepcopy-benchmark)\n```\ngoos: darwin\ngoarch: arm64\npkg: benchmark\nBenchmark_Use_reflectValue_MiniCopy-8   \t  334728\t      3575 ns/op\nBenchmark_Use_reflectValue_DeepCopy-8   \t  595302\t      1956 ns/op\nBenchmark_Use_reflectValue_Copier-8     \t  203574\t      5860 ns/op\nBenchmark_Use_Ptr_jsoniter-8            \t  821113\t      1477 ns/op\nBenchmark_Use_Ptr_pcopy-8               \t 3390382\t       354.0 ns/op\nBenchmark_Use_Ptr_coven-8               \t 1414197\t       848.7 ns/op\nPASS\nok  \tbenchmark\t9.771s\n```\n\n### 本项目压测\n从下面的压测数据可以看到，基本提供了4-10倍的性能提升\n```\ngoos: darwin\ngoarch: arm64\npkg: github.com/antlabs/pcopy\nBenchmark_BaseMap_Unsafe_Pcopy-8               \t  529747\t      2343 ns/op\nBenchmark_BaseMap_miniCopy-8                   \t   62181\t     19212 ns/op\nBenchmark_BaseMap_Reflect-8                    \t   93810\t     12756 ns/op\nBenchmark_BaseSlice_Unsafe_Pcopy-8             \t 2013764\t       595.1 ns/op\nBenchmark_BaseSlice_miniCopy-8                 \t  154918\t      7728 ns/op\nBenchmark_BaseSlice_Reflect-8                  \t  188720\t      6393 ns/op\nBenchmark_BaseType_Unsafe_Pcopy-8              \t 4872112\t       243.8 ns/op\nBenchmark_BaseType_MiniCopy-8                  \t  517814\t      2278 ns/op\nBenchmark_BaseType_Pcopy-8                     \t  635156\t      1886 ns/op\nBenchmark_CompositeMap_Unsafe_Pcopy-8          \t  486253\t      2409 ns/op\nBenchmark_CompositeMap_miniCopy-8              \t  229674\t      5173 ns/op\nBenchmark_CompositeMap_Reflect-8               \t  475243\t      2490 ns/op\nBenchmark_GetLikeFavorited_Unsafe_Pcopy2-8     \t  446907\t      2662 ns/op\nBenchmark_GetLikeFavorited_Unsafe_Pcopy-8      \t  470217\t      2572 ns/op\nBenchmark_GetLikeFavorited_MiniCopy-8          \t   85674\t     13989 ns/op\nBenchmark_GetLikeFavorited_Reflect_Pcopy-8     \t  121603\t      9856 ns/op\nBenchmark_GetRedPoint_Unsafe_Pcopy-8           \t 1626688\t       736.1 ns/op\nBenchmark_GetRedPoint_MiniCopy-8               \t  650004\t      1871 ns/op\nBenchmark_GetRedPoint_Reflect_Pcopy-8          \t 1669778\t       722.0 ns/op\nBenchmark_Interface_Unsafe_Pcopy-8             \t 2869022\t       421.3 ns/op\nBenchmark_Interface_MiniCopy-8                 \t  413936\t      2704 ns/op\nBenchmark_Interface_Pcopy-8                    \t  440250\t      2688 ns/op\nBenchmark_Interface_BaseSlice_Unsafe_Pcopy-8   \t 1266501\t       947.4 ns/op\nBenchmark_Interface_BaseSlice_MiniCopy-8       \t  141610\t      8422 ns/op\nBenchmark_Interface_BaseSlice_Pcopy-8          \t  203906\t      5917 ns/op\nBenchmark_Ptr_BaseType1_Unsafe_Pcopy-8         \t  910153\t      1310 ns/op\nBenchmark_Ptr_BaseType1_Reflect_Pcopy-8        \t  391117\t      3026 ns/op\nBenchmark_Ptr_BaseSlice_Unsafe_Pcopy-8         \t  698156\t      1704 ns/op\nBenchmark_Ptr_BaseSlice_Reflect_Pcopy-8        \t  219999\t      5415 ns/op\nBenchmark_SliceWithStruct_Unsafe_Pcopy-8       \t 1395982\t       860.3 ns/op\nBenchmark_SliceWithStruct_miniCopy-8           \t  163154\t      7298 ns/op\nBenchmark_SliceWithStruct_Reflect_Pcopy-8      \t  190728\t      6213 ns/op\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fantlabs%2Fpcopy","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fantlabs%2Fpcopy","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fantlabs%2Fpcopy/lists"}