Skip to content

Commit 419664d

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 dba7a05 commit 419664d

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
@@ -457,17 +457,6 @@ fn generate_algo_names(names: Vec<(u32, Box<str>)>) {
457457
);
458458
}
459459

460-
/// Block names and ranges from the same Unicode release as the other latest tables.
461-
fn generate_blocks() {
462-
let mut writer = open_writer("blocks.rs");
463-
write_slice_pre(&mut writer, "BLOCKS", "(u32, u32, &str)");
464-
for line in UnicodeLineReader::from_file_name("Blocks.txt", true) {
465-
let name = line.field(NonZeroUsize::new(1).unwrap(), None);
466-
writeln!(writer, "({}, {}, {name:?}),", line.start, line.end).unwrap();
467-
}
468-
write_slice_post(&mut writer);
469-
}
470-
471460
/// Formal name aliases (`NameAliases.txt`) and named sequences
472461
/// (`NamedSequences.txt`). `unicodedata.lookup` consults both; `\N{}`
473462
/// accepts only a single character, so named sequences stay out of
@@ -1024,6 +1013,42 @@ fn generate_numeric_type_3_2(rx: mpsc::Receiver<NumericTypeDiffs>) {
10241013
);
10251014
}
10261015

1016+
/// Block names and ranges from the same Unicode release as the other latest tables.
1017+
fn generate_blocks() {
1018+
let mut writer = open_writer("blocks.rs");
1019+
write_slice_pre(&mut writer, "BLOCKS", "(u32, u32, &str)");
1020+
for line in UnicodeLineReader::from_file_name("Blocks.txt", true) {
1021+
let name = line.field(NonZeroUsize::new(1).unwrap(), None);
1022+
writeln!(writer, "({}, {}, {name:?}),", line.start, line.end).unwrap();
1023+
}
1024+
write_slice_post(&mut writer);
1025+
}
1026+
1027+
/// The version string of the latest Unicode database bundled with the standard
1028+
/// library (`unicodedata.unidata_version`).
1029+
pub fn unicode_version() {
1030+
// Report the vendored database version, not the compiler's Unicode version.
1031+
let version = open_reader("NameAliases.txt", true)
1032+
.lines()
1033+
.next()
1034+
.expect("should be able to read from vendored NameAliases.txt")
1035+
.expect("vendored NameAliases.txt must start with its Unicode version");
1036+
let version = version
1037+
.strip_prefix("# NameAliases-")
1038+
.expect("NameAliases.txt doesn't start with version info")
1039+
.strip_suffix(".txt")
1040+
.unwrap()
1041+
.trim();
1042+
1043+
println!("cargo:rustc-env=UNICODE_VERSION={version}");
1044+
}
1045+
1046+
/// Parsers that are standalone and read small files.
1047+
fn small_parsers() {
1048+
generate_blocks();
1049+
unicode_version();
1050+
}
1051+
10271052
/// Run each UCD parser in parallel.
10281053
fn drive_parsers() {
10291054
let parsers = [
@@ -1034,7 +1059,7 @@ fn drive_parsers() {
10341059
binary_props_3_2,
10351060
combining_class_3_2,
10361061
generate_name_lookups,
1037-
generate_blocks,
1062+
small_parsers,
10381063
];
10391064

10401065
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`]
@@ -546,9 +535,9 @@ impl Ucd {
546535
#[must_use]
547536
pub fn unidata_version(&self) -> String {
548537
if self.modern {
549-
unicode_version()
538+
UNICODE_VERSION.into()
550539
} else {
551-
"3.2.0".into()
540+
UNICODE_VERSION_LEGACY.into()
552541
}
553542
}
554543
}
@@ -565,7 +554,7 @@ mod tests {
565554

566555
#[test]
567556
fn version_matches_vendored_database() {
568-
assert_eq!(super::unicode_version(), "17.0.0");
557+
assert_eq!(super::UNICODE_VERSION, "17.0.0");
569558
assert_eq!(Ucd::new(true).unidata_version(), "17.0.0");
570559
}
571560

‎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)