Skip to content

Commit a1ce402

Browse files
authored
feat(*): string interning (#1313)
1 parent 2efe45d commit a1ce402

File tree

19 files changed

+123
-33
lines changed

19 files changed

+123
-33
lines changed

Cargo.lock

Lines changed: 31 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ reqwest = { version = "0.12.24", optional = true, default-features = false, feat
7777
"http2",
7878
"stream",
7979
] }
80+
strum = { version = "0.27.2", features = ["derive"] }
8081
reqwest-middleware = { version = "0.4.2", optional = true }
8182
reqwest-retry = { version = "0.7.0", optional = true }
8283
futures-util = { version = "0.3", optional = true }
@@ -87,7 +88,7 @@ tantivy = { version = "0.25.0", optional = true }
8788
dirs = { version = "6.0.0", optional = true }
8889
# feature=sql
8990
sea-query = { version = "0.32.7", optional = true }
90-
strum = { version = "0.27.2", features = ["derive"] }
91+
rkyv_intern = { git = "https://github.com/TheOpenDictionary/rkyv_intern.git", version = "0.1.0" }
9192

9293
[dev-dependencies]
9394
criterion = { version = "0.7.0", features = ["async_tokio", "html_reports"] }

lib/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ mod core;
2121
mod error;
2222
mod ext;
2323
mod odict;
24+
mod se;
2425

2526
pub mod format;
2627
pub mod fs;

lib/src/schema/definition.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use rkyv::with::{AsBox, MapNiche};
1+
use rkyv_intern::Intern;
22

33
use crate::serializable;
44

@@ -8,11 +8,12 @@ serializable! {
88
#[derive(Default)]
99
pub struct Definition {
1010
#[serde(rename = "@id")]
11-
#[rkyv(with = MapNiche<AsBox>)]
11+
#[rkyv(with = rkyv::with::Map<rkyv_intern::Intern>)]
1212
#[serde(skip_serializing_if = "Option::is_none")]
1313
pub id: Option<String>,
1414

1515
#[serde(rename = "@value")]
16+
#[rkyv(with = Intern)]
1617
pub value: String,
1718

1819
#[serde(default, rename="example")]

lib/src/schema/dictionary.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
use rkyv::{
2-
deserialize, to_bytes,
3-
with::{AsBox, MapNiche},
4-
};
1+
use rkyv::deserialize;
2+
53
use std::collections::HashSet;
64
use std::str::FromStr;
75

8-
use crate::{error::Error, serializable};
6+
use crate::{error::Error, se::serialize_interned, serializable};
97

108
use super::{entry::Entry, id::ID};
119

@@ -17,7 +15,7 @@ serializable! {
1715
pub id: ID,
1816

1917
#[serde(rename = "@name")]
20-
#[rkyv(with = MapNiche<AsBox>)]
18+
#[rkyv(with = rkyv::with::Map<rkyv_intern::Intern>)]
2119
#[serde(skip_serializing_if = "Option::is_none")]
2220
pub name: Option<String>,
2321

@@ -66,8 +64,7 @@ impl FromStr for Dictionary {
6664

6765
impl Dictionary {
6866
pub(crate) fn serialize(&self) -> crate::Result<Vec<u8>> {
69-
let bytes =
70-
to_bytes::<rkyv::rancor::Error>(self).map_err(|e| Error::Serialize(e.to_string()))?;
67+
let bytes = serialize_interned::<_, rkyv::rancor::Error>(self)?;
7168
Ok(bytes.to_vec())
7269
}
7370
}

lib/src/schema/entry.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
use rkyv::{
2-
deserialize, to_bytes,
2+
deserialize,
33
with::{AsBox, MapNiche},
44
};
55
use std::{
66
borrow::Borrow,
77
hash::{Hash, Hasher},
88
};
99

10-
use crate::{error::Error, schema::Etymology, serializable};
10+
use crate::{error::Error, schema::Etymology, se::serialize_interned, serializable};
1111

1212
use super::{EntryRef, MediaURL};
1313

@@ -68,9 +68,7 @@ impl Borrow<str> for ArchivedEntry {
6868

6969
impl Entry {
7070
pub fn serialize(&self) -> crate::Result<Vec<u8>> {
71-
let bytes =
72-
to_bytes::<rkyv::rancor::Error>(self).map_err(|e| Error::Serialize(e.to_string()))?;
73-
71+
let bytes = serialize_interned::<_, rkyv::rancor::Error>(self)?;
7472
Ok(bytes.to_vec())
7573
}
7674
}

lib/src/schema/entry_ref.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ use crate::serializable;
44

55
serializable! {
66
#[derive(Default)]
7-
pub struct EntryRef(pub String);
7+
pub struct EntryRef(
8+
#[rkyv(with = rkyv_intern::Intern)]
9+
pub String
10+
);
811
}
912

1013
impl EntryRef {

lib/src/schema/enums.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use std::fmt::{Debug, Display};
22

3-
use super::{FormKind, PartOfSpeech, PronunciationKind};
3+
use crate::schema::PronunciationKind;
4+
5+
use super::{FormKind, PartOfSpeech};
46

57
pub trait EnumIdentifier {
68
fn id(&self) -> String;

lib/src/schema/etymology.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
use std::collections::HashSet;
22

3-
use rkyv::with::{AsBox, MapNiche};
4-
53
use crate::schema::pronunciation::Pronunciation;
64
use crate::serializable;
75

@@ -13,7 +11,7 @@ serializable! {
1311
pub struct Etymology {
1412
#[serde(rename = "@id")]
1513
#[serde(skip_serializing_if = "Option::is_none")]
16-
#[rkyv(with = MapNiche<AsBox>)]
14+
#[rkyv(with = rkyv::with::Map<rkyv_intern::Intern>)]
1715
pub id: Option<String>,
1816

1917
#[serde(default, rename = "pronunciation")]
@@ -24,7 +22,7 @@ serializable! {
2422
pub senses: HashSet<Sense>,
2523

2624
#[serde(rename = "@description")]
27-
#[rkyv(with = MapNiche<AsBox>)]
25+
#[rkyv(with = rkyv::with::Map<rkyv_intern::Intern>)]
2826
#[serde(skip_serializing_if = "Option::is_none")]
2927
pub description: Option<String>,
3028
}

lib/src/schema/example.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ serializable! {
66
#[serde(rename = "example")]
77
pub struct Example {
88
#[serde(rename = "@value")]
9+
#[rkyv(with = rkyv_intern::Intern)]
910
pub value: String,
1011

1112
#[serde(default, rename = "translation")]

0 commit comments

Comments
 (0)