Skip to content

perf: lessen conditional branches #416

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

Merged
merged 2 commits into from
Dec 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 19 additions & 30 deletions lib/redis_client/cluster/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,19 @@ def load(nodes, slow_command_timeout: -1) # rubocop:disable Metrics/AbcSize

private

def parse_command_reply(rows)
def parse_command_reply(rows) # rubocop:disable Metrics/CyclomaticComplexity
rows&.each_with_object({}) do |row, acc|
next if row[0].nil?
next if row.first.nil?

pos = case row.first
when 'eval', 'evalsha', 'zinterstore', 'zunionstore' then 3
when 'object' then 2
when 'migrate', 'xread', 'xreadgroup' then 0
else row[3]
end

acc[row.first] = ::RedisClient::Cluster::Command::Detail.new(
first_key_position: row[3],
first_key_position: pos,
key_step: row[5],
write?: row[2].include?('write'),
readonly?: row[2].include?('readonly')
Expand Down Expand Up @@ -90,38 +97,20 @@ def find_command_info(name)
end

def determine_first_key_position(command) # rubocop:disable Metrics/CyclomaticComplexity, Metrics/AbcSize, Metrics/PerceivedComplexity
cmd_name = command.first
i = find_command_info(command.first)&.first_key_position.to_i
return i if i > 0

if cmd_name.casecmp('get').zero?
find_command_info(cmd_name)&.first_key_position.to_i
elsif cmd_name.casecmp('mget').zero?
find_command_info(cmd_name)&.first_key_position.to_i
elsif cmd_name.casecmp('set').zero?
find_command_info(cmd_name)&.first_key_position.to_i
elsif cmd_name.casecmp('mset').zero?
find_command_info(cmd_name)&.first_key_position.to_i
elsif cmd_name.casecmp('del').zero?
find_command_info(cmd_name)&.first_key_position.to_i
elsif cmd_name.casecmp('eval').zero?
3
elsif cmd_name.casecmp('evalsha').zero?
3
elsif cmd_name.casecmp('zinterstore').zero?
3
elsif cmd_name.casecmp('zunionstore').zero?
3
elsif cmd_name.casecmp('object').zero?
2
elsif cmd_name.casecmp('memory').zero?
command[1].to_s.casecmp('usage').zero? ? 2 : 0
elsif cmd_name.casecmp('migrate').zero?
command[3].empty? ? determine_optional_key_position(command, 'keys') : 3
elsif cmd_name.casecmp('xread').zero?
cmd_name = command.first
if cmd_name.casecmp('xread').zero?
determine_optional_key_position(command, 'streams')
elsif cmd_name.casecmp('xreadgroup').zero?
determine_optional_key_position(command, 'streams')
elsif cmd_name.casecmp('migrate').zero?
command[3].empty? ? determine_optional_key_position(command, 'keys') : 3
elsif cmd_name.casecmp('memory').zero?
command[1].to_s.casecmp('usage').zero? ? 2 : 0
else
find_command_info(cmd_name)&.first_key_position.to_i
i
end
end

Expand Down
Loading