The world's most clever kitty cat
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Better brotli options

Ben C 1fe3cd9e 71d0a017

+28 -12
+5 -4
src/cmd/dump_chain.rs
··· 1 1 use std::sync::Arc; 2 2 3 - use brotli::enc::BrotliEncoderParams; 4 3 use twilight_interactions::command::{CommandModel, CreateCommand}; 5 4 use twilight_model::{ 6 5 application::interaction::{Interaction, application_command::CommandData}, 7 6 http::attachment::Attachment, 8 7 }; 9 8 10 - use crate::{BotContext, cmd::DEFER_INTER_RESP_EPHEMERAL, prelude::*}; 9 + use crate::{ 10 + BROTLI_BUF_SIZE, BotContext, cmd::DEFER_INTER_RESP_EPHEMERAL, get_brotli_params, prelude::*, 11 + }; 11 12 12 13 #[derive(CommandModel, CreateCommand)] 13 14 #[command(name = "dump_chain", desc = "Dump chain")] ··· 29 30 .context("Failed to defer")?; 30 31 31 32 let mut buf = Vec::<u8>::with_capacity(4096); 32 - let params = BrotliEncoderParams::default(); 33 - let mut brotli_writer = brotli::CompressorWriter::with_params(&mut buf, 4096, &params); 33 + let mut brotli_writer = 34 + brotli::CompressorWriter::with_params(&mut buf, BROTLI_BUF_SIZE, &get_brotli_params()); 34 35 35 36 if compat.unwrap_or_default() { 36 37 let brain = ctx.brain_handle.read().await;
+3 -2
src/cmd/load_chain.rs
··· 7 7 }; 8 8 9 9 use crate::{ 10 - BotContext, brain::Brain, cmd::DEFER_INTER_RESP_EPHEMERAL, prelude::*, status::update_status, 10 + BROTLI_BUF_SIZE, BotContext, brain::Brain, cmd::DEFER_INTER_RESP_EPHEMERAL, prelude::*, 11 + status::update_status, 11 12 }; 12 13 13 14 #[derive(CommandModel, CreateCommand)] ··· 39 40 .await 40 41 .context("Failed to decode as bytes")?, 41 42 ); 42 - let mut brotli_stream = brotli::Decompressor::new(&mut data, 4096); 43 + let mut brotli_stream = brotli::Decompressor::new(&mut data, BROTLI_BUF_SIZE); 43 44 44 45 let new_brain: Brain = if compat.unwrap_or_default() { 45 46 Brain::from_legacy_hashmap(
+20 -6
src/main.rs
··· 22 22 }, 23 23 }; 24 24 25 - use brotli::enc::BrotliEncoderParams; 25 + use brotli::enc::{BrotliEncoderParams, backward_references::BrotliEncoderMode}; 26 26 use log::{debug, error, info, warn}; 27 27 use prelude::*; 28 28 use tokio::{ ··· 64 64 65 65 async fn handle_discord_event(event: Event, ctx: Arc<BotContext>) -> Result { 66 66 match event { 67 - Event::MessageCreate(msg) => handle_discord_message(msg, ctx).await.context("While handling a new message"), 67 + Event::MessageCreate(msg) => handle_discord_message(msg, ctx) 68 + .await 69 + .context("While handling a new message"), 68 70 Event::InteractionCreate(mut inter) => { 69 71 if let Some(InteractionData::ApplicationCommand(data)) = 70 72 std::mem::take(&mut inter.0.data) 71 73 { 72 - handle_app_command(*data, ctx, inter.0).await.context("While handling an app command") 74 + handle_app_command(*data, ctx, inter.0) 75 + .await 76 + .context("While handling an app command") 73 77 } else { 74 78 Ok(()) 75 79 } ··· 83 87 } 84 88 } 85 89 90 + const BROTLI_BUF_SIZE: usize = 1024 * 1000; 91 + fn get_brotli_params() -> BrotliEncoderParams { 92 + BrotliEncoderParams { 93 + quality: 5, 94 + mode: BrotliEncoderMode::BROTLI_MODE_TEXT, 95 + ..Default::default() 96 + } 97 + } 98 + 86 99 fn load_brain(path: &Path) -> Result<Option<Brain>> { 87 100 if path.exists() { 88 101 let mut file = File::open(path).context("Failed to open brain file")?; 89 - let mut brotli_stream = brotli::Decompressor::new(&mut file, 4096); 102 + let mut brotli_stream = brotli::Decompressor::new(&mut file, BROTLI_BUF_SIZE); 90 103 rmp_serde::from_read(&mut brotli_stream) 91 104 .map(|b| Some(b)) 92 105 .context("Failed to decode brain file") ··· 96 109 } 97 110 98 111 async fn save_brain(ctx: Arc<BotContext>) -> Result { 112 + // TODO: Atomic saves 99 113 let mut file = File::create(&ctx.brain_file_path).context("Failed to open brain file")?; 100 - let params = BrotliEncoderParams::default(); 101 - let mut brotli_writer = brotli::CompressorWriter::with_params(&mut file, 4096, &params); 114 + let mut brotli_writer = 115 + brotli::CompressorWriter::with_params(&mut file, BROTLI_BUF_SIZE, &get_brotli_params()); 102 116 let brain = ctx.brain_handle.read().await; 103 117 rmp_serde::encode::write(&mut brotli_writer, &*brain) 104 118 .context("Failed to write serialized brain")?;