Skip to content

Commit 42ead60

Browse files
committed
Move uri parser to constant
Creating a new one each time is horrible for performance: ```rb require "bundler/inline" gemfile do gem "benchmark-ips" gem "globalid", path: "." end Benchmark.ips do |x| x.report("current") { URI::GID.parse 'gid://bcx/Person/1?key=value' } x.report("pr") { URI::GID.parse_from_pr 'gid://bcx/Person/1?key=value' } x.compare! end $ ruby test.rb ruby 3.4.5 (2025-07-16 revision 20cda200d3) +PRISM [x86_64-linux] Warming up -------------------------------------- current 161.000 i/100ms pr 8.860k i/100ms Calculating ------------------------------------- current 1.621k (± 2.2%) i/s (617.01 μs/i) - 8.211k in 5.068729s pr 93.755k (± 2.2%) i/s (10.67 μs/i) - 469.580k in 5.011127s Comparison: pr: 93755.2 i/s current: 1620.7 i/s - 57.85x slower ```
1 parent 6c719da commit 42ead60

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

lib/global_id/uri/gid.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ class InvalidModelIdError < URI::InvalidComponentError; end
3636
COMPOSITE_MODEL_ID_MAX_SIZE = 20
3737
COMPOSITE_MODEL_ID_DELIMITER = "/"
3838

39+
URI_PARSER = URI::RFC2396_Parser.new # :nodoc:
40+
3941
class << self
4042
# Validates +app+'s as URI hostnames containing only alphanumeric characters
4143
# and hyphens. An ArgumentError is raised if +app+ is invalid.
@@ -62,7 +64,7 @@ def validate_app(app)
6264
# URI.parse('gid://bcx') # => URI::GID instance
6365
# URI::GID.parse('gid://bcx/') # => raises URI::InvalidComponentError
6466
def parse(uri)
65-
generic_components = URI.split(uri) << URI::RFC2396_Parser.new << true # RFC2396 parser, true arg_check
67+
generic_components = URI.split(uri) << URI_PARSER << true # RFC2396 parser, true arg_check
6668
new(*generic_components)
6769
end
6870

0 commit comments

Comments
 (0)