Skip to content

Commit 8514c6b

Browse files
committed
refactor(buffer): 优化缓冲池空闲帧管理
- 将 free_list_ 从 std::list 改为 std::vector,减少内存开销 - 使用 std::iota 初始化空闲帧列表,简化构造函数 - 调整 find_victim_page 函数,使用 vector 作为空闲帧列表
1 parent eb7c373 commit 8514c6b

5 files changed

Lines changed: 18 additions & 19 deletions

File tree

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,6 @@ performance_testing/*.perf
7676
performance_testing/*.data
7777
performance_testing/*.folded
7878

79-
.cunzhi-memory/
79+
.cunzhi-memory/
80+
81+
make.sh

src/execution/execution_manager.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -218,34 +218,33 @@ void QlManager::select_from(std::unique_ptr<AbstractExecutor> executorTreeRoot,
218218
// Print records
219219
size_t num_rec = 0;
220220
// 执行query_plan
221-
char buffer[BUFFER_LENGTH];
222-
int offset = 0;
223-
int size;
221+
constexpr int BUFFER_SIZE = BUFFER_LENGTH;
222+
char buffer[BUFFER_SIZE];
224223
for (executorTreeRoot->beginTuple(); !executorTreeRoot->is_end(); executorTreeRoot->nextTuple()) {
225224
auto Tuple = executorTreeRoot->Next();
226225
const auto &cols = executorTreeRoot->cols();
227226
std::vector<std::string_view> columns;
228227
columns.reserve(cols.size());
228+
size_t offset = 0;
229+
size_t size;
229230
for (auto &col : cols) {
230231
char *rec_buf = Tuple->data + col.offset;
231232
switch (col.type) {
232233
case ColType::TYPE_INT:
233-
size = sprintf(buffer + offset, "%d", *(int *)rec_buf);
234+
size = snprintf(buffer + offset, BUFFER_SIZE - offset, "%d", *(int *)rec_buf);
234235
columns.emplace_back(buffer + offset, size);
235236
offset += size;
236237
break;
237238
case ColType::TYPE_FLOAT:
238-
size = sprintf(buffer + offset, "%.6f", *(float *)rec_buf); // 更简洁的浮点表示
239+
size = snprintf(buffer + offset, BUFFER_SIZE - offset, "%.6f", *(float *)rec_buf); // 更简洁的浮点表示
239240
columns.emplace_back(buffer + offset, size);
240241
offset += size;
241242
break;
242243
case ColType::TYPE_STRING:
243-
size_t actual_len = strnlen(rec_buf, col.len);
244-
columns.emplace_back(rec_buf, actual_len);
244+
columns.emplace_back(rec_buf, strnlen(rec_buf, col.len));
245245
break;
246246
}
247247
}
248-
NXT:;
249248
// print record into buffer
250249
rec_printer.print_record(columns, context);
251250
// print record into file

src/record_printer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class RecordPrinter {
6464
strcpy(buffer, "| ");
6565
if (col.size() > COL_WIDTH) {
6666
memcpy(buffer + 2, col.data(), COL_WIDTH - 3);
67-
strcpy(buffer, "... ");
67+
strcpy(buffer + COL_WIDTH - 1, "... ");
6868
} else {
6969
memset(buffer + 2, ' ', COL_WIDTH - col.size());
7070
memcpy(buffer + 2 + COL_WIDTH - col.size(), col.data(), col.size());

src/storage/buffer_pool_instance.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ bool BufferPoolInstance::find_victim_page(frame_id_t* frame_id) {
3333
// 如果有空闲帧,直接使用
3434
// Caller must hold the latch_ if free_list_ access needs protection.
3535
if (!free_list_.empty()) {
36-
*frame_id = free_list_.front();
37-
free_list_.pop_front();
36+
*frame_id = free_list_.back();
37+
free_list_.pop_back();
3838
return true;
3939
}
4040

src/storage/buffer_pool_instance.h

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ See the Mulan PSL v2 for more details. */
2424
#include "replacer/lru_replacer.h"
2525
#include "replacer/replacer.h"
2626
#include "page_guard.h"
27+
#include <numeric>
2728

2829
extern DiskManager disk_manager;
2930

@@ -37,17 +38,14 @@ class BufferPoolInstance {
3738
static constexpr size_t pool_size_ = BUFFER_POOL_SIZE / BUFFER_POOL_INSTANCE_SIZE; // 缓冲池大小(帧数)
3839
Page pages_[pool_size_]; // 缓冲池中的页面数组,连续分配
3940
std::unordered_map<PageId, frame_id_t> page_table_; // 页面到帧的映射表
40-
std::list<frame_id_t> free_list_; // 空闲帧链表
41+
std::vector<frame_id_t> free_list_; // 空闲帧链表
4142
ReplacerType replacer_; // 页面替换策略实现
4243
std::mutex latch_; // 并发控制锁
4344

4445
public:
45-
BufferPoolInstance() {
46-
// 初始化时,所有的page都在free_list_中
47-
for (size_t i = 0; i < pool_size_; ++i) {
48-
free_list_.emplace_back(static_cast<frame_id_t>(i)); // static_cast转换数据类型
49-
}
50-
page_table_.reserve(pool_size_); // 预留空间,避免频繁扩容
46+
BufferPoolInstance(): free_list_(pool_size_){
47+
std::iota(free_list_.begin(), free_list_.end(), 0);
48+
page_table_.reserve(pool_size_ * 4); // 预留空间,避免频繁扩容
5149
}
5250

5351
~BufferPoolInstance() = default;

0 commit comments

Comments
 (0)