Support simd_json (#105)
This PR adds support for the simd-json library whenever decoding or encoding JSON responses. This may be enabled independently of serenity and twilight support for SIMD acceleration. Co-authored-by: Kyle Simpson <kyleandrew.simpson@gmail.com>
This commit is contained in:
committed by
Kyle Simpson
parent
8cc7a22b0b
commit
cb0a74f511
@@ -199,7 +199,7 @@ impl Compressed {
|
||||
)?;
|
||||
let mut metabytes = b"DCA1\0\0\0\0".to_vec();
|
||||
let orig_len = metabytes.len();
|
||||
serde_json::to_writer(&mut metabytes, &metadata)?;
|
||||
crate::json::to_writer(&mut metabytes, &metadata)?;
|
||||
let meta_len = (metabytes.len() - orig_len)
|
||||
.try_into()
|
||||
.map_err(|_| CodecCacheError::MetadataTooLarge)?;
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
use crate::input::AudioStreamError;
|
||||
use crate::{error::JsonError, input::AudioStreamError};
|
||||
use audiopus::error::Error as OpusError;
|
||||
use serde_json::Error as JsonError;
|
||||
use std::{
|
||||
error::Error as StdError,
|
||||
fmt::{Display, Formatter, Result as FmtResult},
|
||||
|
||||
@@ -110,9 +110,11 @@ impl FormatReader for DcaReader {
|
||||
return symph_err::decode_error("missing DCA1 metadata block");
|
||||
}
|
||||
|
||||
let raw_json = source.read_boxed_slice_exact(size as usize)?;
|
||||
let mut raw_json = source.read_boxed_slice_exact(size as usize)?;
|
||||
|
||||
let metadata: DcaMetadata = serde_json::from_slice::<DcaMetadata>(&raw_json)
|
||||
// NOTE: must be mut for simd-json.
|
||||
#[allow(clippy::unnecessary_mut_passed)]
|
||||
let metadata: DcaMetadata = crate::json::from_slice::<DcaMetadata>(&mut raw_json)
|
||||
.map_err(|_| SymphError::DecodeError("malformed DCA1 metadata block"))?;
|
||||
|
||||
let mut revision = MetadataBuilder::new();
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
use crate::error::JsonError;
|
||||
use std::time::Duration;
|
||||
use symphonia_core::{meta::Metadata as ContainerMetadata, probe::ProbedMetadata};
|
||||
|
||||
@@ -47,8 +48,8 @@ pub struct AuxMetadata {
|
||||
|
||||
impl AuxMetadata {
|
||||
/// Extract metadata and details from the output of `ffprobe -of json`.
|
||||
pub fn from_ffprobe_json(value: &[u8]) -> Result<Self, serde_json::Error> {
|
||||
let output: ffprobe::Output = serde_json::from_slice(value)?;
|
||||
pub fn from_ffprobe_json(value: &mut [u8]) -> Result<Self, JsonError> {
|
||||
let output: ffprobe::Output = crate::json::from_slice(value)?;
|
||||
|
||||
Ok(output.into_aux_metadata())
|
||||
}
|
||||
|
||||
@@ -68,13 +68,13 @@ impl<P: AsRef<Path> + Send + Sync> Compose for File<P> {
|
||||
"-i",
|
||||
];
|
||||
|
||||
let output = Command::new("ffprobe")
|
||||
let mut output = Command::new("ffprobe")
|
||||
.args(&args)
|
||||
.output()
|
||||
.await
|
||||
.map_err(|e| AudioStreamError::Fail(Box::new(e)))?;
|
||||
|
||||
AuxMetadata::from_ffprobe_json(&output.stdout[..])
|
||||
AuxMetadata::from_ffprobe_json(&mut output.stdout[..])
|
||||
.map_err(|e| AudioStreamError::Fail(Box::new(e)))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -59,13 +59,15 @@ impl YoutubeDl {
|
||||
async fn query(&mut self) -> Result<Output, AudioStreamError> {
|
||||
let ytdl_args = ["-j", &self.url, "-f", "ba[abr>0][vcodec=none]/best"];
|
||||
|
||||
let output = Command::new(self.program)
|
||||
let mut output = Command::new(self.program)
|
||||
.args(&ytdl_args)
|
||||
.output()
|
||||
.await
|
||||
.map_err(|e| AudioStreamError::Fail(Box::new(e)))?;
|
||||
|
||||
let stdout: Output = serde_json::from_slice(&output.stdout[..])
|
||||
// NOTE: must be mut for simd-json.
|
||||
#[allow(clippy::unnecessary_mut_passed)]
|
||||
let stdout: Output = crate::json::from_slice(&mut output.stdout[..])
|
||||
.map_err(|e| AudioStreamError::Fail(Box::new(e)))?;
|
||||
|
||||
self.metadata = Some(stdout.as_aux_metadata());
|
||||
|
||||
Reference in New Issue
Block a user