Skip to content

Commit

Permalink
BinaryPlatforms: prevent creating an identical regex every time a `Pl…
Browse files Browse the repository at this point in the history
…atform` is parsed (#52829)

Pkg likes to call `HostPlatform()` a bit too often for its own good
which shows up in some profiles. This makes `HostPlatform()` go from 250
us to 150 us with a quite trivial code change so should be fairly
uncontroversial. (Pkg should probably also be fixed, alt. `HostPlatform`
should be cached in Base.)
  • Loading branch information
KristofferC committed Jan 10, 2024
1 parent 2afc20c commit 0f7b598
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions base/binaryplatforms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -661,18 +661,12 @@ const libstdcxx_version_mapping = Dict{String,String}(
"libstdcxx" => "-libstdcxx\\d+",
)

"""
parse(::Type{Platform}, triplet::AbstractString)
Parses a string platform triplet back into a `Platform` object.
"""
function Base.parse(::Type{Platform}, triplet::String; validate_strict::Bool = false)
const triplet_regex = let
# Helper function to collapse dictionary of mappings down into a regex of
# named capture groups joined by "|" operators
c(mapping) = string("(",join(["(?<$k>$v)" for (k, v) in mapping], "|"), ")")

# We're going to build a mondo regex here to parse everything:
triplet_regex = Regex(string(
Regex(string(
"^",
# First, the core triplet; arch/os/libc/call_abi
c(arch_mapping),
Expand All @@ -687,7 +681,14 @@ function Base.parse(::Type{Platform}, triplet::String; validate_strict::Bool = f
"(?<tags>(?:-[^-]+\\+[^-]+)*)?",
"\$",
))
end

"""
parse(::Type{Platform}, triplet::AbstractString)
Parses a string platform triplet back into a `Platform` object.
"""
function Base.parse(::Type{Platform}, triplet::String; validate_strict::Bool = false)
m = match(triplet_regex, triplet)
if m !== nothing
# Helper function to find the single named field within the giant regex
Expand Down

0 comments on commit 0f7b598

Please sign in to comment.