prsqlite icon indicating copy to clipboard operation
prsqlite copied to clipboard

conditional branch from btree page header size

Open kawasin73 opened this issue 2 years ago • 0 comments

sqlite computes the b-tree page header by ternary operator.

first = hdr + ((flags&PTF_LEAF)==0 ? 12 : 8);

https://github.com/sqlite/sqlite/blob/e17cac189fb40f4b1ccca04e54bf8163de555ae3/src/btree.c#L2162

But we can compute it without conditional branch.

    /// The btree page header size.
    ///
    /// * Returns 8 if this is a leaf page.
    /// * Returns 12 if this is an interior page.
    ///
    /// This does not invoke conditional branch.
    pub fn header_size(&self) -> u8 {
        // 0(leaf) or 8(interior)
        let is_interior = (!*self.pagetype()) & LEAF_FLAG;
        // 0(leaf) or 4(interior)
        let additional_size = is_interior >> 1;
        8 + additional_size
    }

Compare the two in terms of binary size and performance.

kawasin73 avatar Feb 20 '23 10:02 kawasin73