Retire pruned cache (#3573)

* use range beneath subtree for efficient is_pruned check
This commit is contained in:
Antioch Peverell
2021-02-23 19:34:32 +00:00
committed by GitHub
parent 08523b2b39
commit 57f4592499
4 changed files with 53 additions and 32 deletions
+18 -4
View File
@@ -12,8 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
use std::marker;
use std::u64;
use std::{marker, ops::Range, u64};
use croaring::Bitmap;
@@ -541,10 +540,18 @@ pub fn peak_map_height(mut pos: u64) -> (u64, u64) {
/// index. This function is the base on which all others, as well as the MMR,
/// are built.
pub fn bintree_postorder_height(num: u64) -> u64 {
if num == 0 {
let mut pos = num.saturating_sub(1);
if pos == 0 {
return 0;
}
peak_map_height(num - 1).1
let mut peak_size = ALL_ONES >> pos.leading_zeros();
while peak_size != 0 {
if pos >= peak_size {
pos -= peak_size;
}
peak_size >>= 1;
}
pos
}
/// Is this position a leaf in the MMR?
@@ -658,3 +665,10 @@ pub fn bintree_leftmost(num: u64) -> u64 {
let height = bintree_postorder_height(num);
num + 2 - (2 << height)
}
/// All pos in the subtree beneath the provided root, including root itself.
pub fn bintree_range(num: u64) -> Range<u64> {
let height = bintree_postorder_height(num);
let leftmost = num + 2 - (2 << height);
leftmost..(num + 1)
}