瀏覽代碼

Kernel: Use KString instead of String in Ext2FSInode's lookup cache

Idan Horowitz 3 年之前
父節點
當前提交
77a81f5eed
共有 2 個文件被更改,包括 9 次插入7 次删除
  1. 8 6
      Kernel/FileSystem/Ext2FileSystem.cpp
  2. 1 1
      Kernel/FileSystem/Ext2FileSystem.h

+ 8 - 6
Kernel/FileSystem/Ext2FileSystem.cpp

@@ -1174,7 +1174,8 @@ ErrorOr<void> Ext2FSInode::add_child(Inode& child, StringView name, mode_t mode)
     TRY(write_directory(entries));
     TRY(populate_lookup_cache());
 
-    TRY(m_lookup_cache.try_set(name, child.index()));
+    auto cache_entry_name = TRY(KString::try_create(name));
+    TRY(m_lookup_cache.try_set(move(cache_entry_name), child.index()));
     did_add_child(child.identifier(), name);
     return {};
 }
@@ -1187,7 +1188,7 @@ ErrorOr<void> Ext2FSInode::remove_child(StringView name)
 
     TRY(populate_lookup_cache());
 
-    auto it = m_lookup_cache.find(name);
+    auto it = m_lookup_cache.find(name.hash(), [&](auto& entry) { return entry.key->view() == name; });
     if (it == m_lookup_cache.end())
         return ENOENT;
     auto child_inode_index = (*it).value;
@@ -1205,7 +1206,7 @@ ErrorOr<void> Ext2FSInode::remove_child(StringView name)
 
     TRY(write_directory(entries));
 
-    m_lookup_cache.remove(name);
+    m_lookup_cache.remove(it);
 
     auto child_inode = TRY(fs().get_inode(child_id));
     TRY(child_inode->decrement_link_count());
@@ -1527,10 +1528,11 @@ ErrorOr<void> Ext2FSInode::populate_lookup_cache() const
     MutexLocker locker(m_inode_lock);
     if (!m_lookup_cache.is_empty())
         return {};
-    HashMap<String, InodeIndex> children;
+    HashMap<NonnullOwnPtr<KString>, InodeIndex> children;
 
     TRY(traverse_as_directory([&children](auto& entry) -> ErrorOr<void> {
-        TRY(children.try_set(entry.name, entry.inode.index()));
+        auto entry_name = TRY(KString::try_create(entry.name));
+        TRY(children.try_set(move(entry_name), entry.inode.index()));
         return {};
     }));
 
@@ -1548,7 +1550,7 @@ ErrorOr<NonnullRefPtr<Inode>> Ext2FSInode::lookup(StringView name)
     InodeIndex inode_index;
     {
         MutexLocker locker(m_inode_lock);
-        auto it = m_lookup_cache.find(name.hash(), [&](auto& entry) { return entry.key == name; });
+        auto it = m_lookup_cache.find(name.hash(), [&](auto& entry) { return entry.key->view() == name; });
         if (it == m_lookup_cache.end()) {
             dbgln_if(EXT2_DEBUG, "Ext2FSInode[{}]:lookup(): '{}' not found", identifier(), name);
             return ENOENT;

+ 1 - 1
Kernel/FileSystem/Ext2FileSystem.h

@@ -73,7 +73,7 @@ private:
     Ext2FSInode(Ext2FS&, InodeIndex);
 
     mutable Vector<BlockBasedFileSystem::BlockIndex> m_block_list;
-    mutable HashMap<String, InodeIndex> m_lookup_cache;
+    mutable HashMap<NonnullOwnPtr<KString>, InodeIndex> m_lookup_cache;
     ext2_inode m_raw_inode {};
 };