diff --git a/libsql-ffi/bundled/SQLite3MultipleCiphers/src/sqlite3.c b/libsql-ffi/bundled/SQLite3MultipleCiphers/src/sqlite3.c index 62a917fb9b..0ae64a6b48 100644 --- a/libsql-ffi/bundled/SQLite3MultipleCiphers/src/sqlite3.c +++ b/libsql-ffi/bundled/SQLite3MultipleCiphers/src/sqlite3.c @@ -77513,6 +77513,78 @@ SQLITE_PRIVATE int sqlite3BtreeLast(BtCursor *pCur, int *pRes){ return btreeLast(pCur, pRes); } +#if defined(SQLITE_OMIT_FLOATING_POINT) && !defined(LIBSQL_OMIT_INTERP_SEEK) +# define LIBSQL_OMIT_INTERP_SEEK 1 /* the guess needs floating point */ +#endif +#ifndef LIBSQL_OMIT_INTERP_SEEK +/* +** libSQL: interpolation search inside table (rowid) b-tree pages. +** +** Binary search ignores the values of the keys it compares. Rowids are +** integers and are usually close to evenly spaced within a page, so the +** slot of a key can be predicted by linear interpolation between two +** "knots": an exclusive lower and an inclusive upper bound on the keys of +** the page. When descending from an interior page those knots are free: +** the cell that points at the child holds the child's largest rowid and +** its left neighbour bounds the child from below. The search on every +** page records the nearest keys it compared on each side of intKey, and +** those become the knots of the child it descends into. +** +** On a page with both knots and at least LIBSQL_INTERP_MIN_CELLS cells, +** the first probe goes to the interpolated slot, the next ones step away +** from it by 1, 2, 4, ... cells until intKey is bracketed, and the rest of +** the search is the usual binary search inside the bracket. The knots +** only pick probe positions, so the result is always exact and stale or +** corrupt separator keys only cost extra probes. The worst case is about +** twice the probes of a binary search. A page that lacks a knot (the +** root, and pages on the left or right edge of the tree) first probes its +** first or last cell to get one. +*/ +#ifndef LIBSQL_INTERP_MIN_CELLS +# define LIBSQL_INTERP_MIN_CELLS 16 +#endif + +/* +** Predict the slot of intKey on pPage by linear interpolation between the +** knots kLo and kHi, and clamp it to [lwr,upr], the cells of pPage that +** the search has not ruled out yet. +** +** The knots are the keys just outside [lwr,upr]. kLo is the key of cell +** lwr-1, or if lwr==0 the lower bound that the parent gave for this page. +** kHi is the key of cell upr+1 if the search has compared that cell. +** Otherwise upr is the last cell and kHi is the upper bound from the +** parent, which on a leaf is the key of the last cell itself and on an +** interior page belongs to the right-most child, one slot past the last +** cell. +** +** The caller guarantees kLo=1, so there is no division by zero, and the quotient +** lies between 0 and iHi-iLo. +*/ +static SQLITE_INLINE int btreeInterpGuess( + MemPage *pPage, /* The page being searched */ + i64 intKey, /* The rowid being sought */ + i64 kLo, i64 kHi, /* Knots: kLo < intKey <= kHi */ + int lwr, int upr /* Cells not yet ruled out */ +){ + u64 nOff = (u64)intKey - (u64)kLo; /* 1 <= nOff <= nKey */ + u64 nKey = (u64)kHi - (u64)kLo; + int iLo = lwr-1; /* Slot of kLo */ + int iHi = upr + (uprnCell-1 || !pPage->leaf); /* Slot of kHi */ + int i; + assert( kLo=0 && lwr<=upr && uprnCell ); + if( nKey>>63 ){ nOff >>= 1; nKey >>= 1; } /* Keep the casts signed */ + i = iLo + (int)((double)(i64)nOff * (double)(iHi-iLo) / (double)(i64)nKey); + if( iupr ) i = upr; + return i; +} +#endif /* LIBSQL_OMIT_INTERP_SEEK */ + /* Move the cursor so that it points to an entry in a table (a.k.a INTKEY) ** table near the key intKey. Return a success code. ** @@ -77543,6 +77615,15 @@ SQLITE_PRIVATE int sqlite3BtreeTableMoveto( int *pRes /* Write search results here */ ){ int rc; +#ifndef LIBSQL_OMIT_INTERP_SEEK + i64 kLo = 0, kHi = 0; /* Keys of the current page are in (kLo,kHi] */ + u8 bKnots = 0; /* 0x01: kLo is known and less than intKey. + ** 0x02: kHi is known and not less than intKey */ + int iGuess = 0; /* Interpolated slot on the current page */ + int nStep = 1; /* Distance of the next probe from iGuess */ + int eStep; /* 0: bisect. 1: step away from iGuess. + ** 2: probing an end cell for a missing knot */ +#endif assert( cursorOwnsBtShared(pCur) ); assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) ); @@ -77620,8 +77701,22 @@ SQLITE_PRIVATE int sqlite3BtreeTableMoveto( upr = pPage->nCell-1; assert( biasRight==0 || biasRight==1 ); idx = upr>>(1-biasRight); /* idx = biasRight ? upr : (lwr+upr)/2; */ +#ifndef LIBSQL_OMIT_INTERP_SEEK + eStep = 0; + if( !biasRight && upr>=LIBSQL_INTERP_MIN_CELLS-1 ){ + if( (bKnots & 0x01)==0 ){ + idx = 0; eStep = 2; /* first cell gives kLo */ + }else if( (bKnots & 0x02)==0 ){ + idx = upr; eStep = 2; /* last cell gives kHi */ + }else{ + idx = iGuess = btreeInterpGuess(pPage, intKey, kLo, kHi, 0, upr); + nStep = 1; eStep = 1; + } + } +#endif for(;;){ i64 nCellKey; + assert( idx>=lwr && idx<=upr ); /* only probe inside the bracket */ pCell = findCellPastPtr(pPage, idx); if( pPage->intKeyLeaf ){ while( 0x80 <= *(pCell++) ){ @@ -77633,14 +77728,27 @@ SQLITE_PRIVATE int sqlite3BtreeTableMoveto( getVarint(pCell, (u64*)&nCellKey); if( nCellKeyupr ){ c = -1; break; } }else if( nCellKey>intKey ){ upr = idx-1; +#ifndef LIBSQL_OMIT_INTERP_SEEK + kHi = nCellKey; +#endif if( lwr>upr ){ c = +1; break; } }else{ assert( nCellKey==intKey ); pCur->ix = (u16)idx; if( !pPage->leaf ){ +#ifndef LIBSQL_OMIT_INTERP_SEEK + /* The child holds keys in (key[idx-1], intKey]. kLo might be + ** an earlier, smaller key than key[idx-1]. That is harmless: + ** with kHi==intKey the guess is the child's last cell. */ + bKnots = (u8)((bKnots & 0x01) | (lwr>0) | 0x02); + kHi = nCellKey; +#endif lwr = idx; goto moveto_table_next_layer; }else{ @@ -77651,6 +77759,26 @@ SQLITE_PRIVATE int sqlite3BtreeTableMoveto( return SQLITE_OK; } } +#ifndef LIBSQL_OMIT_INTERP_SEEK + if( eStep ){ + if( eStep==2 ){ + /* Probed the first or last cell for a missing knot and did not + ** find intKey beyond it. If that was the first cell and kHi is + ** missing too, probe the last cell next. */ + if( idx==0 && (bKnots & 0x02)==0 ){ idx = upr; continue; } + idx = iGuess = btreeInterpGuess(pPage, intKey, kLo, kHi, lwr, upr); + nStep = 1; eStep = 1; + continue; + } + /* Step away from iGuess by 1, 2, 4, ... cells, towards intKey. + ** Once a step passes intKey, the bound it set puts the next step + ** outside [lwr,upr], and the binary search takes over. */ + idx = nCellKey=lwr && idx<=upr ) continue; + eStep = 0; + } +#endif assert( lwr+upr>=0 ); idx = (lwr+upr)>>1; /* idx = (lwr+upr)/2; */ } @@ -77663,6 +77791,11 @@ SQLITE_PRIVATE int sqlite3BtreeTableMoveto( rc = SQLITE_OK; goto moveto_table_finish; } +#ifndef LIBSQL_OMIT_INTERP_SEEK + /* The child holds keys in (key[lwr-1], key[lwr]]. A side that no + ** probe on this page bounded keeps this page's own knot, if any. */ + bKnots |= (u8)((lwr>0) | ((lwrnCell)<<1)); +#endif moveto_table_next_layer: if( lwr>=pPage->nCell ){ chldPg = get4byte(&pPage->aData[pPage->hdrOffset+8]); diff --git a/libsql-ffi/bundled/src/sqlite3.c b/libsql-ffi/bundled/src/sqlite3.c index 62a917fb9b..0ae64a6b48 100644 --- a/libsql-ffi/bundled/src/sqlite3.c +++ b/libsql-ffi/bundled/src/sqlite3.c @@ -77513,6 +77513,78 @@ SQLITE_PRIVATE int sqlite3BtreeLast(BtCursor *pCur, int *pRes){ return btreeLast(pCur, pRes); } +#if defined(SQLITE_OMIT_FLOATING_POINT) && !defined(LIBSQL_OMIT_INTERP_SEEK) +# define LIBSQL_OMIT_INTERP_SEEK 1 /* the guess needs floating point */ +#endif +#ifndef LIBSQL_OMIT_INTERP_SEEK +/* +** libSQL: interpolation search inside table (rowid) b-tree pages. +** +** Binary search ignores the values of the keys it compares. Rowids are +** integers and are usually close to evenly spaced within a page, so the +** slot of a key can be predicted by linear interpolation between two +** "knots": an exclusive lower and an inclusive upper bound on the keys of +** the page. When descending from an interior page those knots are free: +** the cell that points at the child holds the child's largest rowid and +** its left neighbour bounds the child from below. The search on every +** page records the nearest keys it compared on each side of intKey, and +** those become the knots of the child it descends into. +** +** On a page with both knots and at least LIBSQL_INTERP_MIN_CELLS cells, +** the first probe goes to the interpolated slot, the next ones step away +** from it by 1, 2, 4, ... cells until intKey is bracketed, and the rest of +** the search is the usual binary search inside the bracket. The knots +** only pick probe positions, so the result is always exact and stale or +** corrupt separator keys only cost extra probes. The worst case is about +** twice the probes of a binary search. A page that lacks a knot (the +** root, and pages on the left or right edge of the tree) first probes its +** first or last cell to get one. +*/ +#ifndef LIBSQL_INTERP_MIN_CELLS +# define LIBSQL_INTERP_MIN_CELLS 16 +#endif + +/* +** Predict the slot of intKey on pPage by linear interpolation between the +** knots kLo and kHi, and clamp it to [lwr,upr], the cells of pPage that +** the search has not ruled out yet. +** +** The knots are the keys just outside [lwr,upr]. kLo is the key of cell +** lwr-1, or if lwr==0 the lower bound that the parent gave for this page. +** kHi is the key of cell upr+1 if the search has compared that cell. +** Otherwise upr is the last cell and kHi is the upper bound from the +** parent, which on a leaf is the key of the last cell itself and on an +** interior page belongs to the right-most child, one slot past the last +** cell. +** +** The caller guarantees kLo=1, so there is no division by zero, and the quotient +** lies between 0 and iHi-iLo. +*/ +static SQLITE_INLINE int btreeInterpGuess( + MemPage *pPage, /* The page being searched */ + i64 intKey, /* The rowid being sought */ + i64 kLo, i64 kHi, /* Knots: kLo < intKey <= kHi */ + int lwr, int upr /* Cells not yet ruled out */ +){ + u64 nOff = (u64)intKey - (u64)kLo; /* 1 <= nOff <= nKey */ + u64 nKey = (u64)kHi - (u64)kLo; + int iLo = lwr-1; /* Slot of kLo */ + int iHi = upr + (uprnCell-1 || !pPage->leaf); /* Slot of kHi */ + int i; + assert( kLo=0 && lwr<=upr && uprnCell ); + if( nKey>>63 ){ nOff >>= 1; nKey >>= 1; } /* Keep the casts signed */ + i = iLo + (int)((double)(i64)nOff * (double)(iHi-iLo) / (double)(i64)nKey); + if( iupr ) i = upr; + return i; +} +#endif /* LIBSQL_OMIT_INTERP_SEEK */ + /* Move the cursor so that it points to an entry in a table (a.k.a INTKEY) ** table near the key intKey. Return a success code. ** @@ -77543,6 +77615,15 @@ SQLITE_PRIVATE int sqlite3BtreeTableMoveto( int *pRes /* Write search results here */ ){ int rc; +#ifndef LIBSQL_OMIT_INTERP_SEEK + i64 kLo = 0, kHi = 0; /* Keys of the current page are in (kLo,kHi] */ + u8 bKnots = 0; /* 0x01: kLo is known and less than intKey. + ** 0x02: kHi is known and not less than intKey */ + int iGuess = 0; /* Interpolated slot on the current page */ + int nStep = 1; /* Distance of the next probe from iGuess */ + int eStep; /* 0: bisect. 1: step away from iGuess. + ** 2: probing an end cell for a missing knot */ +#endif assert( cursorOwnsBtShared(pCur) ); assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) ); @@ -77620,8 +77701,22 @@ SQLITE_PRIVATE int sqlite3BtreeTableMoveto( upr = pPage->nCell-1; assert( biasRight==0 || biasRight==1 ); idx = upr>>(1-biasRight); /* idx = biasRight ? upr : (lwr+upr)/2; */ +#ifndef LIBSQL_OMIT_INTERP_SEEK + eStep = 0; + if( !biasRight && upr>=LIBSQL_INTERP_MIN_CELLS-1 ){ + if( (bKnots & 0x01)==0 ){ + idx = 0; eStep = 2; /* first cell gives kLo */ + }else if( (bKnots & 0x02)==0 ){ + idx = upr; eStep = 2; /* last cell gives kHi */ + }else{ + idx = iGuess = btreeInterpGuess(pPage, intKey, kLo, kHi, 0, upr); + nStep = 1; eStep = 1; + } + } +#endif for(;;){ i64 nCellKey; + assert( idx>=lwr && idx<=upr ); /* only probe inside the bracket */ pCell = findCellPastPtr(pPage, idx); if( pPage->intKeyLeaf ){ while( 0x80 <= *(pCell++) ){ @@ -77633,14 +77728,27 @@ SQLITE_PRIVATE int sqlite3BtreeTableMoveto( getVarint(pCell, (u64*)&nCellKey); if( nCellKeyupr ){ c = -1; break; } }else if( nCellKey>intKey ){ upr = idx-1; +#ifndef LIBSQL_OMIT_INTERP_SEEK + kHi = nCellKey; +#endif if( lwr>upr ){ c = +1; break; } }else{ assert( nCellKey==intKey ); pCur->ix = (u16)idx; if( !pPage->leaf ){ +#ifndef LIBSQL_OMIT_INTERP_SEEK + /* The child holds keys in (key[idx-1], intKey]. kLo might be + ** an earlier, smaller key than key[idx-1]. That is harmless: + ** with kHi==intKey the guess is the child's last cell. */ + bKnots = (u8)((bKnots & 0x01) | (lwr>0) | 0x02); + kHi = nCellKey; +#endif lwr = idx; goto moveto_table_next_layer; }else{ @@ -77651,6 +77759,26 @@ SQLITE_PRIVATE int sqlite3BtreeTableMoveto( return SQLITE_OK; } } +#ifndef LIBSQL_OMIT_INTERP_SEEK + if( eStep ){ + if( eStep==2 ){ + /* Probed the first or last cell for a missing knot and did not + ** find intKey beyond it. If that was the first cell and kHi is + ** missing too, probe the last cell next. */ + if( idx==0 && (bKnots & 0x02)==0 ){ idx = upr; continue; } + idx = iGuess = btreeInterpGuess(pPage, intKey, kLo, kHi, lwr, upr); + nStep = 1; eStep = 1; + continue; + } + /* Step away from iGuess by 1, 2, 4, ... cells, towards intKey. + ** Once a step passes intKey, the bound it set puts the next step + ** outside [lwr,upr], and the binary search takes over. */ + idx = nCellKey=lwr && idx<=upr ) continue; + eStep = 0; + } +#endif assert( lwr+upr>=0 ); idx = (lwr+upr)>>1; /* idx = (lwr+upr)/2; */ } @@ -77663,6 +77791,11 @@ SQLITE_PRIVATE int sqlite3BtreeTableMoveto( rc = SQLITE_OK; goto moveto_table_finish; } +#ifndef LIBSQL_OMIT_INTERP_SEEK + /* The child holds keys in (key[lwr-1], key[lwr]]. A side that no + ** probe on this page bounded keeps this page's own knot, if any. */ + bKnots |= (u8)((lwr>0) | ((lwrnCell)<<1)); +#endif moveto_table_next_layer: if( lwr>=pPage->nCell ){ chldPg = get4byte(&pPage->aData[pPage->hdrOffset+8]); diff --git a/libsql-sqlite3/doc/libsql_extensions.md b/libsql-sqlite3/doc/libsql_extensions.md index 3a7b80ecac..0690bf820c 100644 --- a/libsql-sqlite3/doc/libsql_extensions.md +++ b/libsql-sqlite3/doc/libsql_extensions.md @@ -413,3 +413,18 @@ static void sync_db(sqlite3 *db_primary, sqlite3 *db_backup){ libsql_wal_end_commit(db_backup); } ``` + +## Interpolation search for rowid seeks + +Seeking a rowid in a table b-tree (`WHERE rowid=?`, rowid ranges, joins on an `INTEGER PRIMARY KEY`, and the row lookup after an index scan) normally binary-searches every page on the way down. libSQL instead predicts the slot of the rowid on each page by linear interpolation. It interpolates between the bounds that the parent page already gives for that page: the cell that points to a child holds the child's largest rowid, and the cell to its left bounds the child from below. From the predicted slot it steps outward by 1, 2, 4, ... cells until the rowid is bracketed, then finishes with a binary search inside the bracket. The root page, and pages on the left or right edge of the tree where one bound is missing, read their first or last cell to get it. + +The result is always the same as with binary search, and the worst case is still O(log n) cell reads per page. Unevenly spaced rowids, or bounds left stale by deletes, only cost extra cell reads. Nothing changes in the file format or the API. + +The prediction uses exact 64-bit differences between rowids, so it works the same for rowids of any size, for example ids that carry a shard or a tag in their high bits. It helps most when rowids are evenly spaced within a page (sequential ids, ids with regular gaps, random ids, timestamps). When the gaps between neighbouring rowids are heavy-tailed, so that a few huge gaps dominate every page, a seek reads about 20% more cells than binary search does and takes about the same time. + +It applies to pages with at least 16 cells. It is not used for appends (`BTREE_APPEND`), whose binary search already starts at the last cell. Index b-trees, including `WITHOUT ROWID` tables, are not affected. + +Compile-time options: + +* `-DLIBSQL_OMIT_INTERP_SEEK` -- compile it out and use the upstream SQLite binary search on every page. Builds with `SQLITE_OMIT_FLOATING_POINT` leave it out automatically. +* `-DLIBSQL_INTERP_MIN_CELLS=N` -- smallest page, in cells, that uses interpolation (default 16). diff --git a/libsql-sqlite3/src/btree.c b/libsql-sqlite3/src/btree.c index 114f4db2be..03907870b9 100644 --- a/libsql-sqlite3/src/btree.c +++ b/libsql-sqlite3/src/btree.c @@ -5702,6 +5702,78 @@ int sqlite3BtreeLast(BtCursor *pCur, int *pRes){ return btreeLast(pCur, pRes); } +#if defined(SQLITE_OMIT_FLOATING_POINT) && !defined(LIBSQL_OMIT_INTERP_SEEK) +# define LIBSQL_OMIT_INTERP_SEEK 1 /* the guess needs floating point */ +#endif +#ifndef LIBSQL_OMIT_INTERP_SEEK +/* +** libSQL: interpolation search inside table (rowid) b-tree pages. +** +** Binary search ignores the values of the keys it compares. Rowids are +** integers and are usually close to evenly spaced within a page, so the +** slot of a key can be predicted by linear interpolation between two +** "knots": an exclusive lower and an inclusive upper bound on the keys of +** the page. When descending from an interior page those knots are free: +** the cell that points at the child holds the child's largest rowid and +** its left neighbour bounds the child from below. The search on every +** page records the nearest keys it compared on each side of intKey, and +** those become the knots of the child it descends into. +** +** On a page with both knots and at least LIBSQL_INTERP_MIN_CELLS cells, +** the first probe goes to the interpolated slot, the next ones step away +** from it by 1, 2, 4, ... cells until intKey is bracketed, and the rest of +** the search is the usual binary search inside the bracket. The knots +** only pick probe positions, so the result is always exact and stale or +** corrupt separator keys only cost extra probes. The worst case is about +** twice the probes of a binary search. A page that lacks a knot (the +** root, and pages on the left or right edge of the tree) first probes its +** first or last cell to get one. +*/ +#ifndef LIBSQL_INTERP_MIN_CELLS +# define LIBSQL_INTERP_MIN_CELLS 16 +#endif + +/* +** Predict the slot of intKey on pPage by linear interpolation between the +** knots kLo and kHi, and clamp it to [lwr,upr], the cells of pPage that +** the search has not ruled out yet. +** +** The knots are the keys just outside [lwr,upr]. kLo is the key of cell +** lwr-1, or if lwr==0 the lower bound that the parent gave for this page. +** kHi is the key of cell upr+1 if the search has compared that cell. +** Otherwise upr is the last cell and kHi is the upper bound from the +** parent, which on a leaf is the key of the last cell itself and on an +** interior page belongs to the right-most child, one slot past the last +** cell. +** +** The caller guarantees kLo=1, so there is no division by zero, and the quotient +** lies between 0 and iHi-iLo. +*/ +static SQLITE_INLINE int btreeInterpGuess( + MemPage *pPage, /* The page being searched */ + i64 intKey, /* The rowid being sought */ + i64 kLo, i64 kHi, /* Knots: kLo < intKey <= kHi */ + int lwr, int upr /* Cells not yet ruled out */ +){ + u64 nOff = (u64)intKey - (u64)kLo; /* 1 <= nOff <= nKey */ + u64 nKey = (u64)kHi - (u64)kLo; + int iLo = lwr-1; /* Slot of kLo */ + int iHi = upr + (uprnCell-1 || !pPage->leaf); /* Slot of kHi */ + int i; + assert( kLo=0 && lwr<=upr && uprnCell ); + if( nKey>>63 ){ nOff >>= 1; nKey >>= 1; } /* Keep the casts signed */ + i = iLo + (int)((double)(i64)nOff * (double)(iHi-iLo) / (double)(i64)nKey); + if( iupr ) i = upr; + return i; +} +#endif /* LIBSQL_OMIT_INTERP_SEEK */ + /* Move the cursor so that it points to an entry in a table (a.k.a INTKEY) ** table near the key intKey. Return a success code. ** @@ -5732,6 +5804,15 @@ int sqlite3BtreeTableMoveto( int *pRes /* Write search results here */ ){ int rc; +#ifndef LIBSQL_OMIT_INTERP_SEEK + i64 kLo = 0, kHi = 0; /* Keys of the current page are in (kLo,kHi] */ + u8 bKnots = 0; /* 0x01: kLo is known and less than intKey. + ** 0x02: kHi is known and not less than intKey */ + int iGuess = 0; /* Interpolated slot on the current page */ + int nStep = 1; /* Distance of the next probe from iGuess */ + int eStep; /* 0: bisect. 1: step away from iGuess. + ** 2: probing an end cell for a missing knot */ +#endif assert( cursorOwnsBtShared(pCur) ); assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) ); @@ -5809,8 +5890,22 @@ int sqlite3BtreeTableMoveto( upr = pPage->nCell-1; assert( biasRight==0 || biasRight==1 ); idx = upr>>(1-biasRight); /* idx = biasRight ? upr : (lwr+upr)/2; */ +#ifndef LIBSQL_OMIT_INTERP_SEEK + eStep = 0; + if( !biasRight && upr>=LIBSQL_INTERP_MIN_CELLS-1 ){ + if( (bKnots & 0x01)==0 ){ + idx = 0; eStep = 2; /* first cell gives kLo */ + }else if( (bKnots & 0x02)==0 ){ + idx = upr; eStep = 2; /* last cell gives kHi */ + }else{ + idx = iGuess = btreeInterpGuess(pPage, intKey, kLo, kHi, 0, upr); + nStep = 1; eStep = 1; + } + } +#endif for(;;){ i64 nCellKey; + assert( idx>=lwr && idx<=upr ); /* only probe inside the bracket */ pCell = findCellPastPtr(pPage, idx); if( pPage->intKeyLeaf ){ while( 0x80 <= *(pCell++) ){ @@ -5822,14 +5917,27 @@ int sqlite3BtreeTableMoveto( getVarint(pCell, (u64*)&nCellKey); if( nCellKeyupr ){ c = -1; break; } }else if( nCellKey>intKey ){ upr = idx-1; +#ifndef LIBSQL_OMIT_INTERP_SEEK + kHi = nCellKey; +#endif if( lwr>upr ){ c = +1; break; } }else{ assert( nCellKey==intKey ); pCur->ix = (u16)idx; if( !pPage->leaf ){ +#ifndef LIBSQL_OMIT_INTERP_SEEK + /* The child holds keys in (key[idx-1], intKey]. kLo might be + ** an earlier, smaller key than key[idx-1]. That is harmless: + ** with kHi==intKey the guess is the child's last cell. */ + bKnots = (u8)((bKnots & 0x01) | (lwr>0) | 0x02); + kHi = nCellKey; +#endif lwr = idx; goto moveto_table_next_layer; }else{ @@ -5840,6 +5948,26 @@ int sqlite3BtreeTableMoveto( return SQLITE_OK; } } +#ifndef LIBSQL_OMIT_INTERP_SEEK + if( eStep ){ + if( eStep==2 ){ + /* Probed the first or last cell for a missing knot and did not + ** find intKey beyond it. If that was the first cell and kHi is + ** missing too, probe the last cell next. */ + if( idx==0 && (bKnots & 0x02)==0 ){ idx = upr; continue; } + idx = iGuess = btreeInterpGuess(pPage, intKey, kLo, kHi, lwr, upr); + nStep = 1; eStep = 1; + continue; + } + /* Step away from iGuess by 1, 2, 4, ... cells, towards intKey. + ** Once a step passes intKey, the bound it set puts the next step + ** outside [lwr,upr], and the binary search takes over. */ + idx = nCellKey=lwr && idx<=upr ) continue; + eStep = 0; + } +#endif assert( lwr+upr>=0 ); idx = (lwr+upr)>>1; /* idx = (lwr+upr)/2; */ } @@ -5852,6 +5980,11 @@ int sqlite3BtreeTableMoveto( rc = SQLITE_OK; goto moveto_table_finish; } +#ifndef LIBSQL_OMIT_INTERP_SEEK + /* The child holds keys in (key[lwr-1], key[lwr]]. A side that no + ** probe on this page bounded keeps this page's own knot, if any. */ + bKnots |= (u8)((lwr>0) | ((lwrnCell)<<1)); +#endif moveto_table_next_layer: if( lwr>=pPage->nCell ){ chldPg = get4byte(&pPage->aData[pPage->hdrOffset+8]); diff --git a/libsql-sqlite3/test/interpseek.test b/libsql-sqlite3/test/interpseek.test new file mode 100644 index 0000000000..8af003204d --- /dev/null +++ b/libsql-sqlite3/test/interpseek.test @@ -0,0 +1,105 @@ +# 2026 libsql +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for interpolation search inside +# table (rowid) b-tree pages (sqlite3BtreeTableMoveto() in btree.c). The search +# predicts the slot of a rowid from "knots" taken from the parent page's +# separator keys and then corrects with an exponential + binary search, so +# its result must always equal that of a plain binary search. +# +# Every key distribution below is loaded into an INTEGER PRIMARY KEY table +# t (all lookups are rowid seeks that go through sqlite3BtreeTableMoveto) +# and into a mirror table m with no usable index (all lookups are full +# scans). Point, range and BETWEEN lookups are then compared for a set of +# probes made of present keys, their neighbours and the INT64 extremes, +# before and after DML that leaves separator keys stale. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix interpseek + +# One row per probe: the answer to each kind of rowid seek on table $tbl. +proc seek_results {tbl} { + db eval [string map [list %T% $tbl] { + SELECT k, + (SELECT b FROM %T% WHERE a=k), + (SELECT max(a) FROM %T% WHERE ak), + (SELECT min(a) FROM %T% WHERE a>=k), + (SELECT count(*) || ',' || total(b) FROM %T% WHERE a BETWEEN k AND k+1000) + FROM p ORDER BY k + }] +} + +# Key generators. x runs from 1 to $n. The multiplier 2654435761 is odd, +# so (x*2654435761) % 2^32 is a bijection and the "random" keys are +# distinct; the scaled result stays well inside the 64-bit range. +set DIST(dense) {x} +set DIST(gaps) {x*7 + (x*x)%5} +set DIST(random) {((x*2654435761) % 4294967296) * 1073741823 - 2305843009213693952} +set DIST(skewed) {x*x*x*x} +set DIST(neg) {-(x*x*13) + 1000} +set DIST(edge) {CASE WHEN x%2 THEN 9223372036854775807 - (x/2)*(x/2)*(x/2) + ELSE -9223372036854775807 - 1 + (x/2)*3 END} +# Rowids too large to be told apart as doubles: dense above 2^62, and +# dense runs that each start at a multiple of 2^52. +set DIST(high) {4611686018427387904 + x} +set DIST(shard) {(x%7)*4503599627370496 + x/7} + +set tn 0 +foreach {pgsz n} {512 3000 4096 8000} { + foreach dist {dense gaps random skewed neg edge high shard} { + incr tn + reset_db + execsql "PRAGMA page_size = $pgsz" + do_execsql_test 1.$tn.0.$dist.$pgsz [string map [list %K% $DIST($dist) %N% $n] { + CREATE TABLE t(a INTEGER PRIMARY KEY, b); + CREATE TABLE m(a INT, b); + CREATE TABLE p(k INT PRIMARY KEY) WITHOUT ROWID; + WITH s(x) AS (SELECT 1 UNION ALL SELECT x+1 FROM s WHERE x<%N%) + INSERT OR IGNORE INTO t SELECT (%K%), x%1000 FROM s ORDER BY random(); + INSERT INTO m SELECT a, b FROM t; + + INSERT OR IGNORE INTO p + SELECT a FROM (SELECT a, row_number() OVER (ORDER BY a) AS rn FROM t) + WHERE rn%41=0 OR rn<=3 OR rn>%N%-3; + INSERT OR IGNORE INTO p + SELECT k-1 FROM p WHERE k>-9223372036854775807-1; + INSERT OR IGNORE INTO p + SELECT k+1 FROM p WHERE k<9223372036854775807; + INSERT OR IGNORE INTO p VALUES + (-9223372036854775807-1), (-1), (0), (1), (9223372036854775807); + SELECT count(*)=(SELECT count(*) FROM m) FROM t; + }] {1} + + do_test 1.$tn.1.$dist.$pgsz { seek_results t } [seek_results m] + + # Remove runs of keys so that parent separators no longer equal the + # largest key of their child, rewrite some rows and re-insert others. + do_execsql_test 1.$tn.2.$dist.$pgsz { + DELETE FROM t WHERE a IN (SELECT a FROM t WHERE b%7=0 OR b BETWEEN 300 AND 340); + DELETE FROM m WHERE b%7=0 OR b BETWEEN 300 AND 340; + UPDATE t SET b=b+1 WHERE a IN (SELECT k FROM p WHERE k%3=0); + UPDATE m SET b=b+1 WHERE a IN (SELECT k FROM p WHERE k%3=0); + REPLACE INTO t SELECT k, -1 FROM p WHERE k%5=0; + INSERT INTO m SELECT a, b FROM t WHERE a IN (SELECT k FROM p WHERE k%5=0) + AND a NOT IN (SELECT a FROM m); + UPDATE m SET b=-1 WHERE a IN (SELECT k FROM p WHERE k%5=0); + SELECT count(*)=(SELECT count(*) FROM m) FROM t; + } {1} + + do_test 1.$tn.3.$dist.$pgsz { seek_results t } [seek_results m] + do_execsql_test 1.$tn.4.$dist.$pgsz { PRAGMA integrity_check } {ok} + } +} + +finish_test