android_frameworks_base/libs/androidfw/tests/CursorWindow_bench.cpp
Jeff Sharkey ae2d88a65c Rewrite of CursorWindow internals.
The original CursorWindow implementation was created in Android 1.0
and has remained relatively unchanged since then.  Unfortunately that
design results in very poor performance on large windows, since
reading or writing each FieldSlot is O(row/100) to traverse through
a chain of RowSlotChunks.  It's also memory-inefficient due to how
it allocates RowSlotChunks in 404 byte chunks, even when there's only
a single row to store.

This change is a complete redesign of the CursorWindow internals to
use a "heap-and-stack" style approach, where a "heap" of strings
and blobs increment up from the bottom of the window while a "stack"
of FieldSlots increment down from the top of the window.

The included benchmarks show the following improvements, ensuring
no regressions for small windows, while offering very dramatic
improvements for larger windows:

                Big cores      Little cores
4x4 cursor      no regression  no regression
1024x4 cursor   2.2x faster    2.0x faster
16384x4 cursor  48.5x faster   24.4x faster

Detailed unit testing is also included to ensure that the rewrite
behaves correctly.

Bug: 169251528
Test: atest libandroidfw_tests
Test: atest CtsDatabaseTestCases
Test: atest FrameworksCoreTests:android.database
Test: ./frameworks/base/libs/hwui/tests/scripts/prep_generic.sh little && atest libandroidfw_benchmarks
Test: ./frameworks/base/libs/hwui/tests/scripts/prep_generic.sh little && atest CorePerfTests:android.database.CrossProcessCursorPerfTest
Change-Id: I90dff31fd550130dae917a33e0e1fa684e15c107
2020-10-19 16:07:04 -06:00

87 lines
2.5 KiB
C++

/*
* Copyright (C) 2020 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "benchmark/benchmark.h"
#include "androidfw/CursorWindow.h"
namespace android {
static void BM_CursorWindowWrite(benchmark::State& state, size_t rows, size_t cols) {
CursorWindow* w;
CursorWindow::create(String8("test"), 1 << 21, &w);
while (state.KeepRunning()) {
w->clear();
w->setNumColumns(cols);
for (int row = 0; row < rows; row++) {
w->allocRow();
for (int col = 0; col < cols; col++) {
w->putLong(row, col, 0xcafe);
}
}
}
}
static void BM_CursorWindowWrite4x4(benchmark::State& state) {
BM_CursorWindowWrite(state, 4, 4);
}
BENCHMARK(BM_CursorWindowWrite4x4);
static void BM_CursorWindowWrite1Kx4(benchmark::State& state) {
BM_CursorWindowWrite(state, 1024, 4);
}
BENCHMARK(BM_CursorWindowWrite1Kx4);
static void BM_CursorWindowWrite16Kx4(benchmark::State& state) {
BM_CursorWindowWrite(state, 16384, 4);
}
BENCHMARK(BM_CursorWindowWrite16Kx4);
static void BM_CursorWindowRead(benchmark::State& state, size_t rows, size_t cols) {
CursorWindow* w;
CursorWindow::create(String8("test"), 1 << 21, &w);
w->setNumColumns(cols);
for (int row = 0; row < rows; row++) {
w->allocRow();
}
while (state.KeepRunning()) {
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
w->getFieldSlot(row, col);
}
}
}
}
static void BM_CursorWindowRead4x4(benchmark::State& state) {
BM_CursorWindowRead(state, 4, 4);
}
BENCHMARK(BM_CursorWindowRead4x4);
static void BM_CursorWindowRead1Kx4(benchmark::State& state) {
BM_CursorWindowRead(state, 1024, 4);
}
BENCHMARK(BM_CursorWindowRead1Kx4);
static void BM_CursorWindowRead16Kx4(benchmark::State& state) {
BM_CursorWindowRead(state, 16384, 4);
}
BENCHMARK(BM_CursorWindowRead16Kx4);
} // namespace android