Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use serde_bytes crate #2598

Merged
merged 1 commit into from Apr 4, 2018
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

Use serde_bytes crate

This speeds up serializing and deserializing of binary data.
  • Loading branch information
jonleighton committed Apr 3, 2018
commit 74d05cc2f5eec1b4025edb0f601bd82e0bf7e131

Some generated files are not rendered by default. Learn more.

@@ -20,6 +20,7 @@ ipc-channel = {version = "0.10.0", optional = true}
euclid = { version = "0.17", features = ["serde"] }
serde = { version = "=1.0.35", features = ["rc"] }
serde_derive = { version = "=1.0.35", features = ["deserialize_in_place"] }
serde_bytes = "0.10"
time = "0.1"

[target.'cfg(target_os = "macos")'.dependencies]
@@ -2,6 +2,8 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

extern crate serde_bytes;

use app_units::Au;
use channel::{self, MsgSender, Payload, PayloadSender, PayloadSenderHelperMethods};
use std::cell::Cell;
@@ -405,7 +407,11 @@ pub struct UpdateImage {

#[derive(Clone, Deserialize, Serialize)]
pub enum AddFont {
Raw(FontKey, Vec<u8>, u32),
Raw(
FontKey,
#[serde(with = "serde_bytes")] Vec<u8>,
u32
),
Native(FontKey, NativeFontHandle),
}

@@ -2,6 +2,8 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

extern crate serde_bytes;

use font::{FontInstanceKey, FontKey, FontTemplate};
use std::sync::Arc;
use {DevicePoint, DeviceUintRect, IdNamespace, TileOffset, TileSize};
@@ -110,11 +112,26 @@ impl ImageDescriptor {

#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum ImageData {
Raw(Arc<Vec<u8>>),
Blob(BlobImageData),
Raw(#[serde(with = "serde_image_data_raw")] Arc<Vec<u8>>),
Blob(#[serde(with = "serde_bytes")] BlobImageData),
External(ExternalImageData),
}

mod serde_image_data_raw {

This comment has been minimized.

Copy link
@jonleighton

jonleighton Apr 1, 2018

Author Contributor

This is necessary because we are dealing with an Arc. However, maybe it would be better to patch serde_bytes to support this?

This comment has been minimized.

Copy link
@nox

This comment has been minimized.

Copy link
@dtolnay

dtolnay Apr 1, 2018

Could you propose a more general signature for serde_bytes that could work here?

This comment has been minimized.

Copy link
@jonleighton

jonleighton Apr 1, 2018

Author Contributor

Perhaps not, though I haven't looked into it very closely. Assuming a more general signature is not workable, maybe serde_bytes could also ship serde_bytes_rc/serde_bytes_arc modules though? (Essentially what I've done here, but inside the serde_bytes crate.)

extern crate serde_bytes;

use std::sync::Arc;
use serde::{Deserializer, Serializer};

pub fn serialize<'a, S: Serializer>(bytes: &'a Arc<Vec<u8>>, serializer: S) -> Result<S::Ok, S::Error> {
serde_bytes::serialize(bytes.as_slice(), serializer)
}

pub fn deserialize<'de, D: Deserializer<'de>>(deserializer: D) -> Result<Arc<Vec<u8>>, D::Error> {
serde_bytes::deserialize(deserializer).map(Arc::new)
}
}

impl ImageData {
pub fn new(bytes: Vec<u8>) -> Self {
ImageData::Raw(Arc::new(bytes))
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.