diff options
| author | Jeff Brown <jeffbrown@google.com> | 2011-10-27 14:52:28 -0700 |
|---|---|---|
| committer | Jeff Brown <jeffbrown@google.com> | 2011-10-28 01:46:18 -0700 |
| commit | 650de3dcfcbc7635da3c070410ef1dc4027ae464 (patch) | |
| tree | 93cb485d70a4388a76397e6f65a80c1a059425fb /core/java/android/database/DatabaseUtils.java | |
| parent | 4257e3b18118200e29a3125a12044c4572c1baf5 (diff) | |
Optimize fillWindow to improve reverse-seek performance.
Bug: 5520301
When an application requests a row from a SQLiteCursor that
is not in the window, instead of filling from the requested
row position onwards, fill from a little bit ahead of the
requested row position.
This fixes a problem with applications that seek backwards
in large cursor windows. Previously the application could
end up refilling the window every time it moved back
one position.
We try to fill about 1/3 before the requested position and
2/3 after which substantially improves scrolling responsiveness
when the list is bound to a data set that does not fit
entirely within one cursor window.
Change-Id: I168ff1d3aed1a41ac96267be34a026c108590e52
Diffstat (limited to 'core/java/android/database/DatabaseUtils.java')
| -rw-r--r-- | core/java/android/database/DatabaseUtils.java | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/core/java/android/database/DatabaseUtils.java b/core/java/android/database/DatabaseUtils.java index a10ca1502a77..a8ba9a36ad2e 100644 --- a/core/java/android/database/DatabaseUtils.java +++ b/core/java/android/database/DatabaseUtils.java @@ -726,6 +726,32 @@ public class DatabaseUtils { } /** + * Picks a start position for {@link Cursor#fillWindow} such that the + * window will contain the requested row and a useful range of rows + * around it. + * + * When the data set is too large to fit in a cursor window, seeking the + * cursor can become a very expensive operation since we have to run the + * query again when we move outside the bounds of the current window. + * + * We try to choose a start position for the cursor window such that + * 1/3 of the window's capacity is used to hold rows before the requested + * position and 2/3 of the window's capacity is used to hold rows after the + * requested position. + * + * @param cursorPosition The row index of the row we want to get. + * @param cursorWindowCapacity The estimated number of rows that can fit in + * a cursor window, or 0 if unknown. + * @return The recommended start position, always less than or equal to + * the requested row. + * @hide + */ + public static int cursorPickFillWindowStartPosition( + int cursorPosition, int cursorWindowCapacity) { + return Math.max(cursorPosition - cursorWindowCapacity / 3, 0); + } + + /** * Query the table for the number of rows in the table. * @param db the database the table is in * @param table the name of the table to query |
