Skip to content

Commit 1f3e962

Browse files
committed
Defaultly disable LMDB
1 parent b514b4f commit 1f3e962

File tree

5 files changed

+71
-19
lines changed

5 files changed

+71
-19
lines changed

include/luisa/runtime/rhi/device_interface.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ struct DeviceConfig {
8585
size_t device_index{std::numeric_limits<size_t>::max()};
8686
bool inqueue_buffer_limit{true};
8787
bool headless{false};
88+
bool use_lmdb{false};
8889
};
8990

9091
class DeviceExtension {

src/backends/common/default_binary_io.cpp

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

120-
DefaultBinaryIO::DefaultBinaryIO(Context &&ctx, bool headless) noexcept
120+
DefaultBinaryIO::DefaultBinaryIO(Context &&ctx, bool headless, bool use_lmdb) noexcept
121121
: _ctx(std::move(ctx)),
122-
_headless(headless) {
122+
_headless(headless),
123+
_use_lmdb(use_lmdb) {
123124
if (!headless) {
124125
_cache_dir = _ctx.create_runtime_subdir(".cache"sv);
125126
_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));
127+
if (use_lmdb) {
128+
_data_lmdb.create(_data_dir, std::max<size_t>(126ull, std::thread::hardware_concurrency() * 2));
129+
_cache_lmdb.create(_cache_dir, std::max<size_t>(126ull, std::thread::hardware_concurrency() * 2));
130+
}
128131
}
129132
}
130133

131134
DefaultBinaryIO::~DefaultBinaryIO() noexcept {
132135
if (!_headless) {
133-
_data_lmdb.destroy();
134-
_cache_lmdb.destroy();
136+
if (_use_lmdb) {
137+
_data_lmdb.destroy();
138+
_cache_lmdb.destroy();
139+
}
135140
}
136141
}
137142

@@ -145,15 +150,33 @@ luisa::unique_ptr<BinaryStream> DefaultBinaryIO::read_shader_bytecode(luisa::str
145150
}
146151

147152
luisa::unique_ptr<BinaryStream> DefaultBinaryIO::read_shader_cache(luisa::string_view name) const noexcept {
148-
auto r = _cache_lmdb->read(name);
149-
if (r.empty()) return {};
150-
return luisa::make_unique<LMDBBinaryStream>(r.data(), r.size());
153+
if (_use_lmdb) {
154+
auto r = _cache_lmdb->read(name);
155+
if (r.empty()) return {};
156+
return luisa::make_unique<LMDBBinaryStream>(r.data(), r.size());
157+
} else {
158+
std::filesystem::path local_path{name};
159+
if (local_path.is_absolute()) {
160+
return _read(luisa::to_string(name));
161+
}
162+
auto file_path = luisa::to_string(_cache_dir / name);
163+
return _read(file_path);
164+
}
151165
}
152166

153167
luisa::unique_ptr<BinaryStream> DefaultBinaryIO::read_internal_shader(luisa::string_view name) const noexcept {
154-
auto r = _data_lmdb->read(name);
155-
if (r.empty()) return {};
156-
return luisa::make_unique<LMDBBinaryStream>(r.data(), r.size());
168+
if (_use_lmdb) {
169+
auto r = _data_lmdb->read(name);
170+
if (r.empty()) return {};
171+
return luisa::make_unique<LMDBBinaryStream>(r.data(), r.size());
172+
} else {
173+
std::filesystem::path local_path{name};
174+
if (local_path.is_absolute()) {
175+
return _read(luisa::to_string(name));
176+
}
177+
auto file_path = luisa::to_string(_data_dir / name);
178+
return _read(file_path);
179+
}
157180
}
158181

