-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Uint8List.fromList([...])
is ~10x slower than Uint8List(length)..[0] = #..[1] = #
#56052
Comments
Labels: |
It's unsurprising that 10x seems excessive, but that may just be beause (The obvious optimization would be to optimize away the intermediate list so that |
Doesn't seem to be list allocation that costs. Changing the list to a A quick look at the code shows that the definition of @mkustermann Typed data, there's always more to optimize! :) |
For a 10-element list we are already far down the slippery slope of having too many tests to select special cases to close the final x3. I think the language needs something to help us dispatch more quickly to the appropriate specialized kernel. I would like the full glory of control flow collections to be available to typed data lists, with both modifiable, unmodifiable, and perhaps |
Indeed, the only thing here that would match the direct storing approach would be recognizing the pattern A typed-data literal, fx (I'd like the feature to be available to user types too, maybe requiring the type to have a special constructor or interface, we can still special-case platform types.) |
Right now this compiles to this:
Without special compiler magic for the particular use (i.e. relying on normal compiler optimizations), the compiler would need to
This is too much to ask from the compiler. We could recognize this particular pattern higher up in the stack (e.g. kernel level) and rewrite it there. That would probably be ok, but not ideal as we introduce hand-crafted optimizations for library features where we depend on knowing the library implementation details. Ideally we'd want to allow developers expressing the concept of bytes in the language - via const and non-const typed data literals. That would mostly solve this case.
Just using a top-level |
@matanlurey is your benchmark inspired by some pattern that is used frequently in Flutter framework code ? |
Nothing specific - it was just surprising to me that what seemed like the easiest way to create a list of bytes was the least performant. |
Possibly related: #32080.
It would be nice to have an intrinsic for a list-literal or perhaps document
fromList
as inefficient.Here is a micro-benchmark I wrote:
The text was updated successfully, but these errors were encountered: