@@ -112,8 +112,6 @@ pub struct SerializableInterval {
112112 metadata : QueryMetadata ,
113113}
114114
115- // Align ranges to cache line boundary for better memory access
116- #[ repr( align( 64 ) ) ]
117115#[ derive( Debug , Default , Clone ) ]
118116pub struct SortedRanges {
119117 pub ranges : Vec < ( i32 , i32 ) >
@@ -142,22 +140,22 @@ impl SortedRanges {
142140 } else {
143141 ( new_range. 1 , new_range. 0 )
144142 } ;
145-
143+
146144 // Return regions that don't overlap with existing ranges
147145 let mut non_overlapping = Vec :: new ( ) ;
148146 let mut current = start;
149-
147+
150148 // Find the first range that could overlap
151149 let mut i = match self . ranges . binary_search_by_key ( & start, |& ( s, _) | s) {
152150 Ok ( pos) => pos,
153151 Err ( pos) => pos,
154152 } ;
155-
153+
156154 // Check previous range for overlap
157155 if i > 0 && self . ranges [ i - 1 ] . 1 > start {
158156 i -= 1 ;
159157 }
160-
158+
161159 // Process all potentially overlapping ranges
162160 while i < self . ranges . len ( ) && current < end {
163161 let ( range_start, range_end) = self . ranges [ i] ;
@@ -170,11 +168,11 @@ impl SortedRanges {
170168 current = max ( current, range_end) ;
171169 i += 1 ;
172170 }
173-
171+
174172 if current < end {
175173 non_overlapping. push ( ( current, end) ) ;
176174 }
177-
175+
178176 // Now insert the range while maintaining sorted order and merging overlaps
179177 match self . ranges . binary_search_by_key ( & start, |& ( s, _) | s) {
180178 Ok ( pos) | Err ( pos) => {
@@ -186,12 +184,12 @@ impl SortedRanges {
186184 self . ranges [ pos] . 0 = min ( start, self . ranges [ pos] . 0 ) ;
187185 self . ranges [ pos] . 1 = max ( end, self . ranges [ pos] . 1 ) ;
188186 self . merge_forward_from ( pos) ;
189- } else {
187+ } else {
190188 self . ranges . insert ( pos, ( start, end) ) ;
191189 }
192190 }
193191 }
194-
192+
195193 non_overlapping
196194 }
197195
@@ -432,7 +430,21 @@ impl Impg {
432430 }
433431 }
434432 } ) ;
435- }
433+
434+ // Merge contiguous/overlapping ranges with same sequence_id
435+ stack. sort_by_key ( |( id, start, _) | ( * id, * start) ) ;
436+ let mut write = 0 ;
437+ for read in 1 ..stack. len ( ) {
438+ if stack[ write] . 0 == stack[ read] . 0 && // Same sequence_id
439+ stack[ write] . 2 >= stack[ read] . 1 { // Overlapping or contiguous
440+ stack[ write] . 2 = stack[ write] . 2 . max ( stack[ read] . 2 ) ;
441+ } else {
442+ write += 1 ;
443+ stack. swap ( write, read) ;
444+ }
445+ }
446+ stack. truncate ( write + 1 ) ;
447+ }
436448 }
437449
438450 results
0 commit comments