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 issue of slice() function #24

Closed
twoeths opened this issue Jul 25, 2022 · 3 comments
Closed

Performance issue of slice() function #24

twoeths opened this issue Jul 25, 2022 · 3 comments

Comments

@twoeths
Copy link

twoeths commented Jul 25, 2022

Profiling from ChainSafe/lodestar#4114 shows a significant time was used for Uint8Array memory allocation

With a 5-minute profile, it takes 16% of cpu time just for this function.

This is mostly called form "slice()` function as shown in libp2p/js-libp2p#1303

@achingbrain
Copy link
Owner

@tuyennhv what sort of size of length-prefixed data are you seeing consume all this time during your profiling? Is it a few bytes on average or more like megabytes?

@twoeths
Copy link
Author

twoeths commented Jul 27, 2022

@tuyennhv what sort of size of length-prefixed data are you seeing consume all this time during your profiling? Is it a few bytes on average or more like megabytes?

I'm not able to debug the length-prefixed data as uint8arraylist is used in different consumers, but the one thing I notice is >90% of the time slice() function spanning exactly 1 buffer and right now we do the copy of that 1 buffer unnecessarily in concat() function. If we do early return in that case (without calling concat of an array of 1 item) then I get through this issue.

In bl.slice api spec, it said that:

If the requested range spans a single internal buffer then a slice of that buffer will be returned which shares the original memory range of that Buffer. If the range spans multiple buffers then copy operations will likely occur to give you a uniform Buffer.

@achingbrain
Copy link
Owner

If we do early return in that case

This optimisation is now included in the .subarray method. It's in .subarray as that per Uint8Array semantics it should be no-copy (when possible), and not .slice as that should always be copy.

In bl.slice api spec, it said that:

bl follows semantics derived from node Buffer, this module follows semantics derived from Uint8Array which are ever-so-slightly-different-yet-also-completely-incompatible on this point.

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

Successfully merging a pull request may close this issue.

2 participants