From 46aa356182c2837f093d788db3a40b3baaabea2f Mon Sep 17 00:00:00 2001 From: Michael Spector Date: Fri, 5 Apr 2024 15:30:09 +0300 Subject: [PATCH 1/3] Expose Regex memory usage info (fixes #943) --- src/regex/bytes.rs | 9 +++++++++ src/regex/string.rs | 9 +++++++++ 2 files changed, 18 insertions(+) diff --git a/src/regex/bytes.rs b/src/regex/bytes.rs index 5a3a73bd3..a97bd95f6 100644 --- a/src/regex/bytes.rs +++ b/src/regex/bytes.rs @@ -1482,6 +1482,15 @@ impl Regex { pub fn locations(&self) -> CaptureLocations { self.capture_locations() } + + /// Return the total approximate heap memory, in bytes, used by this `Regex`. + /// + /// Note that this value is unrelated to [`RegexBuilder::size_limit`], and can + /// in many cases exceed it. + #[inline] + pub fn memory_usage(&self) -> usize { + self.meta.memory_usage() + self.pattern.len() + } } /// Represents a single match of a regex in a haystack. diff --git a/src/regex/string.rs b/src/regex/string.rs index 627a6c137..f961f3dda 100644 --- a/src/regex/string.rs +++ b/src/regex/string.rs @@ -1481,6 +1481,15 @@ impl Regex { pub fn locations(&self) -> CaptureLocations { self.capture_locations() } + + /// Return the total approximate heap memory, in bytes, used by this `Regex`. + /// + /// Note that this value is unrelated to [`RegexBuilder::size_limit`], and can + /// in many cases exceed it. + #[inline] + pub fn memory_usage(&self) -> usize { + self.meta.memory_usage() + self.pattern.len() + } } /// Represents a single match of a regex in a haystack. From f4ef0f51f3a25b444f32c7ddad5a8feb68ec20fa Mon Sep 17 00:00:00 2001 From: Andrew Gallant Date: Fri, 24 Jul 2026 13:38:24 -0400 Subject: [PATCH 2/3] Update src/regex/bytes.rs --- src/regex/bytes.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/regex/bytes.rs b/src/regex/bytes.rs index a97bd95f6..73f96e87c 100644 --- a/src/regex/bytes.rs +++ b/src/regex/bytes.rs @@ -1485,8 +1485,8 @@ impl Regex { /// Return the total approximate heap memory, in bytes, used by this `Regex`. /// - /// Note that this value is unrelated to [`RegexBuilder::size_limit`], and can - /// in many cases exceed it. + /// Note that this value does not have a defined relationship to + /// to [`RegexBuilder::size_limit`]. #[inline] pub fn memory_usage(&self) -> usize { self.meta.memory_usage() + self.pattern.len() From ed7eb5c5c0bdf6e51a0feed90ed4471d315bbd55 Mon Sep 17 00:00:00 2001 From: Andrew Gallant Date: Fri, 24 Jul 2026 13:38:30 -0400 Subject: [PATCH 3/3] Update src/regex/string.rs --- src/regex/string.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/regex/string.rs b/src/regex/string.rs index f961f3dda..738fa1a71 100644 --- a/src/regex/string.rs +++ b/src/regex/string.rs @@ -1484,8 +1484,8 @@ impl Regex { /// Return the total approximate heap memory, in bytes, used by this `Regex`. /// - /// Note that this value is unrelated to [`RegexBuilder::size_limit`], and can - /// in many cases exceed it. + /// Note that this value does not have a defined relationship to + /// to [`RegexBuilder::size_limit`]. #[inline] pub fn memory_usage(&self) -> usize { self.meta.memory_usage() + self.pattern.len()