From 0f7b598ad2a690cae4fe67b3f834e476a77399f4 Mon Sep 17 00:00:00 2001 From: Kristoffer Carlsson Date: Wed, 10 Jan 2024 21:11:41 +0100 Subject: [PATCH] BinaryPlatforms: prevent creating an identical regex every time a `Platform` 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.) --- base/binaryplatforms.jl | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/base/binaryplatforms.jl b/base/binaryplatforms.jl index b374d57ce9731..e0ee107c4ec73 100644 --- a/base/binaryplatforms.jl +++ b/base/binaryplatforms.jl @@ -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), @@ -687,7 +681,14 @@ function Base.parse(::Type{Platform}, triplet::String; validate_strict::Bool = f "(?(?:-[^-]+\\+[^-]+)*)?", "\$", )) +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