Intern story tags to save memory

This commit is contained in:
Joakim Soderlund 2020-05-23 00:31:50 +02:00
parent 5f52b590cc
commit 1feb234fa2
3 changed files with 28 additions and 7 deletions

11
Cargo.lock generated
View file

@ -124,6 +124,7 @@ version = "0.1.0"
dependencies = [
"chrono 0.4.11 (registry+https://github.com/rust-lang/crates.io-index)",
"hex 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)",
"lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"rayon 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.110 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_json 1.0.53 (registry+https://github.com/rust-lang/crates.io-index)",
@ -296,7 +297,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "proc-macro2"
version = "1.0.14"
version = "1.0.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
@ -307,7 +308,7 @@ name = "quote"
version = "1.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"proc-macro2 1.0.14 (registry+https://github.com/rust-lang/crates.io-index)",
"proc-macro2 1.0.15 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
@ -414,7 +415,7 @@ name = "serde_derive"
version = "1.0.110"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"proc-macro2 1.0.14 (registry+https://github.com/rust-lang/crates.io-index)",
"proc-macro2 1.0.15 (registry+https://github.com/rust-lang/crates.io-index)",
"quote 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)",
"syn 1.0.23 (registry+https://github.com/rust-lang/crates.io-index)",
]
@ -439,7 +440,7 @@ name = "syn"
version = "1.0.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"proc-macro2 1.0.14 (registry+https://github.com/rust-lang/crates.io-index)",
"proc-macro2 1.0.15 (registry+https://github.com/rust-lang/crates.io-index)",
"quote 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)",
"unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
@ -555,7 +556,7 @@ dependencies = [
"checksum num-traits 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "c62be47e61d1842b9170f0fdeec8eba98e60e90e5446449a0545e5152acd7096"
"checksum num_cpus 1.13.0 (registry+https://github.com/rust-lang/crates.io-index)" = "05499f3756671c15885fee9034446956fff3f243d6077b91e5767df161f766b3"
"checksum podio 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "780fb4b6698bbf9cf2444ea5d22411cef2953f0824b98f33cf454ec5615645bd"
"checksum proc-macro2 1.0.14 (registry+https://github.com/rust-lang/crates.io-index)" = "de40dd4ff82d9c9bab6dae29dbab1167e515f8df9ed17d2987cb6012db206933"
"checksum proc-macro2 1.0.15 (registry+https://github.com/rust-lang/crates.io-index)" = "70a50b9351bfa8d65a7d93ce712dc63d2fd15ddbf2c36990fc7cac344859c04f"
"checksum quote 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)" = "54a21852a652ad6f610c9510194f398ff6f8692e334fd1145fed931f7fbe44ea"
"checksum rayon 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "db6ce3297f9c85e16621bb8cca38a06779ffc31bb8184e1be4bed2be4678a098"
"checksum rayon-core 1.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "08a89b46efaf957e52b18062fb2f4660f8b8a4dde1807ca002690868ef2c85a9"

View file

@ -27,6 +27,9 @@ features = ["serde"]
[dependencies.hex]
version = "*"
[dependencies.lazy_static]
version = "*"
[dependencies.rayon]
version = "*"

View file

@ -1,10 +1,17 @@
//! Story meta.
use chrono::prelude::*;
use lazy_static::lazy_static;
use serde::de::Error;
use serde::{Deserialize, Deserializer};
use serde_json::Value;
use super::interner::Interner;
lazy_static! {
static ref TAGS: Interner<Tag> = Interner::new();
}
#[derive(Clone, Debug, Deserialize)]
pub struct Story {
pub archive: Archive,
@ -33,7 +40,8 @@ pub struct Story {
pub short_description: String,
pub status: Status,
pub submitted: bool,
pub tags: Vec<Tag>,
#[serde(deserialize_with = "tags_as_static")]
pub tags: Vec<&'static Tag>,
#[serde(deserialize_with = "null_to_text")]
pub title: String,
pub total_num_views: i32,
@ -143,7 +151,7 @@ pub enum Status {
Visible,
}
#[derive(Clone, Debug, Deserialize)]
#[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq)]
pub struct Tag {
pub id: i64,
pub name: String,
@ -189,6 +197,15 @@ where
}
}
fn tags_as_static<'de, D>(d: D) -> Result<Vec<&'static Tag>, D::Error>
where
D: Deserializer<'de>,
{
let tags: Vec<Tag> = Vec::deserialize(d)?;
Ok(tags.into_iter().map(|tag| TAGS.intern(tag)).collect())
}
impl<'de> Deserialize<'de> for Color {
fn deserialize<D>(d: D) -> Result<Color, D::Error>
where