Skip to content

Commit 2a15ec6

Browse files
committed
Cull LMDB construct in headless mode
1 parent 2c4966d commit 2a15ec6

File tree

5 files changed

+33
-16
lines changed

5 files changed

+33
-16
lines changed

include/luisa/vstl/lmdb.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ class LC_VSTL_API LMDB {
6363
void _dispose() noexcept;
6464

6565
public:
66+
LMDB();
6667
LMDB(
6768
std::filesystem::path const &db_dir,
6869
size_t max_reader = 126ull,
@@ -76,6 +77,12 @@ class LC_VSTL_API LMDB {
7677
std::construct_at(this, std::move(rhs));
7778
return *this;
7879
}
80+
[[nodiscard]] operator bool() const {
81+
return _env != nullptr;
82+
}
83+
[[nodiscard]] bool operator==(std::nullptr_t) const {
84+
return _env == nullptr;
85+
}
7986
[[nodiscard]] luisa::span<const std::byte> read(luisa::span<const std::byte> key) const noexcept;
8087
[[nodiscard]] luisa::span<const std::byte> read(luisa::string_view key) const noexcept {
8188
return read(luisa::span{reinterpret_cast<std::byte const *>(key.data()), key.size()});

src/backends/common/default_binary_io.cpp

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -117,15 +117,23 @@ void DefaultBinaryIO::_write(const luisa::string &file_path, luisa::span<std::by
117117
_unlock(idx, true);
118118
}
119119

120-
DefaultBinaryIO::DefaultBinaryIO(Context &&ctx, void *ext) noexcept
120+
DefaultBinaryIO::DefaultBinaryIO(Context &&ctx, bool headless) noexcept
121121
: _ctx(std::move(ctx)),
122-
_cache_dir{_ctx.create_runtime_subdir(".cache"sv)},
123-
_data_dir{_ctx.create_runtime_subdir(".data"sv)},
124-
_data_lmdb{_data_dir, std::max<size_t>(126ull, std::thread::hardware_concurrency() * 2)},
125-
_cache_lmdb{_cache_dir, std::max<size_t>(126ull, std::thread::hardware_concurrency() * 2)} {
122+
_headless(headless) {
123+
if (!headless) {
124+
_cache_dir = _ctx.create_runtime_subdir(".cache"sv);
125+
_data_dir = _ctx.create_runtime_subdir(".data"sv);
126+
_data_lmdb.create(_data_dir, std::max<size_t>(126ull, std::thread::hardware_concurrency() * 2));
127+
_cache_lmdb.create(_cache_dir, std::max<size_t>(126ull, std::thread::hardware_concurrency() * 2));
128+
}
126129
}
127130

128-
DefaultBinaryIO::~DefaultBinaryIO() noexcept = default;
131+
DefaultBinaryIO::~DefaultBinaryIO() noexcept {
132+
if (!_headless) {
133+
_data_lmdb.destroy();
134+
_cache_lmdb.destroy();
135+
}
136+
}
129137

130138
luisa::unique_ptr<BinaryStream> DefaultBinaryIO::read_shader_bytecode(luisa::string_view name) const noexcept {
131139
std::filesystem::path local_path{name};
@@ -137,13 +145,13 @@ luisa::unique_ptr<BinaryStream> DefaultBinaryIO::read_shader_bytecode(luisa::str
137145
}
138146

139147
luisa::unique_ptr<BinaryStream> DefaultBinaryIO::read_shader_cache(luisa::string_view name) const noexcept {
140-
auto r = _cache_lmdb.read(name);
148+
auto r = _cache_lmdb->read(name);
141149
if (r.empty()) return {};
142150
return luisa::make_unique<LMDBBinaryStream>(r.data(), r.size());
143151
}
144152

145153
luisa::unique_ptr<BinaryStream> DefaultBinaryIO::read_internal_shader(luisa::string_view name) const noexcept {
146-
auto r = _data_lmdb.read(name);
154+
auto r = _data_lmdb->read(name);
147155
if (r.empty()) return {};
148156
return luisa::make_unique<LMDBBinaryStream>(r.data(), r.size());
149157
}
@@ -177,17 +185,17 @@ luisa::filesystem::path DefaultBinaryIO::write_shader_source(luisa::string_view
177185
}
178186

179187
luisa::filesystem::path DefaultBinaryIO::write_shader_cache(luisa::string_view name, luisa::span<std::byte const> data) const noexcept {
180-
_cache_lmdb.write(name, data);
188+
_cache_lmdb->write(name, data);
181189
return _cache_dir / name;
182190
}
183191

184192
luisa::filesystem::path DefaultBinaryIO::write_internal_shader(luisa::string_view name, luisa::span<std::byte const> data) const noexcept {
185-
_data_lmdb.write(name, data);
193+
_data_lmdb->write(name, data);
186194
return _data_dir / name;
187195
}
188196

189197
void DefaultBinaryIO::clear_shader_cache() const noexcept {
190-
std::destroy_at(std::addressof(_cache_lmdb));
198+
_cache_lmdb.destroy();
191199
std::error_code ec;
192200
for (auto &&dir : std::filesystem::directory_iterator(_cache_dir)) {
193201
std::filesystem::remove_all(dir, ec);
@@ -197,7 +205,7 @@ void DefaultBinaryIO::clear_shader_cache() const noexcept {
197205
to_string(dir), ec.message());
198206
}
199207
}
200-
std::construct_at(&_cache_lmdb, _cache_dir);
208+
_cache_lmdb.create(_cache_dir);
201209
}
202210

203211
}// namespace luisa::compute

src/backends/common/default_binary_io.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,13 @@ class DefaultBinaryIO final : public BinaryIO {
2222

2323
private:
2424
Context _ctx;
25+
bool _headless;
2526
mutable luisa::spin_mutex _global_mtx;
2627
mutable MutexMap _mutex_map;
2728
std::filesystem::path _cache_dir;
2829
std::filesystem::path _data_dir;
29-
mutable vstd::LMDB _data_lmdb;
30-
mutable vstd::LMDB _cache_lmdb;
30+
mutable vstd::StackObject<vstd::LMDB> _data_lmdb;
31+
mutable vstd::StackObject<vstd::LMDB> _cache_lmdb;
3132

3233
private:
3334
luisa::unique_ptr<BinaryStream> _read(luisa::string const &file_path) const noexcept;
@@ -36,7 +37,7 @@ class DefaultBinaryIO final : public BinaryIO {
3637
void _unlock(MapIndex const &idx, bool is_write) const noexcept;
3738

3839
public:
39-
explicit DefaultBinaryIO(Context &&ctx, void *ext = nullptr) noexcept;
40+
explicit DefaultBinaryIO(Context &&ctx, bool headless = false) noexcept;
4041
~DefaultBinaryIO() noexcept override;
4142
luisa::unique_ptr<BinaryStream> read_shader_bytecode(luisa::string_view name) const noexcept override;
4243
luisa::unique_ptr<BinaryStream> read_shader_cache(luisa::string_view name) const noexcept override;

src/backends/dx/DXRuntime/Device.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ Device::Device(Context &&ctx, DeviceConfig const *settings)
109109
}
110110
}
111111
if (fileIo == nullptr) {
112-
serVisitor = vstd::make_unique<DefaultBinaryIO>(std::move(ctx), device.Get());
112+
serVisitor = vstd::make_unique<DefaultBinaryIO>(std::move(ctx), !useRuntime);
113113
fileIo = serVisitor.get();
114114
}
115115
if (useRuntime) {

src/vstl/lmdb.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ LMDB::LMDB(LMDB &&rhs) noexcept
8080
_flag(rhs._flag) {
8181
rhs._env = nullptr;
8282
}
83+
LMDB::LMDB() = default;
8384
void LMDB::_dispose() noexcept {
8485
if (_env) {
8586
if (_dbi) {

0 commit comments

Comments
 (0)