From 12db9630243108dee5d1696d63a97709af6739d8 Mon Sep 17 00:00:00 2001 From: Timo Huovinen Date: Sun, 8 Nov 2026 22:58:38 +0200 Subject: [PATCH 1/2] btree: interpolation search for rowid seeks using parent-page knots sqlite3BtreeTableMoveto() binary-searches every page and ignores the key values. Rowids are integers and usually close to evenly spaced within a page, and the search already knows each child's key range when it descends: the interior cell pointing at the child holds the child's largest rowid and its left neighbour bounds the child from below. Use those bounds ("knots") to choose where the existing search loop probes: first the interpolated slot, then steps of 1, 2, 4, ... cells away from it until intKey is bracketed, then the usual (lwr+upr)/2. The loop's own < and > branches record the nearest keys on each side, which become the knots of the child; a page that lacks a knot (the root and the left/right edge of the tree) probes its first or last cell to get one. Only the probe position changes. Any probe inside [lwr,upr] keeps the binary-search invariant, so results are identical to binary search on well-formed pages and the worst case stays O(log n); stale separators only cost probes. An assert() checks that every probe stays inside the bracket. Applies to pages with at least LIBSQL_INTERP_MIN_CELLS (16) cells, not to BTREE_APPEND seeks. No file-format or API change. -DLIBSQL_OMIT_INTERP_SEEK compiles it out; builds with SQLITE_OMIT_FLOATING_POINT leave it out automatically. Cells compared per seek (1M rows, 4 KiB pages) fall from 19.2 to 7.1 for dense rowids and to 11.1 for random 64-bit rowids. Rowid point lookups, range seeks, joins and inserts are 8-37% faster across six key distributions; speedtest1's rowid-seeking tests are 2-28% faster and the other tests run the same instruction count. The same change is applied to both bundled amalgamations. Adds test/interpseek.test and documents the options in doc/libsql_extensions.md. --- .../SQLite3MultipleCiphers/src/sqlite3.c | 103 ++++++++++++++++++ libsql-ffi/bundled/src/sqlite3.c | 103 ++++++++++++++++++ libsql-sqlite3/doc/libsql_extensions.md | 13 +++ libsql-sqlite3/src/btree.c | 103 ++++++++++++++++++ libsql-sqlite3/test/interpseek.test | 101 +++++++++++++++++ 5 files changed, 423 insertions(+) create mode 100644 libsql-sqlite3/test/interpseek.test diff --git a/libsql-ffi/bundled/SQLite3MultipleCiphers/src/sqlite3.c b/libsql-ffi/bundled/SQLite3MultipleCiphers/src/sqlite3.c index 62a917fb9b..70ded4cded 100644 --- a/libsql-ffi/bundled/SQLite3MultipleCiphers/src/sqlite3.c +++ b/libsql-ffi/bundled/SQLite3MultipleCiphers/src/sqlite3.c @@ -77513,6 +77513,52 @@ 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 a page of nCell cells whose keys are in +** (kLo,kHi], by linear interpolation, and clamp it to [lwr,upr]. +*/ +static SQLITE_INLINE int btreeInterpGuess( + i64 intKey, i64 kLo, i64 kHi, int nCell, int lwr, int upr +){ + double d0 = (double)kLo + 1.0; + double x = ((double)intKey - d0) / ((double)kHi - d0) * (double)(nCell-1); + if( !(x>=(double)lwr) ) return lwr; /* also catches NaN */ + if( x>(double)upr ) return upr; + return (int)x; +} +#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 +77589,14 @@ 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. 0x02: kHi is known */ + 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 +77674,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(intKey, kLo, kHi, upr+1, 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 +77701,25 @@ 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] */ + bKnots = (u8)((bKnots & 0x01) | (lwr>0) | 0x02); + kHi = nCellKey; +#endif lwr = idx; goto moveto_table_next_layer; }else{ @@ -77651,6 +77730,25 @@ 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 */ + if( idx==0 && (bKnots & 0x02)==0 ){ idx = upr; continue; } + idx = iGuess = btreeInterpGuess(intKey, kLo, kHi, pPage->nCell, + 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 +77761,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..70ded4cded 100644 --- a/libsql-ffi/bundled/src/sqlite3.c +++ b/libsql-ffi/bundled/src/sqlite3.c @@ -77513,6 +77513,52 @@ 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 a page of nCell cells whose keys are in +** (kLo,kHi], by linear interpolation, and clamp it to [lwr,upr]. +*/ +static SQLITE_INLINE int btreeInterpGuess( + i64 intKey, i64 kLo, i64 kHi, int nCell, int lwr, int upr +){ + double d0 = (double)kLo + 1.0; + double x = ((double)intKey - d0) / ((double)kHi - d0) * (double)(nCell-1); + if( !(x>=(double)lwr) ) return lwr; /* also catches NaN */ + if( x>(double)upr ) return upr; + return (int)x; +} +#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 +77589,14 @@ 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. 0x02: kHi is known */ + 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 +77674,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(intKey, kLo, kHi, upr+1, 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 +77701,25 @@ 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] */ + bKnots = (u8)((bKnots & 0x01) | (lwr>0) | 0x02); + kHi = nCellKey; +#endif lwr = idx; goto moveto_table_next_layer; }else{ @@ -77651,6 +77730,25 @@ 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 */ + if( idx==0 && (bKnots & 0x02)==0 ){ idx = upr; continue; } + idx = iGuess = btreeInterpGuess(intKey, kLo, kHi, pPage->nCell, + 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 +77761,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..0e084bfb65 100644 --- a/libsql-sqlite3/doc/libsql_extensions.md +++ b/libsql-sqlite3/doc/libsql_extensions.md @@ -413,3 +413,16 @@ 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. + +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..4254277315 100644 --- a/libsql-sqlite3/src/btree.c +++ b/libsql-sqlite3/src/btree.c @@ -5702,6 +5702,52 @@ 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 a page of nCell cells whose keys are in +** (kLo,kHi], by linear interpolation, and clamp it to [lwr,upr]. +*/ +static SQLITE_INLINE int btreeInterpGuess( + i64 intKey, i64 kLo, i64 kHi, int nCell, int lwr, int upr +){ + double d0 = (double)kLo + 1.0; + double x = ((double)intKey - d0) / ((double)kHi - d0) * (double)(nCell-1); + if( !(x>=(double)lwr) ) return lwr; /* also catches NaN */ + if( x>(double)upr ) return upr; + return (int)x; +} +#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 +5778,14 @@ 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. 0x02: kHi is known */ + 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 +5863,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(intKey, kLo, kHi, upr+1, 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 +5890,25 @@ 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] */ + bKnots = (u8)((bKnots & 0x01) | (lwr>0) | 0x02); + kHi = nCellKey; +#endif lwr = idx; goto moveto_table_next_layer; }else{ @@ -5840,6 +5919,25 @@ 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 */ + if( idx==0 && (bKnots & 0x02)==0 ){ idx = upr; continue; } + idx = iGuess = btreeInterpGuess(intKey, kLo, kHi, pPage->nCell, + 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 +5950,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..defbc9ca69 --- /dev/null +++ b/libsql-sqlite3/test/interpseek.test @@ -0,0 +1,101 @@ +# 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} + +set tn 0 +foreach {pgsz n} {512 3000 4096 8000} { + foreach dist {dense gaps random skewed neg edge} { + 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 From 08d415a72dbdefcca1f0491c751c322918f1bf01 Mon Sep 17 00:00:00 2001 From: Timo Huovinen Date: Mon, 16 Nov 2026 12:01:38 +0200 Subject: [PATCH 2/2] btree: interpolate rowid seeks from exact integer key differences The slot guess converted each rowid to a double before subtracting. Above 2^53 neighbouring rowids round to the same double, so on tables whose rowids are large (dense ids above 2^62, ids with a shard or tag in the high bits, clusters at large offsets) the guess collapsed to the first cell and a seek compared as many cells as binary search or more (19.2 -> 19.2-24.0 per seek on 1M rows), which made those seeks up to 25% slower than before the interpolation was added. It also relied on IEEE NaN/Inf semantics for the 0/0 and x/0 cases. Take the differences in unsigned 64-bit arithmetic instead and convert only those. They are exact for any pair of rowids, and because both knots are keys that the search has itself compared against intKey, kLo before -> now: dense 1..N 19.2 -> 7.1 -> 6.9 dense above 2^62 19.2 -> 19.2 -> 6.9 dense from INT64_MIN 19.2 -> 17.3 -> 6.9 clusters at large bases 19.2 -> 24.0 -> 15.5 (shard<<52)|seq 19.2 -> 21.3 -> 16.9 random 64-bit 19.2 -> 10.1 -> 10.0 Results are unchanged: only the probe position differs. interpseek.test gains two distributions with large rowids. The bundled amalgamations are regenerated. --- .../SQLite3MultipleCiphers/src/sqlite3.c | 60 ++++++++++++++----- libsql-ffi/bundled/src/sqlite3.c | 60 ++++++++++++++----- libsql-sqlite3/doc/libsql_extensions.md | 2 + libsql-sqlite3/src/btree.c | 58 +++++++++++++----- libsql-sqlite3/test/interpseek.test | 6 +- 5 files changed, 141 insertions(+), 45 deletions(-) diff --git a/libsql-ffi/bundled/SQLite3MultipleCiphers/src/sqlite3.c b/libsql-ffi/bundled/SQLite3MultipleCiphers/src/sqlite3.c index 70ded4cded..0ae64a6b48 100644 --- a/libsql-ffi/bundled/SQLite3MultipleCiphers/src/sqlite3.c +++ b/libsql-ffi/bundled/SQLite3MultipleCiphers/src/sqlite3.c @@ -77545,17 +77545,43 @@ SQLITE_PRIVATE int sqlite3BtreeLast(BtCursor *pCur, int *pRes){ #endif /* -** Predict the slot of intKey on a page of nCell cells whose keys are in -** (kLo,kHi], by linear interpolation, and clamp it to [lwr,upr]. +** 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( - i64 intKey, i64 kLo, i64 kHi, int nCell, int lwr, int upr -){ - double d0 = (double)kLo + 1.0; - double x = ((double)intKey - d0) / ((double)kHi - d0) * (double)(nCell-1); - if( !(x>=(double)lwr) ) return lwr; /* also catches NaN */ - if( x>(double)upr ) return upr; - return (int)x; + 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 */ @@ -77591,7 +77617,8 @@ SQLITE_PRIVATE int sqlite3BtreeTableMoveto( 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. 0x02: kHi is known */ + 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. @@ -77682,7 +77709,7 @@ SQLITE_PRIVATE int sqlite3BtreeTableMoveto( }else if( (bKnots & 0x02)==0 ){ idx = upr; eStep = 2; /* last cell gives kHi */ }else{ - idx = iGuess = btreeInterpGuess(intKey, kLo, kHi, upr+1, 0, upr); + idx = iGuess = btreeInterpGuess(pPage, intKey, kLo, kHi, 0, upr); nStep = 1; eStep = 1; } } @@ -77716,7 +77743,9 @@ SQLITE_PRIVATE int sqlite3BtreeTableMoveto( pCur->ix = (u16)idx; if( !pPage->leaf ){ #ifndef LIBSQL_OMIT_INTERP_SEEK - /* The child holds keys in (key[idx-1], intKey] */ + /* 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 @@ -77733,10 +77762,11 @@ SQLITE_PRIVATE int sqlite3BtreeTableMoveto( #ifndef LIBSQL_OMIT_INTERP_SEEK if( eStep ){ if( eStep==2 ){ - /* Probed the first or last cell for a missing knot */ + /* 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(intKey, kLo, kHi, pPage->nCell, - lwr, upr); + idx = iGuess = btreeInterpGuess(pPage, intKey, kLo, kHi, lwr, upr); nStep = 1; eStep = 1; continue; } diff --git a/libsql-ffi/bundled/src/sqlite3.c b/libsql-ffi/bundled/src/sqlite3.c index 70ded4cded..0ae64a6b48 100644 --- a/libsql-ffi/bundled/src/sqlite3.c +++ b/libsql-ffi/bundled/src/sqlite3.c @@ -77545,17 +77545,43 @@ SQLITE_PRIVATE int sqlite3BtreeLast(BtCursor *pCur, int *pRes){ #endif /* -** Predict the slot of intKey on a page of nCell cells whose keys are in -** (kLo,kHi], by linear interpolation, and clamp it to [lwr,upr]. +** 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( - i64 intKey, i64 kLo, i64 kHi, int nCell, int lwr, int upr -){ - double d0 = (double)kLo + 1.0; - double x = ((double)intKey - d0) / ((double)kHi - d0) * (double)(nCell-1); - if( !(x>=(double)lwr) ) return lwr; /* also catches NaN */ - if( x>(double)upr ) return upr; - return (int)x; + 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 */ @@ -77591,7 +77617,8 @@ SQLITE_PRIVATE int sqlite3BtreeTableMoveto( 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. 0x02: kHi is known */ + 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. @@ -77682,7 +77709,7 @@ SQLITE_PRIVATE int sqlite3BtreeTableMoveto( }else if( (bKnots & 0x02)==0 ){ idx = upr; eStep = 2; /* last cell gives kHi */ }else{ - idx = iGuess = btreeInterpGuess(intKey, kLo, kHi, upr+1, 0, upr); + idx = iGuess = btreeInterpGuess(pPage, intKey, kLo, kHi, 0, upr); nStep = 1; eStep = 1; } } @@ -77716,7 +77743,9 @@ SQLITE_PRIVATE int sqlite3BtreeTableMoveto( pCur->ix = (u16)idx; if( !pPage->leaf ){ #ifndef LIBSQL_OMIT_INTERP_SEEK - /* The child holds keys in (key[idx-1], intKey] */ + /* 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 @@ -77733,10 +77762,11 @@ SQLITE_PRIVATE int sqlite3BtreeTableMoveto( #ifndef LIBSQL_OMIT_INTERP_SEEK if( eStep ){ if( eStep==2 ){ - /* Probed the first or last cell for a missing knot */ + /* 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(intKey, kLo, kHi, pPage->nCell, - lwr, upr); + idx = iGuess = btreeInterpGuess(pPage, intKey, kLo, kHi, lwr, upr); nStep = 1; eStep = 1; continue; } diff --git a/libsql-sqlite3/doc/libsql_extensions.md b/libsql-sqlite3/doc/libsql_extensions.md index 0e084bfb65..0690bf820c 100644 --- a/libsql-sqlite3/doc/libsql_extensions.md +++ b/libsql-sqlite3/doc/libsql_extensions.md @@ -420,6 +420,8 @@ Seeking a rowid in a table b-tree (`WHERE rowid=?`, rowid ranges, joins on an `I 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: diff --git a/libsql-sqlite3/src/btree.c b/libsql-sqlite3/src/btree.c index 4254277315..03907870b9 100644 --- a/libsql-sqlite3/src/btree.c +++ b/libsql-sqlite3/src/btree.c @@ -5734,17 +5734,43 @@ int sqlite3BtreeLast(BtCursor *pCur, int *pRes){ #endif /* -** Predict the slot of intKey on a page of nCell cells whose keys are in -** (kLo,kHi], by linear interpolation, and clamp it to [lwr,upr]. +** 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( - i64 intKey, i64 kLo, i64 kHi, int nCell, int lwr, int upr + 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 */ ){ - double d0 = (double)kLo + 1.0; - double x = ((double)intKey - d0) / ((double)kHi - d0) * (double)(nCell-1); - if( !(x>=(double)lwr) ) return lwr; /* also catches NaN */ - if( x>(double)upr ) return upr; - return (int)x; + 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 */ @@ -5780,7 +5806,8 @@ int sqlite3BtreeTableMoveto( 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. 0x02: kHi is known */ + 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. @@ -5871,7 +5898,7 @@ int sqlite3BtreeTableMoveto( }else if( (bKnots & 0x02)==0 ){ idx = upr; eStep = 2; /* last cell gives kHi */ }else{ - idx = iGuess = btreeInterpGuess(intKey, kLo, kHi, upr+1, 0, upr); + idx = iGuess = btreeInterpGuess(pPage, intKey, kLo, kHi, 0, upr); nStep = 1; eStep = 1; } } @@ -5905,7 +5932,9 @@ int sqlite3BtreeTableMoveto( pCur->ix = (u16)idx; if( !pPage->leaf ){ #ifndef LIBSQL_OMIT_INTERP_SEEK - /* The child holds keys in (key[idx-1], intKey] */ + /* 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 @@ -5922,10 +5951,11 @@ int sqlite3BtreeTableMoveto( #ifndef LIBSQL_OMIT_INTERP_SEEK if( eStep ){ if( eStep==2 ){ - /* Probed the first or last cell for a missing knot */ + /* 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(intKey, kLo, kHi, pPage->nCell, - lwr, upr); + idx = iGuess = btreeInterpGuess(pPage, intKey, kLo, kHi, lwr, upr); nStep = 1; eStep = 1; continue; } diff --git a/libsql-sqlite3/test/interpseek.test b/libsql-sqlite3/test/interpseek.test index defbc9ca69..8af003204d 100644 --- a/libsql-sqlite3/test/interpseek.test +++ b/libsql-sqlite3/test/interpseek.test @@ -50,10 +50,14 @@ 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} { + foreach dist {dense gaps random skewed neg edge high shard} { incr tn reset_db execsql "PRAGMA page_size = $pgsz"