@@ -111,22 +111,25 @@ func IntersectCompressedWithBin(dec *codec.Decoder, q []uint64, o *[]uint64) {
111111 if len (blockUids ) == 0 {
112112 break
113113 }
114- if ld * 10 < lq {
115- IntersectWithBin (blockUids , q , o )
114+ if ld * 10 < len (q ) {
115+ q = q [IntersectWithBin (blockUids , q , o ):]
116+ dec .Next ()
116117 } else {
117118 // For small enough difference between two arrays, we should just
118119 // do lin intersect
119- IntersectWithLin (blockUids , q , o )
120+ _ , off := IntersectWithLin (blockUids , q , o )
121+ if off == 0 {
122+ off = 1
123+ }
124+ if off == len (q ) {
125+ return
126+ }
127+ q = q [off :]
128+ if len (q ) == 0 {
129+ return
130+ }
131+ dec .Seek (q [0 ], codec .SeekStart )
120132 }
121- lastUid := blockUids [len (blockUids )- 1 ]
122- qidx := sort .Search (len (q ), func (idx int ) bool {
123- return q [idx ] >= lastUid
124- })
125- if qidx >= len (q ) {
126- return
127- }
128- q = q [qidx :]
129- dec .Next ()
130133 }
131134 return
132135 }
@@ -242,7 +245,8 @@ func IntersectWithJump(u, v []uint64, o *[]uint64) (int, int) {
242245// IntersectWithBin is based on the paper
243246// "Fast Intersection Algorithms for Sorted Sequences"
244247// https://link.springer.com/chapter/10.1007/978-3-642-12476-1_3
245- func IntersectWithBin (d , q []uint64 , o * []uint64 ) {
248+ // Returns where to move the second array(q) to. O means not found
249+ func IntersectWithBin (d , q []uint64 , o * []uint64 ) int {
246250 ld := len (d )
247251 lq := len (q )
248252
@@ -251,7 +255,7 @@ func IntersectWithBin(d, q []uint64, o *[]uint64) {
251255 d , q = q , d
252256 }
253257 if ld == 0 || lq == 0 || d [ld - 1 ] < q [0 ] || q [lq - 1 ] < d [0 ] {
254- return
258+ return 0
255259 }
256260
257261 val := d [0 ]
@@ -265,6 +269,7 @@ func IntersectWithBin(d, q []uint64, o *[]uint64) {
265269 })
266270
267271 binIntersect (d , q [minq :maxq ], o )
272+ return maxq
268273}
269274
270275// binIntersect is the recursive function used.
0 commit comments