@@ -341,38 +341,77 @@ impl EncodingFile {
341341 }
342342
343343 /// Find encoding key for a content key
344+ ///
345+ /// Uses binary search on the page index to find the candidate page,
346+ /// then linear scan within that page.
344347 pub fn find_encoding ( & self , content_key : & ContentKey ) -> Option < EncodingKey > {
345- // Binary search through pages to find the right page
346- for page in & self . ckey_pages {
347- for entry in & page. entries {
348- if entry. content_key == * content_key {
349- return entry. encoding_keys . first ( ) . copied ( ) ;
350- }
348+ let key_bytes = * content_key. as_bytes ( ) ;
349+
350+ // Binary search through the page index to find the right page.
351+ // Each page's first_key is the lowest key in that page, and pages
352+ // are sorted. We find the last page whose first_key <= our key.
353+ let page_idx = self
354+ . ckey_index
355+ . partition_point ( |idx| idx. first_key <= key_bytes) ;
356+
357+ // partition_point returns the first index where first_key > key,
358+ // so the candidate page is at page_idx - 1
359+ if page_idx == 0 {
360+ return None ;
361+ }
362+
363+ let page = & self . ckey_pages [ page_idx - 1 ] ;
364+ for entry in & page. entries {
365+ if entry. content_key == * content_key {
366+ return entry. encoding_keys . first ( ) . copied ( ) ;
351367 }
352368 }
353369 None
354370 }
355371
356372 /// Find all encoding keys for a given content key
373+ ///
374+ /// Uses binary search on the page index to find the candidate page,
375+ /// then linear scan within that page.
357376 pub fn find_all_encodings ( & self , content_key : & ContentKey ) -> Vec < EncodingKey > {
358- for page in & self . ckey_pages {
359- for entry in & page. entries {
360- if entry. content_key == * content_key {
361- return entry. encoding_keys . clone ( ) ;
362- }
377+ let key_bytes = * content_key. as_bytes ( ) ;
378+
379+ let page_idx = self
380+ . ckey_index
381+ . partition_point ( |idx| idx. first_key <= key_bytes) ;
382+
383+ if page_idx == 0 {
384+ return Vec :: new ( ) ;
385+ }
386+
387+ let page = & self . ckey_pages [ page_idx - 1 ] ;
388+ for entry in & page. entries {
389+ if entry. content_key == * content_key {
390+ return entry. encoding_keys . clone ( ) ;
363391 }
364392 }
365393 Vec :: new ( )
366394 }
367395
368396 /// Find `ESpec` for an encoding key
397+ ///
398+ /// Uses binary search on the page index to find the candidate page,
399+ /// then linear scan within that page.
369400 pub fn find_espec ( & self , encoding_key : & EncodingKey ) -> Option < & str > {
370- // Search through EKey pages
371- for page in & self . ekey_pages {
372- for entry in & page. entries {
373- if entry. encoding_key == * encoding_key {
374- return self . espec_table . get ( entry. espec_index ) ;
375- }
401+ let key_bytes = * encoding_key. as_bytes ( ) ;
402+
403+ let page_idx = self
404+ . ekey_index
405+ . partition_point ( |idx| idx. first_key <= key_bytes) ;
406+
407+ if page_idx == 0 {
408+ return None ;
409+ }
410+
411+ let page = & self . ekey_pages [ page_idx - 1 ] ;
412+ for entry in & page. entries {
413+ if entry. encoding_key == * encoding_key {
414+ return self . espec_table . get ( entry. espec_index ) ;
376415 }
377416 }
378417 None
0 commit comments