|
1 | | -use magnus::{RArray, RHash, Ruby, Symbol, Value, prelude::*, r_hash::ForEach}; |
| 1 | +use magnus::{RArray, RHash, RString, Ruby, Symbol, Value, prelude::*, r_hash::ForEach}; |
2 | 2 | use polars::frame::row::{Row, rows_to_schema_supertypes, rows_to_supertypes}; |
3 | 3 | use polars::prelude::*; |
4 | 4 |
|
@@ -128,13 +128,19 @@ fn dicts_to_rows<'a>(data: &Value, names: &'a [String], _strict: bool) -> RbResu |
128 | 128 | let ruby = Ruby::get_with(*data); |
129 | 129 | let (data, len) = get_rbseq(*data)?; |
130 | 130 | let mut rows = Vec::with_capacity(len); |
| 131 | + |
| 132 | + // pre-convert keys/names so we don't repeatedly create them in the loop |
| 133 | + let rb_keys: Vec<(RString, Symbol)> = names |
| 134 | + .iter() |
| 135 | + .map(|k| (ruby.str_new(k), ruby.to_symbol(k))) |
| 136 | + .collect(); |
| 137 | + |
131 | 138 | for d in data.into_iter() { |
132 | 139 | let d = RHash::try_convert(d)?; |
133 | 140 |
|
134 | 141 | let mut row = Vec::with_capacity(names.len()); |
135 | | - for k in names.iter() { |
136 | | - // TODO improve performance |
137 | | - let val = match d.get(k.clone()).or_else(|| d.get(ruby.to_symbol(k))) { |
| 142 | + for (k, k2) in &rb_keys { |
| 143 | + let val = match d.get(*k).or_else(|| d.get(*k2)) { |
138 | 144 | None => AnyValue::Null, |
139 | 145 | Some(val) => Wrap::<AnyValue>::try_convert(val)?.0, |
140 | 146 | }; |
|
0 commit comments