Cleanup: Use Map instead of GHash for memfile writing #117141

Merged
Hans Goudey merged 4 commits from HooglyBoogly/blender:cleanup-undofile-cpp-map into main 2024-01-16 19:41:39 +01:00
3 changed files with 13 additions and 33 deletions

View File

@ -11,8 +11,8 @@
#include "BLI_filereader.h" #include "BLI_filereader.h"
#include "BLI_listbase.h" #include "BLI_listbase.h"
#include "BLI_map.hh"
struct GHash;
struct Main; struct Main;
struct Scene; struct Scene;
@ -45,7 +45,7 @@ struct MemFileWriteData {
MemFileChunk *reference_current_chunk; MemFileChunk *reference_current_chunk;
/** Maps an ID session uuid to its first reference MemFileChunk, if existing. */ /** Maps an ID session uuid to its first reference MemFileChunk, if existing. */
GHash *id_session_uuid_mapping; blender::Map<uint, MemFileChunk *> id_session_uuid_mapping;
}; };
struct MemFileUndoData { struct MemFileUndoData {

View File

@ -25,7 +25,6 @@
#include "DNA_listBase.h" #include "DNA_listBase.h"
#include "BLI_blenlib.h" #include "BLI_blenlib.h"
#include "BLI_ghash.h"
#include "BLO_readfile.h" #include "BLO_readfile.h"
#include "BLO_undofile.hh" #include "BLO_undofile.hh"
@ -54,13 +53,12 @@ void BLO_memfile_merge(MemFile *first, MemFile *second)
{ {
/* We use this mapping to store the memory buffers from second memfile chunks which are not owned /* We use this mapping to store the memory buffers from second memfile chunks which are not owned
* by it (i.e. shared with some previous memory steps). */ * by it (i.e. shared with some previous memory steps). */
GHash *buffer_to_second_memchunk = BLI_ghash_new( blender::Map<const char *, MemFileChunk *> buffer_to_second_memchunk;
BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp, __func__);
/* First, detect all memchunks in second memfile that are not owned by it. */ /* First, detect all memchunks in second memfile that are not owned by it. */
LISTBASE_FOREACH (MemFileChunk *, sc, &second->chunks) { LISTBASE_FOREACH (MemFileChunk *, sc, &second->chunks) {
if (sc->is_identical) { if (sc->is_identical) {
BLI_ghash_insert(buffer_to_second_memchunk, (void *)sc->buf, sc); buffer_to_second_memchunk.add(sc->buf, sc);
} }
} }
@ -68,9 +66,7 @@ void BLO_memfile_merge(MemFile *first, MemFile *second)
* it is also used by the second memfile, transfer the ownership. */ * it is also used by the second memfile, transfer the ownership. */
LISTBASE_FOREACH (MemFileChunk *, fc, &first->chunks) { LISTBASE_FOREACH (MemFileChunk *, fc, &first->chunks) {
if (!fc->is_identical) { if (!fc->is_identical) {
MemFileChunk *sc = static_cast<MemFileChunk *>( if (MemFileChunk *sc = buffer_to_second_memchunk.lookup_default(fc->buf, nullptr)) {
BLI_ghash_lookup(buffer_to_second_memchunk, fc->buf));
if (sc != nullptr) {
BLI_assert(sc->is_identical); BLI_assert(sc->is_identical);
sc->is_identical = false; sc->is_identical = false;
fc->is_identical = true; fc->is_identical = true;
@ -81,8 +77,6 @@ void BLO_memfile_merge(MemFile *first, MemFile *second)
} }
} }
BLI_ghash_free(buffer_to_second_memchunk, nullptr, nullptr);
BLO_memfile_free(first); BLO_memfile_free(first);
} }
@ -109,22 +103,11 @@ void BLO_memfile_write_init(MemFileWriteData *mem_data,
* current Main data-base broke the order matching with the memchunks from previous step. * current Main data-base broke the order matching with the memchunks from previous step.
*/ */
if (reference_memfile != nullptr) { if (reference_memfile != nullptr) {
mem_data->id_session_uuid_mapping = BLI_ghash_new(
BLI_ghashutil_inthash_p_simple, BLI_ghashutil_intcmp, __func__);
uint current_session_uuid = MAIN_ID_SESSION_UUID_UNSET; uint current_session_uuid = MAIN_ID_SESSION_UUID_UNSET;
LISTBASE_FOREACH (MemFileChunk *, mem_chunk, &reference_memfile->chunks) { LISTBASE_FOREACH (MemFileChunk *, mem_chunk, &reference_memfile->chunks) {
if (!ELEM(mem_chunk->id_session_uuid, MAIN_ID_SESSION_UUID_UNSET, current_session_uuid)) { if (!ELEM(mem_chunk->id_session_uuid, MAIN_ID_SESSION_UUID_UNSET, current_session_uuid)) {
current_session_uuid = mem_chunk->id_session_uuid; current_session_uuid = mem_chunk->id_session_uuid;
void **entry; mem_data->id_session_uuid_mapping.add_new(current_session_uuid, mem_chunk);
if (!BLI_ghash_ensure_p(mem_data->id_session_uuid_mapping,
POINTER_FROM_UINT(current_session_uuid),
&entry))
{
*entry = mem_chunk;
}
else {
BLI_assert_unreachable();
}
} }
} }
} }
@ -132,9 +115,7 @@ void BLO_memfile_write_init(MemFileWriteData *mem_data,
void BLO_memfile_write_finalize(MemFileWriteData *mem_data) void BLO_memfile_write_finalize(MemFileWriteData *mem_data)
{ {
if (mem_data->id_session_uuid_mapping != nullptr) { mem_data->id_session_uuid_mapping.clear_and_shrink();
BLI_ghash_free(mem_data->id_session_uuid_mapping, nullptr, nullptr);
}
} }
void BLO_memfile_chunk_add(MemFileWriteData *mem_data, const char *buf, size_t size) void BLO_memfile_chunk_add(MemFileWriteData *mem_data, const char *buf, size_t size)

View File

@ -435,7 +435,7 @@ struct BlendWriter {
static WriteData *writedata_new(WriteWrap *ww) static WriteData *writedata_new(WriteWrap *ww)
{ {
WriteData *wd = static_cast<WriteData *>(MEM_callocN(sizeof(*wd), "writedata")); WriteData *wd = MEM_new<WriteData>(__func__);
wd->sdna = DNA_sdna_current_get(); wd->sdna = DNA_sdna_current_get();
@ -487,7 +487,7 @@ static void writedata_free(WriteData *wd)
if (wd->buffer.buf) { if (wd->buffer.buf) {
MEM_freeN(wd->buffer.buf); MEM_freeN(wd->buffer.buf);
} }
MEM_freeN(wd); MEM_delete(wd);
} }
/** \} */ /** \} */
@ -620,14 +620,13 @@ static void mywrite_id_begin(WriteData *wd, ID *id)
MemFileChunk *prev_memchunk = curr_memchunk != nullptr ? MemFileChunk *prev_memchunk = curr_memchunk != nullptr ?
static_cast<MemFileChunk *>(curr_memchunk->prev) : static_cast<MemFileChunk *>(curr_memchunk->prev) :
nullptr; nullptr;
if (wd->mem.id_session_uuid_mapping != nullptr && if ((curr_memchunk == nullptr || curr_memchunk->id_session_uuid != id->session_uuid ||
(curr_memchunk == nullptr || curr_memchunk->id_session_uuid != id->session_uuid ||
(prev_memchunk != nullptr && (prev_memchunk != nullptr &&
(prev_memchunk->id_session_uuid == curr_memchunk->id_session_uuid)))) (prev_memchunk->id_session_uuid == curr_memchunk->id_session_uuid))))
{ {
void *ref = BLI_ghash_lookup(wd->mem.id_session_uuid_mapping, if (MemFileChunk *ref = wd->mem.id_session_uuid_mapping.lookup_default(id->session_uuid,
POINTER_FROM_UINT(id->session_uuid)); nullptr))
if (ref != nullptr) { {
wd->mem.reference_current_chunk = static_cast<MemFileChunk *>(ref); wd->mem.reference_current_chunk = static_cast<MemFileChunk *>(ref);
} }
/* Else, no existing memchunk found, i.e. this is supposed to be a new ID. */ /* Else, no existing memchunk found, i.e. this is supposed to be a new ID. */