Skip to content

Commit 5884cbc

Browse files
committed
Optimize skip logic in #update_all_module_details
MSP-11368 Use `Hash<String, Set<String>>` instead of `Array<(String, String)>` so that `include?` call is faster because (1) it's only search through reference names of the same module_type and (2) `Set#include?` is faster than `Array#include?`. This change is a 8.20% average reduction in boot time compare to b863978, for a overall reduction of 40.95% over b5c3c87. See statistics at https://docs.google.com/spreadsheets/d/1TnZIUFIR1S5nCnkeM-7XR3AVSbyCl39x2mItJKJCOqg/edit?usp=sharing and data at https://drive.google.com/folderview?id=0Bx1hRHfpRW92VEFvQ2FaN3RoWWs&usp=drive_web
1 parent 8b5a146 commit 5884cbc

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

lib/msf/core/db_manager.rb

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,9 @@ def update_all_module_details
366366
ActiveRecord::Base.connection_pool.with_connection do
367367

368368
refresh = []
369-
skipped = []
369+
skip_reference_name_set_by_module_type = Hash.new { |hash, module_type|
370+
hash[module_type] = Set.new
371+
}
370372

371373
Mdm::Module::Detail.find_each do |md|
372374

@@ -385,7 +387,8 @@ def update_all_module_details
385387
next
386388
end
387389

388-
skipped << [md.mtype, md.refname]
390+
skip_reference_name_set = skip_reference_name_set_by_module_type[md.mtype]
391+
skip_reference_name_set.add(md.refname)
389392
end
390393

391394
refresh.each { |md| md.destroy }
@@ -398,8 +401,10 @@ def update_all_module_details
398401
['encoder', framework.encoders],
399402
['nop', framework.nops]
400403
].each do |mt|
404+
skip_reference_name_set = skip_reference_name_set_by_module_type[mt[0]]
405+
401406
mt[1].keys.sort.each do |mn|
402-
next if skip?(skipped, [mt[0], mn])
407+
next if skip_reference_name_set.include? mn
403408
obj = mt[1].create(mn)
404409
next if not obj
405410
begin
@@ -419,10 +424,6 @@ def update_all_module_details
419424
self.framework.cache_thread = nil
420425
end
421426

422-
def skip?(skipped, entry)
423-
skipped.include?(entry)
424-
end
425-
426427
# Creates an Mdm::Module::Detail from a module instance.
427428
#
428429
# @param module_instance [Msf::Module] a metasploit module instance.

0 commit comments

Comments
 (0)