Skip to content

Commit cf977b7

Browse files
committed
TDBStore: the work buffer is at least the program size or 64 bytes
Previously, we always set the work buffer to 64 bytes, without checking it was no less the actual program size. But we can't simply switch to use the program size for the work buffer, because if it's too small (e.g. 1 byte in some cases), we will not be able to read the status header (24 bytes), and small buffers means more underlying write operations and lower efficiency. This PR changes the work buffer size to be the program size, or 64 bytes as an absolute minimum like before.
1 parent ece756c commit cf977b7

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

storage/kvstore/include/kvstore/TDBStore.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,7 @@ class TDBStore : public KVStore {
308308
tdbstore_area_data_t _area_params[_num_areas];
309309
uint32_t _prog_size;
310310
uint8_t *_work_buf;
311+
size_t _work_buf_size;
311312
char *_key_buf;
312313
void *_inc_set_handle;
313314
void *_iterator_table[_max_open_iterators];

storage/kvstore/source/TDBStore.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ typedef struct {
7777
uint32_t crc;
7878
} reserved_trailer_t;
7979

80-
static const uint32_t work_buf_size = 64;
80+
static const size_t min_work_buf_size = 64;
8181
static const uint32_t initial_crc = 0xFFFFFFFF;
8282
static const uint32_t initial_max_keys = 16;
8383

@@ -130,7 +130,7 @@ static uint32_t calc_crc(uint32_t init_crc, uint32_t data_size, const void *data
130130
TDBStore::TDBStore(BlockDevice *bd) : _ram_table(0), _max_keys(0),
131131
_num_keys(0), _bd(bd), _buff_bd(0), _free_space_offset(0), _master_record_offset(0),
132132
_master_record_size(0), _is_initialized(false), _active_area(0), _active_area_version(0), _size(0),
133-
_area_params{}, _prog_size(0), _work_buf(0), _key_buf(0), _inc_set_handle(0)
133+
_area_params{}, _prog_size(0), _work_buf(0), _work_buf_size(0), _key_buf(0), _inc_set_handle(0)
134134
{
135135
for (int i = 0; i < _num_areas; i++) {
136136
_area_params[i] = { 0 };
@@ -327,7 +327,7 @@ int TDBStore::read_record(uint8_t area, uint32_t offset, char *key,
327327
user_key_ptr[key_size] = '\0';
328328
} else {
329329
dest_buf = _work_buf;
330-
chunk_size = std::min(key_size, work_buf_size);
330+
chunk_size = std::min(key_size, _work_buf_size);
331331
}
332332
} else {
333333
// This means that we're on the data part
@@ -337,13 +337,13 @@ int TDBStore::read_record(uint8_t area, uint32_t offset, char *key,
337337
// 3. After actual part is finished - read to work buffer
338338
// 4. Copy data flag not set - read to work buffer
339339
if (curr_data_offset < data_offset) {
340-
chunk_size = std::min((size_t)work_buf_size, (size_t)(data_offset - curr_data_offset));
340+
chunk_size = std::min<size_t>(_work_buf_size, (data_offset - curr_data_offset));
341341
dest_buf = _work_buf;
342342
} else if (copy_data && (curr_data_offset < data_offset + actual_data_size)) {
343343
chunk_size = actual_data_size;
344344
dest_buf = static_cast<uint8_t *>(data_buf);
345345
} else {
346-
chunk_size = std::min(work_buf_size, total_size);
346+
chunk_size = std::min<size_t>(_work_buf_size, total_size);
347347
dest_buf = _work_buf;
348348
}
349349
}
@@ -860,7 +860,7 @@ int TDBStore::copy_record(uint8_t from_area, uint32_t from_offset, uint32_t to_o
860860
total_size -= chunk_size;
861861

862862
while (total_size) {
863-
chunk_size = std::min(total_size, work_buf_size);
863+
chunk_size = std::min(total_size, _work_buf_size);
864864
ret = read_area(from_area, from_offset, chunk_size, _work_buf);
865865
if (ret) {
866866
return ret;
@@ -1044,7 +1044,8 @@ int TDBStore::init()
10441044
}
10451045

10461046
_prog_size = _bd->get_program_size();
1047-
_work_buf = new uint8_t[work_buf_size];
1047+
_work_buf_size = std::max<size_t>(_prog_size, min_work_buf_size);
1048+
_work_buf = new uint8_t[_work_buf_size];
10481049
_key_buf = new char[MAX_KEY_SIZE];
10491050
_inc_set_handle = new inc_set_handle_t;
10501051
memset(_inc_set_handle, 0, sizeof(inc_set_handle_t));

0 commit comments

Comments
 (0)