glTF2: Avoid lookups for generating IDs (#6120)

* glTF2: Reduce lookups for generating IDs
- closes https://github.com/assimp/assimp/issues/5661
This commit is contained in:
Jerome St-Louis
2025-11-13 04:50:26 -05:00
committed by GitHub
parent c6f997b584
commit 0be7cdd6db
7 changed files with 27 additions and 38 deletions

View File

@@ -49,7 +49,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <set>
//
#if _MSC_VER > 1500 || (defined __GNUC___)
#if _MSC_VER > 1500 || (defined __GNUC__)
# define ASSIMP_FBX_USE_UNORDERED_MULTIMAP
# else
# define fbx_unordered_map map

View File

@@ -58,7 +58,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# pragma warning(disable : 4127 4456 4245 4512 )
#endif // _MSC_VER
#if _MSC_VER > 1500 || (defined __GNUC___)
#if _MSC_VER > 1500 || (defined __GNUC__)
# define ASSIMP_STEP_USE_UNORDERED_MULTIMAP
#else
# define step_unordered_map map

View File

@@ -63,7 +63,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <memory>
//
#if _MSC_VER > 1500 || (defined __GNUC___)
#if _MSC_VER > 1500 || (defined __GNUC__)
# define ASSIMP_STEP_USE_UNORDERED_MULTIMAP
# else
# define step_unordered_map map

View File

@@ -83,7 +83,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#endif
#if _MSC_VER > 1500 || (defined __GNUC___)
#if _MSC_VER > 1500 || (defined __GNUC__)
# define ASSIMP_GLTF_USE_UNORDERED_MULTIMAP
# else
# define gltf_unordered_map map

View File

@@ -64,7 +64,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <algorithm>
#include <list>
#include <map>
#include <unordered_map>
#include <set>
#include <stdexcept>
#include <string>
@@ -95,7 +95,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# define ai_assert
#endif
#if _MSC_VER > 1500 || (defined __GNUC___)
#if _MSC_VER > 1500 || (defined __GNUC__)
# define ASSIMP_GLTF_USE_UNORDERED_MULTIMAP
#else
# define gltf_unordered_map map
@@ -1266,9 +1266,8 @@ private:
size_t mSceneLength;
size_t mBodyOffset;
size_t mBodyLength;
IdMap mUsedIds;
std::map<std::string, int, std::less<>> mUsedNamesMap;
Ref<Buffer> mBodyBuffer;
std::unordered_map<std::string, int> lastUsedID;
};
inline std::string getContextForErrorMessages(const std::string &id, const std::string &name) {

View File

@@ -438,7 +438,6 @@ unsigned int LazyDict<T>::Remove(const char *id) {
const unsigned int index = objIt->second;
mAsset.mUsedIds[id] = false;
mObjsById.erase(id);
mObjsByOIndex.erase(index);
delete mObjs[index];
@@ -472,7 +471,6 @@ unsigned int LazyDict<T>::Remove(const char *id) {
template <class T>
Ref<T> LazyDict<T>::Retrieve(unsigned int i) {
typename Dict::iterator it = mObjsByOIndex.find(i);
if (it != mObjsByOIndex.end()) { // already created?
return Ref<T>(mObjs, it->second);
@@ -540,16 +538,11 @@ Ref<T> LazyDict<T>::Add(T *obj) {
mObjs.push_back(obj);
mObjsByOIndex[obj->oIndex] = idx;
mObjsById[obj->id] = idx;
mAsset.mUsedIds[obj->id] = true;
return Ref<T>(mObjs, idx);
}
template <class T>
Ref<T> LazyDict<T>::Create(const char *id) {
Asset::IdMap::iterator it = mAsset.mUsedIds.find(id);
if (it != mAsset.mUsedIds.end()) {
throw DeadlyImportError("GLTF: two objects with the same ID exist");
}
T *inst = new T();
unsigned int idx = unsigned(mObjs.size());
inst->id = id;
@@ -565,11 +558,12 @@ inline Buffer::Buffer() :
byteLength(0),
type(Type_arraybuffer),
EncodedRegion_Current(nullptr),
mIsSpecial(false) {}
mIsSpecial(false) {
// empty
}
inline Buffer::~Buffer() {
for (SEncodedRegion *reg : EncodedRegion_List)
delete reg;
for (SEncodedRegion *reg : EncodedRegion_List) delete reg;
}
inline const char *Buffer::TranslateId(Asset & /*r*/, const char *id) {
@@ -693,7 +687,6 @@ inline void Buffer::EncodedRegion_SetCurrent(const std::string &pID) {
}
inline bool Buffer::ReplaceData(const size_t pBufferData_Offset, const size_t pBufferData_Count, const uint8_t *pReplace_Data, const size_t pReplace_Count) {
if ((pBufferData_Count == 0) || (pReplace_Count == 0) || (pReplace_Data == nullptr)) {
return false;
}
@@ -809,8 +802,7 @@ inline uint8_t *BufferView::GetPointerAndTailSize(size_t accOffset, size_t& outT
}
}
if (offset >= buffer->byteLength)
{
if (offset >= buffer->byteLength) {
outTailSize = 0;
return nullptr;
}
@@ -2206,29 +2198,27 @@ inline IOStream *Asset::OpenFile(const std::string &path, const char *mode, bool
inline std::string Asset::FindUniqueID(const std::string &str, const char *suffix) {
std::string id = str;
if (!id.empty()) {
if (mUsedIds.find(id) == mUsedIds.end()){
mUsedNamesMap[id] = 0;
int n = 1;
if(!id.empty()) {
n = lastUsedID[id];
if(!n) {
lastUsedID[id] = n+1;
return id;
}
id += "_";
}
id += suffix;
Asset::IdMap::iterator it = mUsedIds.find(id);
if (it == mUsedIds.end()) {
mUsedNamesMap[id] = 0;
return id;
if(suffix) {
id += suffix;
n = lastUsedID[id];
if(!n) {
lastUsedID[id] = n+1;
return id;
}
}
auto key = id;
id += "_" + std::to_string(mUsedNamesMap[key]);
mUsedNamesMap[key] = mUsedNamesMap[key] + 1;
return id;
lastUsedID[id] = n+1;
return id + "_" + std::to_string(n-1);
}
#if _MSC_VER

View File

@@ -69,7 +69,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# define ai_assert
#endif
#if _MSC_VER > 1500 || (defined __GNUC___)
#if _MSC_VER > 1500 || (defined __GNUC__)
# define ASSIMP_GLTF_USE_UNORDERED_MULTIMAP
#else
# define gltf_unordered_map map