159182
luisa::unique_ptr<BinaryStream> DefaultBinaryIO::read_shader_source(luisa::string_view name) const noexcept {
@@ -185,16 +208,39 @@ luisa::filesystem::path DefaultBinaryIO::write_shader_source(luisa::string_view
185208
}
186209

187210
luisa::filesystem::path DefaultBinaryIO::write_shader_cache(luisa::string_view name, luisa::span<std::byte const> data) const noexcept {
188-
_cache_lmdb->write(name, data);
189-
return _cache_dir / name;
211+
if (_use_lmdb) {
212+
_cache_lmdb->write(name, data);
213+
return _cache_dir / name;
214+
} else {
215+
std::filesystem::path local_path{name};
216+
if (local_path.is_absolute()) {
217+
_write(luisa::to_string(name), data);
218+
return local_path;
219+
}
220+
auto file_path = _cache_dir / name;
221+
_write(luisa::to_string(file_path), data);
222+
return file_path;
223+
}
190224
}
191225

192226
luisa::filesystem::path DefaultBinaryIO::write_internal_shader(luisa::string_view name, luisa::span<std::byte const> data) const noexcept {
193-
_data_lmdb->write(name, data);
194-
return _data_dir / name;
227+
if (_use_lmdb) {
228+
_data_lmdb->write(name, data);
229+
return _data_dir / name;
230+
} else {
231+
std::filesystem::path local_path{name};
232+
if (local_path.is_absolute()) {
233+
_write(luisa::to_string(name), data);
234+
return local_path;
235+
}
236+
auto file_path = _data_dir / name;
237+
_write(luisa::to_string(file_path), data);
238+
return file_path;
239+
}
195240
}
196241

197242
void DefaultBinaryIO::clear_shader_cache() const noexcept {
243+
if (!_use_lmdb) return;
198244
_cache_lmdb.destroy();
199245
std::error_code ec;
200246
for (auto &&dir : std::filesystem::directory_iterator(_cache_dir)) {

src/backends/common/default_binary_io.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class DefaultBinaryIO final : public BinaryIO {
2323
private:
2424
Context _ctx;
2525
bool _headless;
26+
bool _use_lmdb;
2627
mutable luisa::spin_mutex _global_mtx;
2728
mutable MutexMap _mutex_map;
2829
std::filesystem::path _cache_dir;
@@ -37,7 +38,7 @@ class DefaultBinaryIO final : public BinaryIO {
3738
void _unlock(MapIndex const &idx, bool is_write) const noexcept;
3839

3940
public:
40-
explicit DefaultBinaryIO(Context &&ctx, bool headless = false) noexcept;
41+
explicit DefaultBinaryIO(Context &&ctx, bool headless = false, bool use_lmdb = true) noexcept;
4142
~DefaultBinaryIO() noexcept override;
4243
luisa::unique_ptr<BinaryStream> read_shader_bytecode(luisa::string_view name) const noexcept override;
4344
luisa::unique_ptr<BinaryStream> read_shader_cache(luisa::string_view name) const noexcept override;

src/backends/dx/DXRuntime/Device.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,12 @@ Device::Device(Context &&ctx, DeviceConfig const *settings)
9292
using Microsoft::WRL::ComPtr;
9393
size_t index{std::numeric_limits<size_t>::max()};
9494
bool useRuntime = true;
95+
bool use_lmdb = false;
9596
if (settings) {
9697
index = settings->device_index;
9798
// auto select
9899
useRuntime = !settings->headless;
100+
use_lmdb = settings->use_lmdb;
99101
maxAllocatorCount = settings->inqueue_buffer_limit ? 2 : std::numeric_limits<size_t>::max();
100102
fileIo = settings->binary_io;
101103
profiler = settings->profiler;
@@ -111,7 +113,7 @@ Device::Device(Context &&ctx, DeviceConfig const *settings)
111113
gDxcRefCount++;
112114
}
113115
if (fileIo == nullptr) {
114-
serVisitor = vstd::make_unique<DefaultBinaryIO>(std::move(ctx), !useRuntime);
116+
serVisitor = vstd::make_unique<DefaultBinaryIO>(std::move(ctx), !useRuntime, use_lmdb);
115117
fileIo = serVisitor.get();
116118
}
117119
if (useRuntime) {

src/backends/vk/device.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,12 +275,14 @@ Device::Device(Context &&ctx_arg, DeviceConfig const *configs)
275275
set_bindless_kernel(BuiltinKernel::LoadBindlessSetKernel),
276276
set_accel_kernel(BuiltinKernel::LoadAccelSetKernel) {
277277
bool headless = false;
278+
bool use_lmdb = false;
278279
uint device_idx = -1;
279280
if (configs) {
280281
if (configs->extension) {
281282
_config_ext = luisa::unique_ptr<VulkanDeviceConfigExt>{reinterpret_cast<VulkanDeviceConfigExt *>(configs->extension.get())};
282283
}
283284
headless = configs->headless;
285+
use_lmdb = configs->use_lmdb;
284286
device_idx = configs->device_index;
285287
_binary_io = configs->binary_io;
286288
}
@@ -314,7 +316,7 @@ Device::Device(Context &&ctx_arg, DeviceConfig const *configs)
314316
gDxcRefCount++;
315317
}
316318
if (!_binary_io) {
317-
_default_file_io = vstd::make_unique<DefaultBinaryIO>(context(), headless);
319+
_default_file_io = vstd::make_unique<DefaultBinaryIO>(context(), headless, use_lmdb);
318320
_binary_io = _default_file_io.get();
319321
}
320322
if (!headless) {

0 commit comments

Comments
 (0)