|
1 |
| -use crate::{JsonMessage, JsonPosition}; |
| 1 | +use crate::JsonMessage; |
2 | 2 | use napi::bindgen_prelude::Array;
|
3 |
| -use napi::{Env, Property}; |
| 3 | +use napi::Env; |
4 | 4 | use napi_derive::napi;
|
5 | 5 |
|
6 | 6 | // Use the mimalloc allocator explicitly when building the node addon.
|
7 | 7 | extern crate intl_allocator;
|
8 | 8 |
|
9 |
| -#[napi(object)] |
10 |
| -pub struct Position { |
| 9 | +#[napi] |
| 10 | +pub struct Message { |
| 11 | + pub key: String, |
| 12 | + pub value: String, |
| 13 | + // The Position properties are created directly inline on the message |
| 14 | + // because NAPI object construction is pretty slow (like 10x slower than |
| 15 | + // the engine creating an object directly), so moving these to direct |
| 16 | + // properties of the class makes the whole process ~30-35% faster. |
| 17 | + // |
| 18 | + // See https://github.com/nodejs/node/issues/45905 for future updates. |
11 | 19 | pub line: u32,
|
12 | 20 | pub col: u32,
|
13 | 21 | }
|
14 | 22 |
|
15 |
| -impl From<JsonPosition> for Position { |
16 |
| - fn from(pos: JsonPosition) -> Self { |
17 |
| - Self { |
18 |
| - line: pos.line, |
19 |
| - col: pos.col, |
20 |
| - } |
21 |
| - } |
22 |
| -} |
23 |
| - |
24 | 23 | fn collect_messages(env: Env, iterator: impl Iterator<Item = JsonMessage>) -> napi::Result<Array> {
|
25 | 24 | // This is an arbitrary size hint that should be suitable for a lot of use
|
26 | 25 | // cases. While it may inadvertently allocate extra memory for some,
|
27 | 26 | // avoiding repeated re-allocations that we're pretty confident will happen
|
28 | 27 | // ends up saving a lot more time in the end.
|
29 | 28 | let mut result = env.create_array(1024)?;
|
30 |
| - // NAPI does not have an API for creating multiple instances of the same |
31 |
| - // object structure, but we can get as close as possible by pre-defining |
32 |
| - // object properties and cloning them to avoid allocating extra space for |
33 |
| - // the same key many times over. |
34 |
| - // See https://github.com/nodejs/node/issues/45905 for future updates. |
35 |
| - let line_prop = Property::new("line")?; |
36 |
| - let col_prop = Property::new("col")?; |
37 |
| - let key_prop = Property::new("key")?; |
38 |
| - let value_prop = Property::new("value")?; |
39 |
| - let position_prop = Property::new("position")?; |
40 | 29 | for message in iterator {
|
41 |
| - let key = env.create_string(&message.key)?; |
42 |
| - let value = env.create_string(&message.value)?; |
43 |
| - let mut position = env.create_object()?; |
44 |
| - position.define_properties(&[ |
45 |
| - line_prop |
46 |
| - .clone() |
47 |
| - .with_value(&env.create_uint32(message.position.line)?), |
48 |
| - col_prop |
49 |
| - .clone() |
50 |
| - .with_value(&env.create_uint32(message.position.col)?), |
51 |
| - ])?; |
52 |
| - |
53 |
| - let mut converted = env.create_object()?; |
54 |
| - converted.define_properties(&[ |
55 |
| - key_prop.clone().with_value(&key), |
56 |
| - value_prop.clone().with_value(&value), |
57 |
| - position_prop.clone().with_value(&position), |
58 |
| - ])?; |
59 |
| - |
60 |
| - result.insert(converted)?; |
| 30 | + result.insert(Message { |
| 31 | + key: message.key.to_string(), |
| 32 | + value: message.value.to_string(), |
| 33 | + line: message.position.line, |
| 34 | + col: message.position.col, |
| 35 | + })?; |
61 | 36 | }
|
62 | 37 |
|
63 | 38 | Ok(result)
|
64 | 39 | }
|
65 | 40 |
|
66 |
| -#[napi(object)] |
67 |
| -pub struct Message { |
68 |
| - pub key: String, |
69 |
| - pub value: String, |
70 |
| - pub position: Position, |
71 |
| -} |
72 |
| - |
73 | 41 | #[napi(ts_return_type = "Message[]")]
|
74 | 42 | pub fn parse_json(env: Env, text: String) -> napi::Result<Array> {
|
75 | 43 | let messages = crate::parse_flat_translation_json(&text);
|
|
0 commit comments