Skip to content

Commit 106ccd0

Browse files
authored
improvement: split implement of MemoryIO (#1272)
Signed-off-by: LHT129 <tianlan.lht@antgroup.com>
1 parent 3e69e7f commit 106ccd0

File tree

3 files changed

+62
-42
lines changed

3 files changed

+62
-42
lines changed

src/io/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
set (IO_SRC
1818
io_parameter.cpp
19+
memory_io.cpp
1920
memory_io_parameter.cpp
2021
memory_block_io_parameter.cpp
2122
buffer_io_parameter.cpp

src/io/memory_io.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
2+
// Copyright 2024-present the vsag project
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
#include "memory_io.h"
17+
18+
namespace vsag {
19+
20+
void
21+
MemoryIO::WriteImpl(const uint8_t* data, uint64_t size, uint64_t offset) {
22+
check_and_realloc(size + offset);
23+
memcpy(start_ + offset, data, size);
24+
}
25+
26+
bool
27+
MemoryIO::ReadImpl(uint64_t size, uint64_t offset, uint8_t* data) const {
28+
bool ret = check_valid_offset(size + offset);
29+
if (ret) {
30+
memcpy(data, start_ + offset, size);
31+
}
32+
return ret;
33+
}
34+
35+
const uint8_t*
36+
MemoryIO::DirectReadImpl(uint64_t size, uint64_t offset, bool& need_release) const {
37+
need_release = false;
38+
if (check_valid_offset(size + offset)) {
39+
return start_ + offset;
40+
}
41+
return nullptr;
42+
}
43+
bool
44+
MemoryIO::MultiReadImpl(uint8_t* datas, uint64_t* sizes, uint64_t* offsets, uint64_t count) const {
45+
bool ret = true;
46+
for (uint64_t i = 0; i < count; ++i) {
47+
ret &= this->ReadImpl(sizes[i], offsets[i], datas);
48+
datas += sizes[i];
49+
}
50+
return ret;
51+
}
52+
void
53+
MemoryIO::PrefetchImpl(uint64_t offset, uint64_t cache_line) {
54+
PrefetchLines(this->start_ + offset, cache_line);
55+
}
56+
} // namespace vsag

src/io/memory_io.h

Lines changed: 5 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -46,19 +46,19 @@ class MemoryIO : public BasicIO<MemoryIO> {
4646
this->allocator_->Deallocate(start_);
4747
}
4848

49-
inline void
49+
void
5050
WriteImpl(const uint8_t* data, uint64_t size, uint64_t offset);
5151

52-
inline bool
52+
bool
5353
ReadImpl(uint64_t size, uint64_t offset, uint8_t* data) const;
5454

55-
[[nodiscard]] inline const uint8_t*
55+
[[nodiscard]] const uint8_t*
5656
DirectReadImpl(uint64_t size, uint64_t offset, bool& need_release) const;
5757

58-
inline bool
58+
bool
5959
MultiReadImpl(uint8_t* datas, uint64_t* sizes, uint64_t* offsets, uint64_t count) const;
6060

61-
inline void
61+
void
6262
PrefetchImpl(uint64_t offset, uint64_t cache_line = 64);
6363

6464
private:
@@ -74,41 +74,4 @@ class MemoryIO : public BasicIO<MemoryIO> {
7474
private:
7575
uint8_t* start_{nullptr};
7676
};
77-
78-
void
79-
MemoryIO::WriteImpl(const uint8_t* data, uint64_t size, uint64_t offset) {
80-
check_and_realloc(size + offset);
81-
memcpy(start_ + offset, data, size);
82-
}
83-
84-
bool
85-
MemoryIO::ReadImpl(uint64_t size, uint64_t offset, uint8_t* data) const {
86-
bool ret = check_valid_offset(size + offset);
87-
if (ret) {
88-
memcpy(data, start_ + offset, size);
89-
}
90-
return ret;
91-
}
92-
93-
const uint8_t*
94-
MemoryIO::DirectReadImpl(uint64_t size, uint64_t offset, bool& need_release) const {
95-
need_release = false;
96-
if (check_valid_offset(size + offset)) {
97-
return start_ + offset;
98-
}
99-
return nullptr;
100-
}
101-
bool
102-
MemoryIO::MultiReadImpl(uint8_t* datas, uint64_t* sizes, uint64_t* offsets, uint64_t count) const {
103-
bool ret = true;
104-
for (uint64_t i = 0; i < count; ++i) {
105-
ret &= this->ReadImpl(sizes[i], offsets[i], datas);
106-
datas += sizes[i];
107-
}
108-
return ret;
109-
}
110-
void
111-
MemoryIO::PrefetchImpl(uint64_t offset, uint64_t cache_line) {
112-
PrefetchLines(this->start_ + offset, cache_line);
113-
}
11477
} // namespace vsag

0 commit comments

Comments
 (0)