Skip to content

Commit 4ebb3c5

Browse files
No alloc Unicode version
Simple patch that moves the NameAliases version parser to build.rs. This avoids embedding the text file just to parse the file line each time unicode_version() is called. I also fixed version_owner to allocate less by not constructing a temporary String to turn into a CString.
1 parent 9a82f61 commit 4ebb3c5

4 files changed

Lines changed: 58 additions & 36 deletions

File tree

‎crates/stdlib/src/unicodedata.rs‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ impl<'a> TryFromBorrowedObject<'a> for NormalizeFormArg {
3131

3232
#[pymodule]
3333
mod unicodedata {
34+
use alloc::ffi::CString;
35+
use rustpython_unicode::{UNICODE_VERSION, UNICODE_VERSION_LEGACY};
36+
3437
use super::{NormalizeFormArg, unicode_core};
3538
use crate::vm::{
3639
Py, PyObject, PyObjectRef, PyPayload, PyResult, VirtualMachine,
@@ -312,23 +315,25 @@ mod unicodedata {
312315
inner: unicode_core::Ucd,
313316
/// Owns the bytes `unidata_version` points at.
314317
#[expect(dead_code, reason = "keeps the version string alive")]
315-
version_owner: alloc::ffi::CString,
318+
version_owner: CString,
316319
#[pymember]
317320
unidata_version: crate::vm::builtins::descriptor::CStrMember,
318321
}
319322

320323
impl Ucd {
321324
pub(super) fn new(modern: bool) -> Self {
325+
let inner = unicode_core::Ucd::new(modern);
322326
let text = if modern {
323-
unicode_core::unicode_version()
327+
UNICODE_VERSION
324328
} else {
325-
"3.2.0".to_owned()
329+
UNICODE_VERSION_LEGACY
326330
};
327-
let version_owner = alloc::ffi::CString::new(text).unwrap_or_default();
331+
let version_owner = CString::new(text)
332+
.expect("Embedded Unicode version string should never have internal NULs");
328333
let unidata_version =
329334
crate::vm::builtins::descriptor::CStrMember::new(version_owner.as_ptr());
330335
Self {
331-
inner: unicode_core::Ucd::new(modern),
336+
inner,
332337
version_owner,
333338
unidata_version,
334339
}
@@ -503,6 +508,6 @@ mod unicodedata {
503508

504509
#[pyattr]
505510
fn unidata_version(_vm: &VirtualMachine) -> String {
506-
unicode_core::unicode_version()
511+
UNICODE_VERSION.into()
507512
}
508513
}

‎crates/unicode/build.rs‎

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -536,17 +536,6 @@ fn generate_algo_names(names: Vec<(u32, Box<str>)>) {
536536
);
537537
}
538538

539-
/// Block names and ranges from the same Unicode release as the other latest tables.
540-
fn generate_blocks() {
541-
let mut writer = open_writer("blocks.rs");
542-
write_slice_pre(&mut writer, "BLOCKS", "(u32, u32, &str)");
543-
for line in UnicodeLineReader::from_file_name("Blocks.txt", true) {
544-
let name = line.field(NonZeroUsize::new(1).unwrap(), None);
545-
writeln!(writer, "({}, {}, {name:?}),", line.start, line.end).unwrap();
546-
}
547-
write_slice_post(&mut writer);
548-
}
549-
550539
/// Formal name aliases (`NameAliases.txt`) and named sequences
551540
/// (`NamedSequences.txt`). `unicodedata.lookup` consults both; `\N{}`
552541
/// accepts only a single character, so named sequences stay out of
@@ -1103,6 +1092,42 @@ fn generate_numeric_type_3_2(rx: mpsc::Receiver<NumericTypeDiffs>) {
11031092
);
11041093
}
11051094

1095+
/// Block names and ranges from the same Unicode release as the other latest tables.
1096+
fn generate_blocks() {
1097+
let mut writer = open_writer("blocks.rs");
1098+
write_slice_pre(&mut writer, "BLOCKS", "(u32, u32, &str)");
1099+
for line in UnicodeLineReader::from_file_name("Blocks.txt", true) {
1100+
let name = line.field(NonZeroUsize::new(1).unwrap(), None);
1101+
writeln!(writer, "({}, {}, {name:?}),", line.start, line.end).unwrap();
1102+
}
1103+
write_slice_post(&mut writer);
1104+
}
1105+
1106+
/// The version string of the latest Unicode database bundled with the standard
1107+
/// library (`unicodedata.unidata_version`).
1108+
pub fn unicode_version() {
1109+
// Report the vendored database version, not the compiler's Unicode version.
1110+
let version = open_reader("NameAliases.txt", true)
1111+
.lines()
1112+
.next()
1113+
.expect("should be able to read from vendored NameAliases.txt")
1114+
.expect("vendored NameAliases.txt must start with its Unicode version");
1115+
let version = version
1116+
.strip_prefix("# NameAliases-")
1117+
.expect("NameAliases.txt doesn't start with version info")
1118+
.strip_suffix(".txt")
1119+
.unwrap()
1120+
.trim();
1121+
1122+
println!("cargo:rustc-env=UNICODE_VERSION={version}");
1123+
}
1124+
1125+
/// Parsers that are standalone and read small files.
1126+
fn small_parsers() {
1127+
generate_blocks();
1128+
unicode_version();
1129+
}
1130+
11061131
/// Run each UCD parser in parallel.
11071132
fn drive_parsers() {
11081133
let parsers = [
@@ -1113,8 +1138,8 @@ fn drive_parsers() {
11131138
binary_props_3_2,
11141139
combining_class_3_2,
11151140
generate_name_lookups,
1116-
generate_blocks,
11171141
generate_case_mappings,
1142+
small_parsers,
11181143
];
11191144

11201145
let mut handles = Vec::with_capacity(parsers.len() + 2);

‎crates/unicode/src/data.rs‎

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ use icu_properties::props::{
1919
};
2020
use rustpython_wtf8::CodePoint;
2121

22+
pub const UNICODE_VERSION: &str = env!("UNICODE_VERSION");
23+
pub const UNICODE_VERSION_LEGACY: &str = "3.2.0";
24+
2225
// Latest Unicode
2326
include!(concat!(env!("OUT_DIR"), "/generated/blocks.rs"));
2427
include!(concat!(env!("OUT_DIR"), "/generated/algo_names.rs"));
@@ -237,20 +240,6 @@ pub fn membership_3_2(ch: u32) -> bool {
237240
.is_ok()
238241
}
239242

240-
/// The version string of the latest Unicode database bundled with the standard
241-
/// library (`unicodedata.unidata_version`).
242-
#[must_use]
243-
pub fn unicode_version() -> String {
244-
// Report the vendored database version, not the compiler's Unicode version.
245-
include_str!("../unicode/latest/NameAliases.txt")
246-
.lines()
247-
.next()
248-
.and_then(|header| header.strip_prefix("# NameAliases-"))
249-
.and_then(|header| header.strip_suffix(".txt"))
250-
.expect("vendored NameAliases.txt must start with its Unicode version")
251-
.into()
252-
}
253-
254243
/// Look up a name in the latest UCD (`unicodedata.lookup`).
255244
///
256245
/// Named sequences are included; `\N{...}` should use [`lookup_character`]
@@ -540,9 +529,9 @@ impl Ucd {
540529
#[must_use]
541530
pub fn unidata_version(&self) -> String {
542531
if self.modern {
543-
unicode_version()
532+
UNICODE_VERSION.into()
544533
} else {
545-
"3.2.0".into()
534+
UNICODE_VERSION_LEGACY.into()
546535
}
547536
}
548537
}
@@ -559,7 +548,7 @@ mod tests {
559548

560549
#[test]
561550
fn version_matches_vendored_database() {
562-
assert_eq!(super::unicode_version(), "17.0.0");
551+
assert_eq!(super::UNICODE_VERSION, "17.0.0");
563552
assert_eq!(Ucd::new(true).unidata_version(), "17.0.0");
564553
}
565554

‎crates/unicode/src/lib.rs‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,8 @@ pub mod grapheme;
1616
pub mod identifier;
1717
pub mod normalize;
1818

19-
pub use data::{LookupResult, Ucd, character_name, lookup_character, lookup_name, unicode_version};
19+
pub use data::{
20+
LookupResult, UNICODE_VERSION, UNICODE_VERSION_LEGACY, Ucd, character_name, lookup_character,
21+
lookup_name,
22+
};
2023
pub use normalize::{NormalizeForm, is_normalized, normalize};

0 commit comments

Comments
 (0)