Skip to content

Commit

Permalink
perf: lessen conditional branches
Browse files Browse the repository at this point in the history
  • Loading branch information
supercaracal committed Dec 7, 2024
1 parent bc11552 commit 5d57ae7
Showing 1 changed file with 15 additions and 26 deletions.
41 changes: 15 additions & 26 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' 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,29 +97,11 @@ 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?
cmd_name = command.first
if 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
Expand All @@ -121,7 +110,7 @@ def determine_first_key_position(command) # rubocop:disable Metrics/CyclomaticCo
elsif cmd_name.casecmp('xreadgroup').zero?
determine_optional_key_position(command, 'streams')
else
find_command_info(cmd_name)&.first_key_position.to_i
i
end
end

Expand Down

0 comments on commit 5d57ae7

Please sign in to comment.