Skip to content

Commit 90f82cb

Browse files
Taejoon Songtorvalds
authored andcommitted
zram: try to avoid worst-case scenario on same element pages
The worst-case scenario on finding same element pages is that almost all elements are same at the first glance but only last few elements are different. Since the same element tends to be grouped from the beginning of the pages, if we check the first element with the last element before looping through all elements, we might have some chances to quickly detect non-same element pages. 1. Test is done under LG webOS TV (64-bit arch) 2. Dump the swap-out pages (~819200 pages) 3. Analyze the pages with simple test script which counts the iteration number and measures the speed at off-line Under 64-bit arch, the worst iteration count is PAGE_SIZE / 8 bytes = 512. The speed is based on the time to consume page_same_filled() function only. The result, on average, is listed as below: Num of Iter Speed(MB/s) Looping-Forward (Orig) 38 99265 Looping-Backward 36 102725 Last-element-check (This Patch) 33 125072 The result shows that the average iteration count decreases by 13% and the speed increases by 25% with this patch. This patch does not increase the overall time complexity, though. I also ran simpler version which uses backward loop. Just looping backward also makes some improvement, but less than this patch. [[email protected]: fix off-by-one] Link: http://lkml.kernel.org/r/[email protected] Link: http://lkml.kernel.org/r/[email protected] Signed-off-by: Taejoon Song <[email protected]> Acked-by: Minchan Kim <[email protected]> Cc: Sergey Senozhatsky <[email protected]> Cc: Jens Axboe <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent 0a3c577 commit 90f82cb

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

drivers/block/zram/zram_drv.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,14 +207,17 @@ static inline void zram_fill_page(void *ptr, unsigned long len,
207207

208208
static bool page_same_filled(void *ptr, unsigned long *element)
209209
{
210-
unsigned int pos;
211210
unsigned long *page;
212211
unsigned long val;
212+
unsigned int pos, last_pos = PAGE_SIZE / sizeof(*page) - 1;
213213

214214
page = (unsigned long *)ptr;
215215
val = page[0];
216216

217-
for (pos = 1; pos < PAGE_SIZE / sizeof(*page); pos++) {
217+
if (val != page[last_pos])
218+
return false;
219+
220+
for (pos = 1; pos < last_pos; pos++) {
218221
if (val != page[pos])
219222
return false;
220223
}

0 commit comments

Comments
 (0)