-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: reduce memory allocations (#126)
- Loading branch information
1 parent
dab3fd6
commit 461d027
Showing
10 changed files
with
249 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'singleton' | ||
|
||
class RedisClient | ||
class Cluster | ||
class NormalizedCmdName | ||
include Singleton | ||
|
||
EMPTY_STRING = '' | ||
|
||
def initialize | ||
@cache = {} | ||
@mutex = Mutex.new | ||
end | ||
|
||
def get_by_command(command) | ||
get(command, index: 0) | ||
end | ||
|
||
def get_by_subcommand(command) | ||
get(command, index: 1) | ||
end | ||
|
||
def get_by_name(name) | ||
get(name, index: 0) | ||
end | ||
|
||
def clear | ||
@mutex.synchronize { @cache.clear } | ||
end | ||
|
||
private | ||
|
||
def get(command, index:) | ||
name = extract_name(command, index: index) | ||
return EMPTY_STRING if name.nil? || name.empty? | ||
|
||
normalize(name) | ||
end | ||
|
||
def extract_name(command, index:) | ||
case command | ||
when String, Symbol then index.zero? ? command : nil | ||
when Array then extract_name_from_array(command, index: index) | ||
end | ||
end | ||
|
||
def extract_name_from_array(command, index:) | ||
return if command.size - 1 < index | ||
|
||
case e = command[index] | ||
when String, Symbol then e | ||
when Array then e[index] | ||
end | ||
end | ||
|
||
def normalize(name) | ||
return @cache[name] if @cache.key?(name) | ||
return name.to_s.downcase if @mutex.locked? | ||
|
||
@mutex.synchronize { @cache[name] = name.to_s.downcase } | ||
@cache[name] | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.