From c22d54c5d95f4d23629484579414387b0c7503ee Mon Sep 17 00:00:00 2001 From: Mason Reed Date: Sun, 6 Apr 2025 20:00:00 -0400 Subject: [SharedCache] Replace write log with callback registration for reapplying slide info This improves performance by ~10x with the only real issue being a copy of the cache entry existing for each weak ref. Also fixes - Some old TODO's that are no longer relevant - Some function signatures not being const - FileAccessorCache not evicting enough accessors to fit under an adjusted cache size - Added a warning if we are over the global cache size in view initialization - Logger name not having a `.` in `SlideInfoProcessor::SlideInfoProcessor` - Re-added the accessor dirty check before applying slide info to fix https://github.com/Vector35/binaryninja-api/issues/6570 --- view/sharedcache/core/FileAccessorCache.h | 36 +++++++++++-------------------- 1 file changed, 12 insertions(+), 24 deletions(-) (limited to 'view/sharedcache/core/FileAccessorCache.h') diff --git a/view/sharedcache/core/FileAccessorCache.h b/view/sharedcache/core/FileAccessorCache.h index 0ef6595a..811aea78 100644 --- a/view/sharedcache/core/FileAccessorCache.h +++ b/view/sharedcache/core/FileAccessorCache.h @@ -37,48 +37,36 @@ public: // Adjust the cache size limit. // This will NOT evict current cache entries, as they are already available. // Any subsequent call to `Open` will assume this cache size, evicting until the size is equal to the cache size. - void SetCacheSize(uint64_t size) { m_cacheSize = size; }; -}; - -// Write log to be used in conjunction with `WeakFileAccessor` to re-apply written data to a "revived" file. -struct FileAccessorWriteLog -{ - // To persist writes to a file accessor being revived (within the lock() function) - // we keep a list of writes that will be re-applied in the lock function. - std::shared_mutex m_persistedMutex; - std::vector> m_persistedPointers; - - FileAccessorWriteLog() = default; + void SetCacheSize(const uint64_t size) { m_cacheSize = size; }; - // Add the pointer to the persisted pointers. - void AddPointer(size_t address, size_t pointer); - - // Apply all logged writes to the given accessor. - void ApplyWrites(MappedFileAccessor& accessor); + size_t GetCacheSize() const { return m_cacheSize; } }; class WeakFileAccessor { + using ReviveCallback = std::function; + // Weak pointer to the mapped file accessor, once this is expired we will re-open. std::weak_ptr m_weakPtr; // File path for re-opening if needed std::string m_filePath; // Used to re-add writes once the file accessor is "revived". - std::shared_ptr m_writeLog; + std::optional m_reviveCallback; // TODO: Store a weak_ptr/shared_ptr to FileAccessorCache? That way we dont access Global() // TODO: Only need to do the above if we want multiple caches. public: explicit WeakFileAccessor(std::weak_ptr weakPtr, std::string filePath) : - m_weakPtr(std::move(weakPtr)), m_filePath(std::move(filePath)), - m_writeLog(std::make_shared()) + m_weakPtr(std::move(weakPtr)), m_filePath(std::move(filePath)) {} - std::shared_ptr lock(); + // Register the function to be called once the file accessor is revived, this is typically + // used to re-apply writes such as from slide info. + void RegisterReviveCallback(const ReviveCallback& callback) { + m_reviveCallback = callback; + } - // Persists the written pointer within this weak file accessor. - // This works as we expect the weak file accessor to be stored per virtual memory region. - void WritePointer(size_t address, size_t pointer); + std::shared_ptr lock(); }; -- cgit v1.3.1