Skip to content

Commit 8040f51

Browse files
committed
unicodedata: allocate the per-call result strings through the managed path
`category`, `bidirectional`, `east_asian_width`, `decomposition`, `name` and `lookup` build a fresh string on every call and returned it through `w_str_new`, whose value buffer comes from `malloc_raw` -- a buffer the collector can never reclaim. Scanning a text one character at a time accumulated one such buffer per call. `w_str_new_managed` allocates a GC storage box when the interpreter collector and the value tid are both live, and falls back to immortal otherwise. The remaining `w_str_new` calls in the module stay: `unidata_version` and its siblings are module constants built once per process, and the others are in tests. Assisted-by: Claude
1 parent 9a13788 commit 8040f51

1 file changed

Lines changed: 6 additions & 6 deletions

File tree

  • pyre/pyre-interpreter/src/module/unicodedata

‎pyre/pyre-interpreter/src/module/unicodedata/mod.rs‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,14 +107,14 @@ fn char_and_default(
107107
// `LEGACY` for the `ucd_3_2_0` instance.
108108

109109
fn category_impl(db: &ucd_core::Ucd, args: &[PyObjectRef]) -> PyResult {
110-
Ok(w_str_new(db.category(one_char("category", args)?)))
110+
Ok(w_str_new_managed(db.category(one_char("category", args)?)))
111111
}
112112
fn category(args: &[PyObjectRef]) -> PyResult {
113113
category_impl(&MODERN, args)
114114
}
115115

116116
fn bidirectional_impl(db: &ucd_core::Ucd, args: &[PyObjectRef]) -> PyResult {
117-
Ok(w_str_new(
117+
Ok(w_str_new_managed(
118118
db.bidirectional(one_char("bidirectional", args)?),
119119
))
120120
}
@@ -123,7 +123,7 @@ fn bidirectional(args: &[PyObjectRef]) -> PyResult {
123123
}
124124

125125
fn east_asian_width_impl(db: &ucd_core::Ucd, args: &[PyObjectRef]) -> PyResult {
126-
Ok(w_str_new(
126+
Ok(w_str_new_managed(
127127
db.east_asian_width(one_char("east_asian_width", args)?),
128128
))
129129
}
@@ -146,7 +146,7 @@ fn mirrored(args: &[PyObjectRef]) -> PyResult {
146146
}
147147

148148
fn decomposition_impl(db: &ucd_core::Ucd, args: &[PyObjectRef]) -> PyResult {
149-
Ok(w_str_new(
149+
Ok(w_str_new_managed(
150150
&db.decomposition(one_char("decomposition", args)?),
151151
))
152152
}
@@ -198,7 +198,7 @@ fn name_impl(db: &ucd_core::Ucd, args: &[PyObjectRef]) -> PyResult {
198198
if db.category(cp) != UNASSIGNED_CATEGORY
199199
&& let Some(name) = cp.to_char().and_then(ucd_core::character_name)
200200
{
201-
return Ok(w_str_new(&name));
201+
return Ok(w_str_new_managed(&name));
202202
}
203203
default.ok_or_else(|| PyError::value_error("no such name"))
204204
}
@@ -230,7 +230,7 @@ fn lookup(args: &[PyObjectRef]) -> PyResult {
230230
{
231231
let mut buf = String::with_capacity(ch.len_utf8());
232232
buf.push(ch);
233-
return Ok(w_str_new(&buf));
233+
return Ok(w_str_new_managed(&buf));
234234
}
235235
let mut msg = Wtf8Buf::from_string("undefined character name '".to_string());
236236
msg.push_wtf8(name);

0 commit comments

Comments
 (0)