@@ -85,6 +85,13 @@ enum IDLState {
85
85
}
86
86
87
87
impl IDLState {
88
+ fn shrink_to_fit ( & mut self ) {
89
+ match self {
90
+ IDLState :: Sparse ( svec) => svec. shrink_to_fit ( ) ,
91
+ IDLState :: Compressed ( vec) => vec. shrink_to_fit ( ) ,
92
+ }
93
+ }
94
+
88
95
fn sparse_bitand_fast_path ( smol : & [ u64 ] , lrg : & [ u64 ] ) -> Self {
89
96
let mut nlist = SmallVec :: with_capacity ( smol. len ( ) ) ;
90
97
// We cache the idx inbetween to narrow the bst sizes.
@@ -99,6 +106,8 @@ impl IDLState {
99
106
idx_min = idx;
100
107
}
101
108
} ) ;
109
+
110
+ nlist. shrink_to_fit ( ) ;
102
111
IDLState :: Sparse ( nlist)
103
112
}
104
113
@@ -120,6 +129,7 @@ impl IDLState {
120
129
}
121
130
} ) ;
122
131
132
+ nlist. shrink_to_fit ( ) ;
123
133
IDLState :: Sparse ( nlist)
124
134
}
125
135
}
@@ -201,7 +211,7 @@ impl Default for IDLBitRange {
201
211
/// Construct a new, empty set.
202
212
fn default ( ) -> Self {
203
213
IDLBitRange {
204
- state : IDLState :: Sparse ( SmallVec :: new ( ) ) ,
214
+ state : IDLState :: Sparse ( SmallVec :: with_capacity ( 0 ) ) ,
205
215
}
206
216
}
207
217
}
@@ -334,8 +344,8 @@ impl IDLBitRange {
334
344
let range: u64 = id - bvalue;
335
345
336
346
if let Some ( last) = list. last_mut ( ) {
337
- debug_assert ! ( id >= ( * last) . range) ;
338
- if ( * last) . range == range {
347
+ debug_assert ! ( id >= last. range) ;
348
+ if last. range == range {
339
349
// Insert the bit.
340
350
( * last) . push_id ( bvalue) ;
341
351
return ;
@@ -366,7 +376,7 @@ impl IDLBitRange {
366
376
let r = list. binary_search ( & candidate) ;
367
377
match r {
368
378
Ok ( idx) => {
369
- let mut existing = list. get_mut ( idx) . unwrap ( ) ;
379
+ let existing = list. get_mut ( idx) . unwrap ( ) ;
370
380
existing. mask |= candidate. mask ;
371
381
}
372
382
Err ( idx) => {
@@ -402,7 +412,7 @@ impl IDLBitRange {
402
412
//
403
413
// To do this, we not the candidate, so all other bits remain,
404
414
// then we perform and &= so that the existing bits survive.
405
- let mut existing = list. get_mut ( idx) . unwrap ( ) ;
415
+ let existing = list. get_mut ( idx) . unwrap ( ) ;
406
416
407
417
existing. mask &= !candidate. mask ;
408
418
@@ -422,7 +432,7 @@ impl IDLBitRange {
422
432
if self . is_compressed ( ) {
423
433
return ;
424
434
}
425
- let mut prev_state = IDLState :: Compressed ( Vec :: new ( ) ) ;
435
+ let mut prev_state = IDLState :: Compressed ( Vec :: with_capacity ( 0 ) ) ;
426
436
std:: mem:: swap ( & mut prev_state, & mut self . state ) ;
427
437
match prev_state {
428
438
IDLState :: Sparse ( list) => list. into_iter ( ) . for_each ( |i| unsafe {
@@ -441,14 +451,15 @@ impl IDLBitRange {
441
451
None
442
452
} else {
443
453
let mut maybe = IDLBitRange {
444
- state : IDLState :: Compressed ( Vec :: new ( ) ) ,
454
+ state : IDLState :: Compressed ( Vec :: with_capacity ( 0 ) ) ,
445
455
} ;
446
456
list. iter ( ) . for_each ( |id| unsafe { maybe. push_id ( * id) } ) ;
447
457
448
458
if maybe. len_ranges ( ) > 0
449
459
&& ( maybe. len ( ) / maybe. len_ranges ( ) ) >= AVG_RANGE_COMP_REQ
450
460
{
451
- let IDLBitRange { state } = maybe;
461
+ let IDLBitRange { mut state } = maybe;
462
+ state. shrink_to_fit ( ) ;
452
463
Some ( state)
453
464
} else {
454
465
None
@@ -507,6 +518,9 @@ impl IDLBitRange {
507
518
}
508
519
}
509
520
}
521
+
522
+ nlist. shrink_to_fit ( ) ;
523
+
510
524
IDLState :: Sparse ( nlist)
511
525
} ;
512
526
@@ -540,12 +554,14 @@ impl IDLBitRange {
540
554
}
541
555
} ) ;
542
556
557
+ nlist. shrink_to_fit ( ) ;
558
+
543
559
IDLBitRange {
544
560
state : IDLState :: Sparse ( nlist) ,
545
561
}
546
562
}
547
563
( IDLState :: Compressed ( list1) , IDLState :: Compressed ( list2) ) => {
548
- let mut nlist = Vec :: new ( ) ;
564
+ let mut nlist = Vec :: with_capacity ( 0 ) ;
549
565
let mut liter = list1. iter ( ) ;
550
566
let mut riter = list2. iter ( ) ;
551
567
@@ -577,6 +593,7 @@ impl IDLBitRange {
577
593
if nlist. is_empty ( ) {
578
594
IDLBitRange :: new ( )
579
595
} else {
596
+ nlist. shrink_to_fit ( ) ;
580
597
IDLBitRange {
581
598
state : IDLState :: Compressed ( nlist) ,
582
599
}
@@ -636,6 +653,8 @@ impl IDLBitRange {
636
653
rnext = riter. next ( ) ;
637
654
}
638
655
656
+ nlist. shrink_to_fit ( ) ;
657
+
639
658
IDLState :: Sparse ( nlist)
640
659
} ;
641
660
@@ -672,7 +691,7 @@ impl IDLBitRange {
672
691
Ok ( idx) => {
673
692
debug_assert ! ( Ok ( idx + idx_min) == list. binary_search( & candidate) ) ;
674
693
let idx = idx + idx_min;
675
- let mut existing = list. get_mut ( idx) . unwrap ( ) ;
694
+ let existing = list. get_mut ( idx) . unwrap ( ) ;
676
695
existing. mask |= candidate. mask ;
677
696
debug_assert ! ( idx >= idx_min) ;
678
697
idx_min = idx;
@@ -689,6 +708,8 @@ impl IDLBitRange {
689
708
} ;
690
709
} ) ;
691
710
711
+ list. shrink_to_fit ( ) ;
712
+
692
713
IDLBitRange {
693
714
state : IDLState :: Compressed ( list) ,
694
715
}
@@ -743,6 +764,9 @@ impl IDLBitRange {
743
764
nlist. push ( newrange) ;
744
765
rnextrange = riter. next ( ) ;
745
766
}
767
+
768
+ nlist. shrink_to_fit ( ) ;
769
+
746
770
IDLBitRange {
747
771
state : IDLState :: Compressed ( nlist) ,
748
772
}
@@ -788,6 +812,8 @@ impl IDLBitRange {
788
812
lnext = liter. next ( ) ;
789
813
}
790
814
815
+ nlist. shrink_to_fit ( ) ;
816
+
791
817
IDLBitRange {
792
818
state : IDLState :: Sparse ( nlist) ,
793
819
}
@@ -832,6 +858,8 @@ impl IDLBitRange {
832
858
}
833
859
} ) ;
834
860
861
+ nlist. shrink_to_fit ( ) ;
862
+
835
863
IDLBitRange {
836
864
state : IDLState :: Sparse ( nlist) ,
837
865
}
@@ -850,7 +878,7 @@ impl IDLBitRange {
850
878
if let Ok ( idx) = partition. binary_search ( & candidate) {
851
879
debug_assert ! ( Ok ( idx + idx_min) == nlist. binary_search( & candidate) ) ;
852
880
let idx = idx + idx_min;
853
- let mut existing = nlist. get_mut ( idx) . unwrap ( ) ;
881
+ let existing = nlist. get_mut ( idx) . unwrap ( ) ;
854
882
existing. mask &= !candidate. mask ;
855
883
if existing. mask == 0 {
856
884
nlist. remove ( idx) ;
@@ -859,6 +887,9 @@ impl IDLBitRange {
859
887
idx_min = idx;
860
888
}
861
889
} ) ;
890
+
891
+ nlist. shrink_to_fit ( ) ;
892
+
862
893
IDLBitRange {
863
894
state : IDLState :: Compressed ( nlist) ,
864
895
}
@@ -908,6 +939,8 @@ impl IDLBitRange {
908
939
lnextrange = liter. next ( ) ;
909
940
}
910
941
942
+ nlist. shrink_to_fit ( ) ;
943
+
911
944
IDLBitRange {
912
945
state : IDLState :: Compressed ( nlist) ,
913
946
}
@@ -943,7 +976,10 @@ impl FromIterator<u64> for IDLBitRange {
943
976
}
944
977
} ) ;
945
978
946
- new_sparse. maybe_compress ( ) ;
979
+ if !new_sparse. maybe_compress ( ) {
980
+ // If the compression didn't occur, trim the vec anyway.
981
+ new_sparse. state . shrink_to_fit ( ) ;
982
+ }
947
983
new_sparse
948
984
}
949
985
}
0 commit comments