-
Notifications
You must be signed in to change notification settings - Fork 192
Lazy loading large assets #1923
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from 10 commits
adb7ec9
571c319
c9f1e1d
1ecca81
956617f
95c48f5
89a0054
32f0f37
a33a92e
ced3589
2b4a5b9
3e0802a
e5883a3
3be1580
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -17,8 +17,17 @@ struct Asset { | |||||||||||||||||||||
| explicit Asset(std::string k) : key(std::move(k)) {} | ||||||||||||||||||||||
| const std::string key; | ||||||||||||||||||||||
| std::vector<std::uint8_t> data; | ||||||||||||||||||||||
| std::filesystem::path path; | ||||||||||||||||||||||
| std::size_t size = 0; | ||||||||||||||||||||||
| std::uint32_t alignment = 1; | ||||||||||||||||||||||
| std::vector<std::uint8_t>& getData(); | ||||||||||||||||||||||
| std::size_t getSize() const; | ||||||||||||||||||||||
| std::string getRelativeUri(); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| private: | ||||||||||||||||||||||
| friend class AssetManager; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| bool dataLoaded = false; | ||||||||||||||||||||||
|
coderabbitai[bot] marked this conversation as resolved.
|
||||||||||||||||||||||
| }; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| class AssetsMutable : public Assets { | ||||||||||||||||||||||
|
|
@@ -127,6 +136,8 @@ class AssetManager /*: public Assets*/ { | |||||||||||||||||||||
|
|
||||||||||||||||||||||
| /// Serializes | ||||||||||||||||||||||
| void serialize(AssetsMutable& assets, std::vector<std::uint8_t>& assetStorage, std::string prefix = "") const; | ||||||||||||||||||||||
| /// Calculates the size of the serialized data | ||||||||||||||||||||||
| std::size_t getSerializedSize(std::size_t offset = 0) const; | ||||||||||||||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. needs docstring
Comment on lines
+142
to
+143
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value Document the The docstring answers the earlier request. It does not state what 📝 Proposed docstring- /// Calculates the size of the serialized data
+ /**
+ * Calculates the size of the serialized asset data.
+ *
+ * `@param` offset Starting offset in the aggregate asset storage
+ * `@returns` End offset after all assets, including alignment padding
+ * `@throws` std::runtime_error If an asset alignment is zero, or if the storage would exceed 4 GiB
+ */
std::size_t getSerializedSize(std::size_t offset = 0) const;📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||
| }; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| } // namespace dai | ||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -6,14 +6,75 @@ | |||||||||||||||||||||||
| #include "utility/spdlog-fmt.hpp" | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| // std | ||||||||||||||||||||||||
| #include <algorithm> | ||||||||||||||||||||||||
| #include <array> | ||||||||||||||||||||||||
| #include <fstream> | ||||||||||||||||||||||||
| #include <limits> | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| namespace dai { | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| namespace { | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| constexpr std::size_t MAX_ASSET_STORAGE_SIZE = std::numeric_limits<std::uint32_t>::max(); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| std::size_t getSerializedEndOffset(std::size_t offset, std::uint32_t alignment, std::size_t assetSize) { | ||||||||||||||||||||||||
| if(alignment == 0) { | ||||||||||||||||||||||||
| throw std::runtime_error("Asset alignment cannot be zero"); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| if(offset > MAX_ASSET_STORAGE_SIZE || assetSize > MAX_ASSET_STORAGE_SIZE) { | ||||||||||||||||||||||||
| throw std::runtime_error("Asset storage cannot exceed 4 GiB"); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| std::size_t padding = 0; | ||||||||||||||||||||||||
| if(alignment > 1 && offset % alignment != 0) { | ||||||||||||||||||||||||
| padding = alignment - (offset % alignment); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| if(padding > MAX_ASSET_STORAGE_SIZE - offset || assetSize > MAX_ASSET_STORAGE_SIZE - offset - padding) { | ||||||||||||||||||||||||
| throw std::runtime_error("Asset storage cannot exceed 4 GiB"); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| return offset + padding + assetSize; | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
Comment on lines
+20
to
+38
|
||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| } // namespace | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| std::string Asset::getRelativeUri() { | ||||||||||||||||||||||||
| return fmt::format("{}:{}", "asset", key); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| std::vector<std::uint8_t>& Asset::getData() { | ||||||||||||||||||||||||
| if(data.empty() && !dataLoaded && !path.empty()) { | ||||||||||||||||||||||||
| std::ifstream stream(path, std::ios::in | std::ios::binary); | ||||||||||||||||||||||||
| if(!stream.is_open()) { | ||||||||||||||||||||||||
| throw std::runtime_error(fmt::format("Cannot load asset, file at path {} doesn't exist.", path)); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| auto loadedData = std::vector<std::uint8_t>(size); | ||||||||||||||||||||||||
| std::size_t loadedSize = 0; | ||||||||||||||||||||||||
| while(loadedSize < size) { | ||||||||||||||||||||||||
| const auto bytesToRead = std::min<std::size_t>(size - loadedSize, 1024 * 1024); | ||||||||||||||||||||||||
| stream.read(reinterpret_cast<char*>(loadedData.data() + loadedSize), bytesToRead); | ||||||||||||||||||||||||
| const auto bytesRead = stream.gcount(); | ||||||||||||||||||||||||
| if(bytesRead != static_cast<std::streamsize>(bytesToRead)) { | ||||||||||||||||||||||||
| throw std::runtime_error(fmt::format("Cannot load asset, file at path {} has changed size.", path)); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| loadedSize += static_cast<std::size_t>(bytesRead); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| if(stream.peek() != std::char_traits<char>::eof()) { | ||||||||||||||||||||||||
| throw std::runtime_error(fmt::format("Cannot load asset, file at path {} has changed size.", path)); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| data = std::move(loadedData); | ||||||||||||||||||||||||
| dataLoaded = true; | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| return data; | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
Comment on lines
+46
to
+72
|
||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| std::size_t Asset::getSize() const { | ||||||||||||||||||||||||
| return path.empty() ? data.size() : size; | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| AssetManager::AssetManager() {} | ||||||||||||||||||||||||
| AssetManager::AssetManager(const std::string& rootPath) : rootPath{rootPath} {} | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
|
|
@@ -56,6 +117,9 @@ std::shared_ptr<dai::Asset> AssetManager::set(const std::string& key, Asset asse | |||||||||||||||||||||||
| // Rename the asset with supplied key and store | ||||||||||||||||||||||||
| Asset a(key); | ||||||||||||||||||||||||
| a.data = std::move(asset.data); | ||||||||||||||||||||||||
| a.path = std::move(asset.path); | ||||||||||||||||||||||||
| a.size = a.path.empty() ? 0 : asset.size; | ||||||||||||||||||||||||
| a.dataLoaded = asset.dataLoaded; | ||||||||||||||||||||||||
| a.alignment = asset.alignment; | ||||||||||||||||||||||||
| return set(std::move(a)); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
@@ -72,7 +136,8 @@ std::shared_ptr<dai::Asset> AssetManager::set(const std::string& key, const std: | |||||||||||||||||||||||
| // Create an asset | ||||||||||||||||||||||||
| Asset binaryAsset(key); | ||||||||||||||||||||||||
| binaryAsset.alignment = alignment; | ||||||||||||||||||||||||
| binaryAsset.data = std::vector<std::uint8_t>(std::istreambuf_iterator<char>(stream), {}); | ||||||||||||||||||||||||
| binaryAsset.path = path; | ||||||||||||||||||||||||
| binaryAsset.size = static_cast<std::size_t>(std::filesystem::file_size(path)); | ||||||||||||||||||||||||
| // Store asset | ||||||||||||||||||||||||
| return set(std::move(binaryAsset)); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
@@ -81,7 +146,7 @@ std::shared_ptr<dai::Asset> AssetManager::set(const std::string& key, const std: | |||||||||||||||||||||||
| // Create an asset | ||||||||||||||||||||||||
| Asset binaryAsset(key); | ||||||||||||||||||||||||
| binaryAsset.alignment = alignment; | ||||||||||||||||||||||||
| binaryAsset.data = std::move(data); | ||||||||||||||||||||||||
| binaryAsset.data = data; | ||||||||||||||||||||||||
| // Store asset | ||||||||||||||||||||||||
| return set(std::move(binaryAsset)); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
@@ -151,27 +216,75 @@ void AssetManager::serialize(AssetsMutable& mutableAssets, std::vector<std::uint | |||||||||||||||||||||||
| prefix = rootPath; | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| for(auto& kv : assetMap) { | ||||||||||||||||||||||||
| auto& a = *kv.second; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| // calculate additional bytes needed to offset to alignment | ||||||||||||||||||||||||
| int toAdd = 0; | ||||||||||||||||||||||||
| if(a.alignment > 1 && storage.size() % a.alignment != 0) { | ||||||||||||||||||||||||
| toAdd = a.alignment - (storage.size() % a.alignment); | ||||||||||||||||||||||||
| const auto storageStart = storage.size(); | ||||||||||||||||||||||||
| const auto mutableAssetsStart = mutableAssets; | ||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||
| for(auto& kv : assetMap) { | ||||||||||||||||||||||||
| auto& a = *kv.second; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| const auto assetSize = a.getSize(); | ||||||||||||||||||||||||
| const auto assetStorageStart = storage.size(); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| // Calculate additional bytes needed to offset to alignment. | ||||||||||||||||||||||||
| std::size_t toAdd = 0; | ||||||||||||||||||||||||
| if(a.alignment > 1 && storage.size() % a.alignment != 0) { | ||||||||||||||||||||||||
| toAdd = a.alignment - (storage.size() % a.alignment); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| const auto storageEnd = getSerializedEndOffset(storage.size(), a.alignment, assetSize); | ||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 📐 Maintainability & Code Quality | 🟠 Major | ⚡ Quick win Remove the duplicated alignment-padding formula. Lines 229-232 recompute the padding that Extract the padding into a shared helper and use it in both places. ♻️ Proposed refactor+std::size_t getAlignmentPadding(std::size_t offset, std::uint32_t alignment) {
+ if(alignment > 1 && offset % alignment != 0) {
+ return alignment - (offset % alignment);
+ }
+ return 0;
+}
+
std::size_t getSerializedEndOffset(std::size_t offset, std::uint32_t alignment, std::size_t assetSize) {
if(alignment == 0) {
throw std::runtime_error("Asset alignment cannot be zero");
}
if(offset > MAX_ASSET_STORAGE_SIZE || assetSize > MAX_ASSET_STORAGE_SIZE) {
throw std::runtime_error("Asset storage cannot exceed 4 GiB");
}
- std::size_t padding = 0;
- if(alignment > 1 && offset % alignment != 0) {
- padding = alignment - (offset % alignment);
- }
+ const std::size_t padding = getAlignmentPadding(offset, alignment);Then in - // Calculate additional bytes needed to offset to alignment.
- std::size_t toAdd = 0;
- if(a.alignment > 1 && storage.size() % a.alignment != 0) {
- toAdd = a.alignment - (storage.size() % a.alignment);
- }
-
const auto storageEnd = getSerializedEndOffset(storage.size(), a.alignment, assetSize);
+ // Calculate additional bytes needed to offset to alignment.
+ const std::size_t toAdd = getAlignmentPadding(storage.size(), a.alignment);
storage.reserve(storageEnd);Note that 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||
| storage.reserve(storageEnd); | ||||||||||||||||||||||||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| // calculate offset | ||||||||||||||||||||||||
| std::uint32_t offset = static_cast<uint32_t>(storage.size()) + toAdd; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| // Add alignment bytes | ||||||||||||||||||||||||
| storage.resize(storage.size() + toAdd); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| if(!a.path.empty()) { | ||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||
| std::ifstream stream(a.path, std::ios::in | std::ios::binary); | ||||||||||||||||||||||||
| if(!stream.is_open()) { | ||||||||||||||||||||||||
| throw std::runtime_error(fmt::format("Cannot load asset, file at path {} doesn't exist.", a.path)); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| std::vector<std::uint8_t> buffer(1024 * 1024); | ||||||||||||||||||||||||
| std::size_t streamedSize = 0; | ||||||||||||||||||||||||
| while(streamedSize < assetSize) { | ||||||||||||||||||||||||
| const auto bytesToRead = std::min(buffer.size(), assetSize - streamedSize); | ||||||||||||||||||||||||
| stream.read(reinterpret_cast<char*>(buffer.data()), bytesToRead); | ||||||||||||||||||||||||
| auto bytesRead = stream.gcount(); | ||||||||||||||||||||||||
| if(bytesRead != static_cast<std::streamsize>(bytesToRead)) { | ||||||||||||||||||||||||
| throw std::runtime_error(fmt::format("Asset at path {} changed while serializing.", a.path)); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| storage.insert(storage.end(), buffer.data(), buffer.data() + bytesRead); | ||||||||||||||||||||||||
| streamedSize += static_cast<std::size_t>(bytesRead); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| if(stream.peek() != std::char_traits<char>::eof()) { | ||||||||||||||||||||||||
| throw std::runtime_error(fmt::format("Asset at path {} changed while serializing.", a.path)); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| } catch(...) { | ||||||||||||||||||||||||
| storage.resize(assetStorageStart); | ||||||||||||||||||||||||
| throw; | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||
| storage.insert(storage.end(), a.data.begin(), a.data.end()); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| // Add to map the currently added asset | ||||||||||||||||||||||||
| mutableAssets.set(prefix + a.key, offset, static_cast<uint32_t>(assetSize), a.alignment); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| } catch(...) { | ||||||||||||||||||||||||
| storage.resize(storageStart); | ||||||||||||||||||||||||
| mutableAssets = mutableAssetsStart; | ||||||||||||||||||||||||
| throw; | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| // calculate offset | ||||||||||||||||||||||||
| std::uint32_t offset = static_cast<uint32_t>(storage.size()) + toAdd; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| // Add alignment bytes | ||||||||||||||||||||||||
| storage.resize(storage.size() + toAdd); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| // copy data | ||||||||||||||||||||||||
| storage.insert(storage.end(), a.data.begin(), a.data.end()); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| // Add to map the currently added asset | ||||||||||||||||||||||||
| mutableAssets.set(prefix + a.key, offset, static_cast<uint32_t>(a.data.size()), a.alignment); | ||||||||||||||||||||||||
| std::size_t AssetManager::getSerializedSize(std::size_t offset) const { | ||||||||||||||||||||||||
| for(const auto& kv : assetMap) { | ||||||||||||||||||||||||
| const auto& a = *kv.second; | ||||||||||||||||||||||||
| offset = getSerializedEndOffset(offset, a.alignment, a.getSize()); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| return offset; | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| void AssetsMutable::set(const std::string& key, std::uint32_t offset, std::uint32_t size, std::uint32_t alignment) { | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change was necessary as
std::vector<uint8_t>is declared as opaque so the previous version of the binding didn't work.