Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory optimization #104

Merged
merged 1 commit into from
Aug 24, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 16 additions & 8 deletions lib/user_agent_parser/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,24 @@ def parse(user_agent)
private

def load_patterns(path)
yml = YAML.load_file(path)

# Parse all the regexs
yml.each_pair do |_type, patterns|
patterns.each do |pattern|
pattern[:regex] = Regexp.new(pattern['regex'], pattern['regex_flag'] == 'i')
end
yml = begin
YAML.load_file(path, freeze: true)
rescue ArgumentError
YAML.load_file(path)
end
[
parse_pattern(yml['user_agent_parsers']),
parse_pattern(yml['os_parsers']),
parse_pattern(yml['device_parsers']),
]
end

[yml['user_agent_parsers'], yml['os_parsers'], yml['device_parsers']]
def parse_pattern(patterns)
patterns.map do |pattern|
pattern = pattern.dup
pattern[:regex] = Regexp.new(pattern.delete('regex'), pattern.delete('regex_flag') == 'i')
pattern
end
end

def parse_ua(user_agent, os = nil, device = nil)
Expand Down