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

IArray: from_iter optimization for empty arrays #48

Merged
merged 6 commits into from Nov 20, 2023

Conversation

cecton
Copy link
Member

@cecton cecton commented Nov 19, 2023

This will optimize creation of IArray from an empty iterator.

Also added a constant IArray<T>::EMPTY.

@kirillsemyonkin
Copy link
Collaborator

Does Rust not optimize &[] to 0? Wonder if profiling before doing this is needed, if its only for optimization and not for making API nicer.

@cecton
Copy link
Member Author

cecton commented Nov 19, 2023

hmm it's true that a collection of 0 elements collected to a vec will not allocate. But it does make an Rc for nothing I guess

Copy link
Collaborator

@kirillsemyonkin kirillsemyonkin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we have no quick way to benchmark these changes, it is fine then lol

@cecton
Copy link
Member Author

cecton commented Nov 19, 2023

I copy-pasted the divan thingy from yew to see and oh wow it's terribly faster:

After:

vnode                           fastest       │ slowest       │ median        │ mean          │ samples │ iters
╰─ from_iter_empty_collections  2.919 ns      │ 4.163 ns      │ 2.933 ns      │ 2.944 ns      │ 100     │ 1000000000

Before:

vnode                           fastest       │ slowest       │ median        │ mean          │ samples │ iters
╰─ from_iter_empty_collections  16.53 ns      │ 18.01 ns      │ 16.76 ns      │ 16.77 ns      │ 100     │ 1000000000

(I need to rename the test file I know :D)

@cecton
Copy link
Member Author

cecton commented Nov 19, 2023

The one for single element is also impressive:

Before:

Timer precision: 30 ns
vnode                                        fastest       │ slowest       │ median        │ mean          │ samples │ iters
├─ from_iter_collection_with_single_element  24.04 ns      │ 25.43 ns      │ 24.2 ns       │ 24.21 ns      │ 100     │ 1000000000
╰─ from_iter_empty_collections               15.4 ns       │ 16.27 ns      │ 15.71 ns      │ 15.81 ns      │ 100     │ 1000000000

After:

Timer precision: 30 ns
array                                        fastest       │ slowest       │ median        │ mean          │ samples │ iters
├─ from_iter_collection_with_single_element  9.718 ns      │ 12.57 ns      │ 9.756 ns      │ 9.787 ns      │ 100     │ 1000000000
╰─ from_iter_empty_collections               3.142 ns      │ 3.153 ns      │ 3.15 ns       │ 3.15 ns       │ 100     │ 1000000000

@cecton
Copy link
Member Author

cecton commented Nov 19, 2023

I just noticed that Rc has a from_iter to Rc<[T]>. This should also avoid one allocation.

array                                        fastest       │ slowest       │ median        │ mean          │ samples │ iters
├─ from_iter_collection_with_single_element  19.36 ns      │ 21.4 ns       │ 19.49 ns      │ 19.51 ns      │ 100     │ 1000000000
╰─ from_iter_empty_collections               12.11 ns      │ 12.2 ns       │ 12.16 ns      │ 12.16 ns      │ 100     │ 1000000000

(The others are better but it's good to have it for collections that have more than 1 items!)

@cecton
Copy link
Member Author

cecton commented Nov 19, 2023

@kirillsemyonkin I don't really want to do all the CI stuff for now to display a comment with the benchmark -_- but I'm already happy with the results of the benchmarks. If it's good for you I will merge.

@cecton cecton merged commit c95b92e into yewstack:main Nov 20, 2023
12 checks passed
@cecton cecton deleted the optimize-from-iter-empty-collection branch November 20, 2023 12:15
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 this pull request may close these issues.

None yet

2 participants