Skip to content

Commit 3bf695a

Browse files
tenderlovehsbt
authored andcommitted
[rubygems/rubygems] Pull Gem.win_platform? out of a hot path
`normalize_path` is a pretty hot path, it's called many times per file in each gem. Since the platform isn't going to change from call to call, we can conditionally define `normalize_path` based on the value of `Gem.win_platform?`. ruby/rubygems@d5e61411f2
1 parent da130d2 commit 3bf695a

File tree

3 files changed

+38
-29
lines changed

3 files changed

+38
-29
lines changed

lib/rubygems.rb

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ module Gem
1616
require_relative "rubygems/deprecate"
1717
require_relative "rubygems/errors"
1818
require_relative "rubygems/target_rbconfig"
19+
require_relative "rubygems/win_platform"
1920

2021
##
2122
# RubyGems is the Ruby standard for publishing and managing third party
@@ -113,18 +114,6 @@ module Gem
113114
module Gem
114115
RUBYGEMS_DIR = __dir__
115116

116-
##
117-
# An Array of Regexps that match windows Ruby platforms.
118-
119-
WIN_PATTERNS = [
120-
/bccwin/i,
121-
/cygwin/i,
122-
/djgpp/i,
123-
/mingw/i,
124-
/mswin/i,
125-
/wince/i,
126-
].freeze
127-
128117
GEM_DEP_FILES = %w[
129118
gem.deps.rb
130119
gems.rb
@@ -160,8 +149,6 @@ module Gem
160149

161150
DEFAULT_SOURCE_DATE_EPOCH = 315_619_200
162151

163-
@@win_platform = nil
164-
165152
@configuration = nil
166153
@gemdeps = nil
167154
@loaded_specs = {}
@@ -1091,18 +1078,6 @@ def self.use_paths(home, *paths)
10911078
self.paths = hash
10921079
end
10931080

1094-
##
1095-
# Is this a windows platform?
1096-
1097-
def self.win_platform?
1098-
if @@win_platform.nil?
1099-
ruby_platform = RbConfig::CONFIG["host_os"]
1100-
@@win_platform = !WIN_PATTERNS.find {|r| ruby_platform =~ r }.nil?
1101-
end
1102-
1103-
@@win_platform
1104-
end
1105-
11061081
##
11071082
# Is this a java platform?
11081083

lib/rubygems/package.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
# rubocop:enable Style/AsciiComments
99

10+
require_relative "win_platform"
1011
require_relative "security"
1112
require_relative "user_interaction"
1213

@@ -518,10 +519,12 @@ def install_location(filename, destination_dir) # :nodoc:
518519
destination
519520
end
520521

521-
def normalize_path(pathname)
522-
if Gem.win_platform?
522+
if Gem.win_platform?
523+
def normalize_path(pathname) # :nodoc:
523524
pathname.downcase
524-
else
525+
end
526+
else
527+
def normalize_path(pathname) # :nodoc:
525528
pathname
526529
end
527530
end

lib/rubygems/win_platform.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# frozen_string_literal: true
2+
3+
require "rbconfig"
4+
5+
module Gem
6+
##
7+
# An Array of Regexps that match windows Ruby platforms.
8+
9+
WIN_PATTERNS = [
10+
/bccwin/i,
11+
/cygwin/i,
12+
/djgpp/i,
13+
/mingw/i,
14+
/mswin/i,
15+
/wince/i,
16+
].freeze
17+
18+
@@win_platform = nil
19+
20+
##
21+
# Is this a windows platform?
22+
23+
def self.win_platform?
24+
if @@win_platform.nil?
25+
ruby_platform = RbConfig::CONFIG["host_os"]
26+
@@win_platform = !WIN_PATTERNS.find {|r| ruby_platform =~ r }.nil?
27+
end
28+
29+
@@win_platform
30+
end
31+
end

0 commit comments

Comments
 (0)