Skip to content

Commit 7e8cec6

Browse files
authored
perf: lessen conditional branches (#416)
1 parent bc11552 commit 7e8cec6

File tree

1 file changed

+19
-30
lines changed

1 file changed

+19
-30
lines changed

lib/redis_client/cluster/command.rb

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,19 @@ def load(nodes, slow_command_timeout: -1) # rubocop:disable Metrics/AbcSize
4646

4747
private
4848

49-
def parse_command_reply(rows)
49+
def parse_command_reply(rows) # rubocop:disable Metrics/CyclomaticComplexity
5050
rows&.each_with_object({}) do |row, acc|
51-
next if row[0].nil?
51+
next if row.first.nil?
52+
53+
pos = case row.first
54+
when 'eval', 'evalsha', 'zinterstore', 'zunionstore' then 3
55+
when 'object' then 2
56+
when 'migrate', 'xread', 'xreadgroup' then 0
57+
else row[3]
58+
end
5259

5360
acc[row.first] = ::RedisClient::Cluster::Command::Detail.new(
54-
first_key_position: row[3],
61+
first_key_position: pos,
5562
key_step: row[5],
5663
write?: row[2].include?('write'),
5764
readonly?: row[2].include?('readonly')
@@ -90,38 +97,20 @@ def find_command_info(name)
9097
end
9198

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

95-
if cmd_name.casecmp('get').zero?
96-
find_command_info(cmd_name)&.first_key_position.to_i
97-
elsif cmd_name.casecmp('mget').zero?
98-
find_command_info(cmd_name)&.first_key_position.to_i
99-
elsif cmd_name.casecmp('set').zero?
100-
find_command_info(cmd_name)&.first_key_position.to_i
101-
elsif cmd_name.casecmp('mset').zero?
102-
find_command_info(cmd_name)&.first_key_position.to_i
103-
elsif cmd_name.casecmp('del').zero?
104-
find_command_info(cmd_name)&.first_key_position.to_i
105-
elsif cmd_name.casecmp('eval').zero?
106-
3
107-
elsif cmd_name.casecmp('evalsha').zero?
108-
3
109-
elsif cmd_name.casecmp('zinterstore').zero?
110-
3
111-
elsif cmd_name.casecmp('zunionstore').zero?
112-
3
113-
elsif cmd_name.casecmp('object').zero?
114-
2
115-
elsif cmd_name.casecmp('memory').zero?
116-
command[1].to_s.casecmp('usage').zero? ? 2 : 0
117-
elsif cmd_name.casecmp('migrate').zero?
118-
command[3].empty? ? determine_optional_key_position(command, 'keys') : 3
119-
elsif cmd_name.casecmp('xread').zero?
103+
cmd_name = command.first
104+
if cmd_name.casecmp('xread').zero?
120105
determine_optional_key_position(command, 'streams')
121106
elsif cmd_name.casecmp('xreadgroup').zero?
122107
determine_optional_key_position(command, 'streams')
108+
elsif cmd_name.casecmp('migrate').zero?
109+
command[3].empty? ? determine_optional_key_position(command, 'keys') : 3
110+
elsif cmd_name.casecmp('memory').zero?
111+
command[1].to_s.casecmp('usage').zero? ? 2 : 0
123112
else
124-
find_command_info(cmd_name)&.first_key_position.to_i
113+
i
125114
end
126115
end
127116

0 commit comments

Comments
 (0)