From e0fc49b5123fa89a5c12d7ce06687aaf4096d0c5 Mon Sep 17 00:00:00 2001 From: Travis Turner Date: Mon, 15 Jan 2018 19:59:14 -0600 Subject: [PATCH] reset lastContainer cache on Put to a mapped container. check lastContainer cache on Get --- roaring/containers_btree.go | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/roaring/containers_btree.go b/roaring/containers_btree.go index ce78c278a..a97eaed57 100644 --- a/roaring/containers_btree.go +++ b/roaring/containers_btree.go @@ -24,15 +24,28 @@ type BTreeContainers struct { } func (btc *BTreeContainers) Get(key uint64) *container { + // Check the last* cache for same container. + if key == btc.lastKey && btc.lastContainer != nil { + return btc.lastContainer + } + var c *container el, ok := btc.tree.Get(key) if ok { c = el.(*container) + btc.lastKey = key + btc.lastContainer = c } return c } func (btc *BTreeContainers) Put(key uint64, c *container) { + // If a mapped container is added to the tree, reset the + // lastContainer cache so that the cache is not pointing + // at a read-only mmap. + if c.mapped { + btc.lastContainer = nil + } btc.tree.Set(key, c) } @@ -73,7 +86,6 @@ func (btc *BTreeContainers) Clone() Containers { } nbtc.tree.Set(k, v.(*container).clone()) } - return nbtc }