Skip to content

Commit

Permalink
Store MIME parents in a distinct Hash
Browse files Browse the repository at this point in the history
Since most entries don't have a parent, this
allows to remove one array per entry, saving about 50KiB (~10%).
  • Loading branch information
byroot committed May 10, 2022
1 parent 9ee8f63 commit e413dae
Show file tree
Hide file tree
Showing 5 changed files with 1,169 additions and 909 deletions.
15 changes: 8 additions & 7 deletions lib/marcel/magic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ def initialize(type)
# * <i>:comment</i>: Comment string
def self.add(type, options)
extensions = [options[:extensions]].flatten.compact
TYPES[type] = [extensions,
[options[:parents]].flatten.compact,
options[:comment]]
TYPE_EXTS[type] = extensions
parents = [options[:parents]].flatten.compact
TYPE_PARENTS[type] = parents unless parents.empty?
extensions.each {|ext| EXTENSIONS[ext] = type }
MAGIC.unshift [type, options[:magic]] if options[:magic]
end
Expand All @@ -42,7 +42,8 @@ def self.add(type, options)
def self.remove(type)
EXTENSIONS.delete_if {|ext, t| t == type }
MAGIC.delete_if {|t, m| t == type }
TYPES.delete(type)
TYPE_EXTS.delete(type)
TYPE_PARENTS.delete(type)
end

# Returns true if type is a text format
Expand All @@ -60,12 +61,12 @@ def child_of?(parent)

# Get string list of file extensions
def extensions
TYPES.key?(type) ? TYPES[type][0] : []
TYPE_EXTS[type] || []
end

# Get mime comment
def comment
(TYPES.key?(type) ? TYPES[type][2] : nil).to_s
nil # deprecated
end

# Lookup mime type by file extension
Expand Down Expand Up @@ -110,7 +111,7 @@ def hash
alias == eql?

def self.child?(child, parent)
child == parent || TYPES.key?(child) && TYPES[child][1].any? {|p| child?(p, parent) }
child == parent || TYPE_PARENTS[child]&.any? {|p| child?(p, parent) }
end

def self.magic_match(io, method)
Expand Down
15 changes: 6 additions & 9 deletions lib/marcel/mime_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,9 @@ class MimeType

class << self
def extend(type, extensions: [], parents: [], magic: nil)
existing = Marcel::TYPES[type] || [[], [], ""]

extensions = (Array(extensions) + existing[0]).uniq
parents = (Array(parents) + existing[1]).uniq
comment = existing[2]

Magic.add(type, extensions: extensions, magic: magic, parents: parents, comment: comment)
extensions = (Array(extensions) + Array(Marcel::TYPE_EXTS[type])).uniq
parents = (Array(parents) + Array(Marcel::TYPE_PARENTS[type])).uniq
Magic.add(type, extensions: extensions, magic: magic, parents: parents)
end

# Returns the most appropriate content type for the given file.
Expand Down Expand Up @@ -42,6 +38,7 @@ def for(pathname_or_io = nil, name: nil, extension: nil, declared_type: nil)
end

private

def for_data(pathname_or_io)
if pathname_or_io
with_io(pathname_or_io) do |io|
Expand Down Expand Up @@ -103,10 +100,10 @@ def most_specific_type(from_magic_type, fallback_type)
end

def root_types(type)
if TYPES[type].nil? || TYPES[type][1].empty?
if TYPE_EXTS[type].nil? || TYPE_PARENTS[type].nil?
[ type ]
else
TYPES[type][1].map {|t| root_types t }.flatten
TYPE_PARENTS[type].map {|t| root_types t }.flatten
end
end
end
Expand Down
Loading

0 comments on commit e413dae

Please sign in to comment.