Skip to content

Commit 9acf2d0

Browse files
committed
refactor(index): 优化索引扫描和内存管理
- 移除 IxScan::next 中的动态内存分配,使用栈分配替代 - 简化 IxScanFinal 构造函数中的代码 - 重构 Page 类,使用静态数组替代动态分配
1 parent df1fc12 commit 9acf2d0

3 files changed

Lines changed: 9 additions & 10 deletions

File tree

src/index/ix_scan.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,21 @@ See the Mulan PSL v2 for more details. */
1111
#include "ix_scan.h"
1212

1313
void IxScan::next() {
14-
auto node = new IxNodeHandle(ih_->file_hdr_, now);
15-
assert(node->is_leaf_page());
16-
assert(iid_.slot_no < node->get_size());
14+
IxNodeHandle node(ih_->file_hdr_, now);
15+
assert(node.is_leaf_page());
16+
assert(iid_.slot_no < node.get_size());
1717

1818
// increment slot no
1919
iid_.slot_no++;
20-
if (iid_.page_no != ih_->file_hdr_->last_leaf_ && iid_.slot_no == node->get_size()) {
20+
if (iid_.page_no != ih_->file_hdr_->last_leaf_ && iid_.slot_no == node.get_size()) {
2121
// go to next leaf
2222
iid_.slot_no = 0;
23-
iid_.page_no = node->get_next_leaf();
23+
iid_.page_no = node.get_next_leaf();
2424
Page* next_page = buffer_pool_manager.fetch_page({ih_->fd_, iid_.page_no});
2525
next_page->RLatch();
2626
unlatch(); // 释放当前页面
2727
now = next_page; // 更新当前页面为下一个叶节点
2828
}
29-
delete node; // 释放内存
3029
}
3130

3231
/**

src/index/ix_scan_final.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ IxScanFinal::IxScanFinal(const IxIndexHandle *ih, const Iid &lower, const Iid &u
1818
Page *page = buffer_pool_manager.fetch_page({ih_->fd_, iid.page_no});
1919
page->RLatch();
2020
while (true) {
21-
auto node = IxNodeHandle(ih_->file_hdr_, page);
21+
IxNodeHandle node(ih_->file_hdr_, page);
2222

2323
while (iid != upper) {
2424
rids_.push_back(ih_->get_rid(iid));

src/storage/page.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ class Page {
5454
friend class RmFileHandle;
5555

5656
public:
57-
Page() { data_ = static_cast<char *>(calloc(PAGE_SIZE, sizeof(char))); }
57+
Page() = default;
5858

59-
~Page() { delete[] data_; }
59+
~Page() = default;
6060

6161
PageId get_page_id() const { return id_; }
6262

@@ -89,7 +89,7 @@ class Page {
8989
/** The actual data that is stored within a page.
9090
* 该页面在bufferPool中的偏移地址
9191
*/
92-
char *data_;
92+
char data_[PAGE_SIZE];
9393

9494
/** 脏页判断 */
9595
bool is_dirty_ = false;

0 commit comments

Comments
 (0)