-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf(rust, python): optimize string kernels, (elide redundant allocs) (…
- Loading branch information
Showing
4 changed files
with
126 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
use polars_core::prelude::Utf8Chunked; | ||
|
||
pub(super) fn to_lowercase<'a>(ca: &'a Utf8Chunked) -> Utf8Chunked { | ||
// amortize allocation | ||
let mut buf = String::new(); | ||
let f = |s: &'a str| { | ||
buf.clear(); | ||
buf.push_str(s); | ||
buf.make_ascii_lowercase(); | ||
// extend lifetime | ||
// lifetime is bound to 'a | ||
let slice = buf.as_str(); | ||
unsafe { std::mem::transmute::<&str, &'a str>(slice) } | ||
}; | ||
ca.apply_mut(f) | ||
} | ||
|
||
pub(super) fn to_uppercase<'a>(ca: &'a Utf8Chunked) -> Utf8Chunked { | ||
// amortize allocation | ||
let mut buf = String::new(); | ||
let f = |s: &'a str| { | ||
buf.clear(); | ||
buf.push_str(s); | ||
buf.make_ascii_uppercase(); | ||
// extend lifetime | ||
// lifetime is bound to 'a | ||
let slice = buf.as_str(); | ||
unsafe { std::mem::transmute::<&str, &'a str>(slice) } | ||
}; | ||
ca.apply_mut(f) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
use std::fmt::Write; | ||
|
||
use polars_core::prelude::Utf8Chunked; | ||
|
||
pub(super) fn ljust<'a>(ca: &'a Utf8Chunked, width: usize, fillchar: char) -> Utf8Chunked { | ||
// amortize allocation | ||
let mut buf = String::new(); | ||
let f = |s: &'a str| { | ||
let padding = width.saturating_sub(s.len()); | ||
if padding == 0 { | ||
s | ||
} else { | ||
buf.clear(); | ||
buf.push_str(s); | ||
for _ in 0..padding { | ||
buf.push(fillchar) | ||
} | ||
// extend lifetime | ||
// lifetime is bound to 'a | ||
let slice = buf.as_str(); | ||
unsafe { std::mem::transmute::<&str, &'a str>(slice) } | ||
} | ||
}; | ||
ca.apply_mut(f) | ||
} | ||
|
||
pub(super) fn rjust<'a>(ca: &'a Utf8Chunked, width: usize, fillchar: char) -> Utf8Chunked { | ||
// amortize allocation | ||
let mut buf = String::new(); | ||
let f = |s: &'a str| { | ||
let padding = width.saturating_sub(s.len()); | ||
if padding == 0 { | ||
s | ||
} else { | ||
buf.clear(); | ||
for _ in 0..padding { | ||
buf.push(fillchar) | ||
} | ||
buf.push_str(s); | ||
// extend lifetime | ||
// lifetime is bound to 'a | ||
let slice = buf.as_str(); | ||
unsafe { std::mem::transmute::<&str, &'a str>(slice) } | ||
} | ||
}; | ||
ca.apply_mut(f) | ||
} | ||
|
||
pub(super) fn zfill<'a>(ca: &'a Utf8Chunked, alignment: usize) -> Utf8Chunked { | ||
// amortize allocation | ||
let mut buf = String::new(); | ||
let f = |s: &'a str| { | ||
let alignment = alignment.saturating_sub(s.len()); | ||
if alignment == 0 { | ||
return s; | ||
} | ||
buf.clear(); | ||
if let Some(stripped) = s.strip_prefix('-') { | ||
write!( | ||
&mut buf, | ||
"-{:0alignment$}{value}", | ||
0, | ||
alignment = alignment, | ||
value = stripped | ||
) | ||
.unwrap(); | ||
} else { | ||
write!( | ||
&mut buf, | ||
"{:0alignment$}{value}", | ||
0, | ||
alignment = alignment, | ||
value = s | ||
) | ||
.unwrap(); | ||
}; | ||
// extend lifetime | ||
// lifetime is bound to 'a | ||
let slice = buf.as_str(); | ||
unsafe { std::mem::transmute::<&str, &'a str>(slice) } | ||
}; | ||
ca.apply_mut(f) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters