From abe60a7df50b6d20335c9ea2c9051126d6a52a2a Mon Sep 17 00:00:00 2001 From: Michael Spector Date: Fri, 5 Apr 2024 15:30:09 +0300 Subject: [PATCH] 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 7b7aad574..275d0e2db 100644 --- a/src/regex/bytes.rs +++ b/src/regex/bytes.rs @@ -1429,6 +1429,15 @@ impl Regex { pub fn locations(&self) -> CaptureLocations { self.capture_locations() } + + /// Returns the memory usage, in bytes, of this `Regex`. + /// + /// This does **not** include the stack size used up by this `Regex`. + /// To compute that, use `std::mem::size_of::()`. + #[inline] + pub fn memory_usage(&self) -> usize { + self.meta.memory_usage() + } } /// Represents a single match of a regex in a haystack. diff --git a/src/regex/string.rs b/src/regex/string.rs index dba94d46e..10fd3adf7 100644 --- a/src/regex/string.rs +++ b/src/regex/string.rs @@ -1432,6 +1432,15 @@ impl Regex { pub fn locations(&self) -> CaptureLocations { self.capture_locations() } + + /// Returns the memory usage, in bytes, of this `Regex`. + /// + /// This does **not** include the stack size used up by this `Regex`. + /// To compute that, use `std::mem::size_of::()`. + #[inline] + pub fn memory_usage(&self) -> usize { + self.meta.memory_usage() + } } /// Represents a single match of a regex in a haystack.