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

Impl Bake for BTreeMap #4274

Merged

Conversation

adamchalmers
Copy link
Contributor

@adamchalmers adamchalmers commented Nov 13, 2023

Part of #4266

@sffc
Copy link
Member

sffc commented Nov 13, 2023

Is there a more efficient way to construct this than From<&[(K, V)]>?

@adamchalmers
Copy link
Contributor Author

adamchalmers commented Nov 13, 2023

The implementation could use this instead:

    fn bake(&self, ctx: &CrateEnv) -> TokenStream {
        ctx.insert("alloc");
        let data = self.iter().map(|(k, v)| {
            let k = k.bake(ctx);
            let v = v.bake(ctx);
            quote!((#k, #v))
        });
        quote! {
            {
                let mut map = alloc::collections::BTreeMap::new();
                for (k, v) in [#(#data),*] {
                    map.insert(k, v);
                }
                map
            }
        }
    }

but I suspect that's actually less efficient. I don't see any benchmarks for databake, maybe I could add one.

Copy link
Member

@sffc sffc left a comment

Choose a reason for hiding this comment

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

Yeah, in theory there should be a way to get an O(N) construction of BTreeMap if we know that the elements are sorted and how many elements there are, but From is likely the most efficient construction mechanism that is currently stable, and we can improve it later if Rust gives us such an API in the future.

Copy link
Member

@Manishearth Manishearth left a comment

Choose a reason for hiding this comment

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

Ideally this PR would also contain a changelog entry

@robertbastian robertbastian merged commit e7178ec into unicode-org:main Nov 14, 2023
29 checks passed
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.

4 participants