@@ -720,18 +720,57 @@ int get_extent_cache_chunks(unifyfs_extent_t* extent,
720720 /* search cache array for extents containing start and end offset */
721721 extent_metadata * em_begin = NULL ;
722722 extent_metadata * em_end = NULL ;
723- for (int i = 0 ; i < (int )cache_sz ; i ++ ) {
723+
724+ /* bisection search for extent containing start offset */
725+ int start_ndx = 0 ;
726+ int end_ndx = (int )cache_sz - 1 ;
727+ int search_ndx ;
728+ int last_ndx = -1 ;
729+ //LOGDBG("bisection search of extent cache array (sz=%zu)", cache_sz);
730+ do {
731+ // look at extent halfway between start and end indices
732+ search_ndx = start_ndx + ((end_ndx - start_ndx ) >> 1 );
733+ if (search_ndx < cache_sz ) {
734+ if (last_ndx == search_ndx ) break ;
735+ last_ndx = search_ndx ;
736+ //LOGDBG("checking extent at index=%d", search_ndx);
737+ extent_metadata * em = cache + search_ndx ;
738+ if (em -> start <= ext_start_off ) {
739+ if (em -> end >= ext_start_off ) {
740+ //LOGDBG("found start extent at index=%d", search_ndx);
741+ em_begin = em ;
742+ start_ndx = search_ndx ;
743+ break ;
744+ } else { // go forward
745+ //LOGDBG("searching forward");
746+ start_ndx = search_ndx + 1 ;
747+ }
748+ } else { // go back
749+ //LOGDBG("searching backward");
750+ end_ndx = search_ndx ;
751+ }
752+ } else {
753+ LOGERR ("search went out of range, index=%d" , search_ndx );
754+ start_ndx = 0 ;
755+ break ;
756+ }
757+ } while (1 );
758+
759+ /* linear search from start for extent containing end offset */
760+ for (int i = start_ndx ; i < (int )cache_sz ; i ++ ) {
724761 extent_metadata * em = cache + i ;
725- if ((NULL == em_begin ) &&
726- (em -> start <= ext_start_off ) &&
727- (em -> end >= ext_start_off )) {
728- em_begin = em ;
762+ if (NULL == em_begin ) { // in case not found above
763+ if ((em -> start <= ext_start_off ) &&
764+ (em -> end >= ext_start_off )) {
765+ em_begin = em ;
766+ }
729767 }
730- if ((NULL == em_end ) &&
731- (em -> start <= ext_end_off ) &&
732- (em -> end >= ext_end_off )) {
733- em_end = em ;
734- break ;
768+ if (NULL == em_end ) {
769+ if ((em -> start <= ext_end_off ) &&
770+ (em -> end >= ext_end_off )) {
771+ em_end = em ;
772+ break ;
773+ }
735774 }
736775 }
737776
0 commit comments