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

[FEATURE] Harness Sonic's speed and PreTouch for deep copying objects with Copy method #729

Open
scr-oath opened this issue Jan 11, 2025 · 2 comments

Comments

@scr-oath
Copy link

Some ad hoc testing shows that using something like this for deep copy beats many of the libraries that are actually built for this purpose (by a factor of at least 20 for github.com/mohae/deepcopy but over close to 100 for others). I know benchmarking is tricky but I do believe that sonic is quite performant, and I wonder if using it for copy can be faster still if the interim json representation can be skipped.

I would love to see a copy method in some form func Copy(src, dst any) err seems like a reasonable pattern.

Alternatively, is there any support for transforming to/from ast.Node from/to interfaces? (it looked like getting the ast.Node was only possible from json []byte via sonic.Get, and from it using node.MarshalJson()).

func BenchmarkSonic(b *testing.B) {
	for i := 0; i < b.N; i++ {
		data, err := sonic.Marshal(auctionResponse)
		require.NoError(b, err)

		var to *hookstage.AuctionResponsePayload
		require.NoError(b, sonic.Unmarshal(data, &to))

		require.NotSame(b, auctionResponse.BidResponse, to.BidResponse)
	}
}
@scr-oath
Copy link
Author

It may be that ast's intention to be "self-contained" means that it doesn't benefit from all of the Pretouch and asm optimizations… in any case, some mechanism of copying without serialization would be really helpful, and most likely the fastest mechanism available.

@AsterDY
Copy link
Collaborator

AsterDY commented Jan 13, 2025

what's the purpose of deep-copying? ast.Node uses pass-by-reference syntax to keep efficient.

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

2 participants