Skip to content

Commit efdc78c

Browse files
waynexiaadriangb
authored andcommitted
optimize initcap function by avoiding memory allocation (apache#16878)
Signed-off-by: Ruihang Xia <waynestxia@gmail.com>
1 parent 5dd8387 commit efdc78c

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

datafusion/functions/src/unicode/initcap.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,11 @@ fn initcap<T: OffsetSizeTrait>(args: &[ArrayRef]) -> Result<ArrayRef> {
131131
string_array.value_data().len(),
132132
);
133133

134+
let mut container = String::new();
134135
string_array.iter().for_each(|str| match str {
135136
Some(s) => {
136-
let initcap_str = initcap_string(s);
137-
builder.append_value(initcap_str);
137+
initcap_string(s, &mut container);
138+
builder.append_value(&container);
138139
}
139140
None => builder.append_null(),
140141
});
@@ -147,42 +148,41 @@ fn initcap_utf8view(args: &[ArrayRef]) -> Result<ArrayRef> {
147148

148149
let mut builder = StringViewBuilder::with_capacity(string_view_array.len());
149150

151+
let mut container = String::new();
150152
string_view_array.iter().for_each(|str| match str {
151153
Some(s) => {
152-
let initcap_str = initcap_string(s);
153-
builder.append_value(initcap_str);
154+
initcap_string(s, &mut container);
155+
builder.append_value(&container);
154156
}
155157
None => builder.append_null(),
156158
});
157159

158160
Ok(Arc::new(builder.finish()) as ArrayRef)
159161
}
160162

161-
fn initcap_string(input: &str) -> String {
162-
let mut result = String::with_capacity(input.len());
163+
fn initcap_string(input: &str, container: &mut String) {
164+
container.clear();
163165
let mut prev_is_alphanumeric = false;
164166

165167
if input.is_ascii() {
166168
for c in input.chars() {
167169
if prev_is_alphanumeric {
168-
result.push(c.to_ascii_lowercase());
170+
container.push(c.to_ascii_lowercase());
169171
} else {
170-
result.push(c.to_ascii_uppercase());
172+
container.push(c.to_ascii_uppercase());
171173
};
172174
prev_is_alphanumeric = c.is_ascii_alphanumeric();
173175
}
174176
} else {
175177
for c in input.chars() {
176178
if prev_is_alphanumeric {
177-
result.extend(c.to_lowercase());
179+
container.extend(c.to_lowercase());
178180
} else {
179-
result.extend(c.to_uppercase());
181+
container.extend(c.to_uppercase());
180182
}
181183
prev_is_alphanumeric = c.is_alphanumeric();
182184
}
183185
}
184-
185-
result
186186
}
187187

188188
#[cfg(test)]

0 commit comments

Comments
 (0)