Repository: DioxusLabs/dioxus
File: packages/core/src/nodes.rs (Rust)

Code before:
    ///
    /// These will be one segment shorter than the path sent to the renderer since those paths are relative to the
    /// topmost element, not the `roots` field.
    #[cfg_attr(
        feature = "serialize",
        serde(deserialize_with = "deserialize_bytes_leaky")
    )]
    pub node_paths: StaticPathArray,

    /// The paths of each dynamic attribute relative to the root of the template
    ///
    /// These will be one segment shorter than the path sent to the renderer since those paths are relative to the
    /// topmost element, not the `roots` field.
    #[cfg_attr(
        feature = "serialize",
        serde(deserialize_with = "deserialize_bytes_leaky", bound = "")
    )]
    pub attr_paths: StaticPathArray,
}

impl std::hash::Hash for Template {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        // If debug assertions are enabled, rust is probably built with a lower
        // optimization level which will not merge identical statics. If it is,
        // then we hash by contents
        if cfg!(debug_assertions) {
            self.roots.hash(state);
            self.node_paths.hash(state);
            self.attr_paths.hash(state);
        }
        // Otherwise, we hash by pointers
        else {
            std::ptr::hash(self.roots as *const _, state);
            std::ptr::hash(self.node_paths as *const _, state);
            std::ptr::hash(self.attr_paths as *const _, state);
        }
    }
}

impl PartialEq for Template {
    fn eq(&self, other: &Self) -> bool {
        // If debug assertions are enabled, rust is probably built with a lower
        // optimization level which will not merge identical statics. If it is,
        // then we compare by contents
        if cfg!(debug_assertions) {
            self.roots == other.roots
                && self.node_paths == other.node_paths
                && self.attr_paths == other.attr_paths
        }
        // Otherwise, we compare by pointers
        else {

Review comment (bug):
I think `debug_assertions` is the closest we can get to checking if that optimization is likely active, but it can fail in some cases like if you disable `debug_assertions` in debug mode for faster build times

Code after:
    ///
    /// These will be one segment shorter than the path sent to the renderer since those paths are relative to the
    /// topmost element, not the `roots` field.
    #[cfg_attr(
        feature = "serialize",
        serde(deserialize_with = "deserialize_bytes_leaky")
    )]
    pub node_paths: StaticPathArray,

    /// The paths of each dynamic attribute relative to the root of the template
    ///
    /// These will be one segment shorter than the path sent to the renderer since those paths are relative to the
    /// topmost element, not the `roots` field.
    #[cfg_attr(
        feature = "serialize",
        serde(deserialize_with = "deserialize_bytes_leaky", bound = "")
    )]
    pub attr_paths: StaticPathArray,
}

// Are identical static items merged in the current build. Rust doesn't have a cfg(merge_statics) attribute
// so we have to check this manually
fn static_items_merged() -> bool {
    fn a() {}
    fn b() {}

    a as fn() == b as fn()
}

impl std::hash::Hash for Template {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        // If identical static items are merged, we can compare templates by pointer
        if static_items_merged() {
            std::ptr::hash(self.roots as *const _, state);
            std::ptr::hash(self.node_paths as *const _, state);
            std::ptr::hash(self.attr_paths as *const _, state);
        }
        // Otherwise, we hash by value
        else {
            self.roots.hash(state);
            self.node_paths.hash(state);
            self.attr_paths.hash(state);
        }
    }
}

impl PartialEq for Template {
    fn eq(&self, other: &Self) -> bool {
        // If identical static items are merged, we can compare templates by pointer
        if static_items_merged() {
            std::ptr::eq(self.roots as *const _, other.roots as *const _)