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

Speed up integer and float printing #86

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ fn main() {
map.insert("y".to_string(), 2.0);

let s = serde_json::to_string(&map).unwrap();
assert_eq!(s, "{\"x\":1,\"y\":2}");
assert_eq!(s, "{\"x\":1.0,\"y\":2.0}");

let deserialized_map: Map<String, f64> = serde_json::from_str(&s).unwrap();
assert_eq!(map, deserialized_map);
Expand Down Expand Up @@ -78,7 +78,7 @@ fn main() {
let point = Point { x: 1.0, y: 2.0 };

let s = serde_json::to_string(&point).unwrap();
assert_eq!(s, "{\"x\":1,\"y\":2}");
assert_eq!(s, "{\"x\":1.0,\"y\":2.0}");

let deserialized_point: Point = serde_json::from_str(&s).unwrap();
assert_eq!(point, deserialized_point);
Expand Down
2 changes: 2 additions & 0 deletions json/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@ serde = "^0.7.0"
num-traits = "~0.1.32"
clippy = { version = "^0.*", optional = true }
linked-hash-map = { version = "0.0.11", optional = true }
itoa = "0.1"
dtoa = "0.1"
2 changes: 2 additions & 0 deletions json/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@
extern crate num_traits;
extern crate core;
extern crate serde;
extern crate itoa;
extern crate dtoa;
#[cfg(feature = "preserve_order")]
extern crate linked_hash_map;

Expand Down
27 changes: 15 additions & 12 deletions json/src/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ use std::num::FpCategory;
use serde::ser;
use super::error::{Error, ErrorCode, Result};

use itoa;
use dtoa;

/// A structure for serializing Rust values into JSON.
pub struct Serializer<W, F=CompactFormatter> {
writer: W,
Expand Down Expand Up @@ -77,52 +80,52 @@ impl<W, F> ser::Serializer for Serializer<W, F>

#[inline]
fn serialize_isize(&mut self, value: isize) -> Result<()> {
write!(&mut self.writer, "{}", value).map_err(From::from)
itoa::write(&mut self.writer, value).map_err(From::from)
}

#[inline]
fn serialize_i8(&mut self, value: i8) -> Result<()> {
write!(&mut self.writer, "{}", value).map_err(From::from)
itoa::write(&mut self.writer, value).map_err(From::from)
}

#[inline]
fn serialize_i16(&mut self, value: i16) -> Result<()> {
write!(&mut self.writer, "{}", value).map_err(From::from)
itoa::write(&mut self.writer, value).map_err(From::from)
}

#[inline]
fn serialize_i32(&mut self, value: i32) -> Result<()> {
write!(&mut self.writer, "{}", value).map_err(From::from)
itoa::write(&mut self.writer, value).map_err(From::from)
}

#[inline]
fn serialize_i64(&mut self, value: i64) -> Result<()> {
write!(&mut self.writer, "{}", value).map_err(From::from)
itoa::write(&mut self.writer, value).map_err(From::from)
}

#[inline]
fn serialize_usize(&mut self, value: usize) -> Result<()> {
write!(&mut self.writer, "{}", value).map_err(From::from)
itoa::write(&mut self.writer, value).map_err(From::from)
}

#[inline]
fn serialize_u8(&mut self, value: u8) -> Result<()> {
write!(&mut self.writer, "{}", value).map_err(From::from)
itoa::write(&mut self.writer, value).map_err(From::from)
}

#[inline]
fn serialize_u16(&mut self, value: u16) -> Result<()> {
write!(&mut self.writer, "{}", value).map_err(From::from)
itoa::write(&mut self.writer, value).map_err(From::from)
}

#[inline]
fn serialize_u32(&mut self, value: u32) -> Result<()> {
write!(&mut self.writer, "{}", value).map_err(From::from)
itoa::write(&mut self.writer, value).map_err(From::from)
}

#[inline]
fn serialize_u64(&mut self, value: u64) -> Result<()> {
write!(&mut self.writer, "{}", value).map_err(From::from)
itoa::write(&mut self.writer, value).map_err(From::from)
}

#[inline]
Expand Down Expand Up @@ -555,7 +558,7 @@ fn fmt_f32_or_null<W>(wr: &mut W, value: f32) -> Result<()>
try!(wr.write_all(b"null"))
}
_ => {
try!(write!(wr, "{:?}", value))
try!(dtoa::write(wr, value))
}
}

Expand All @@ -570,7 +573,7 @@ fn fmt_f64_or_null<W>(wr: &mut W, value: f64) -> Result<()>
try!(wr.write_all(b"null"))
}
_ => {
try!(write!(wr, "{:?}", value))
try!(dtoa::write(wr, value))
}
}

Expand Down
12 changes: 4 additions & 8 deletions json_tests/tests/test_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,18 +117,14 @@ fn test_write_i64() {

#[test]
fn test_write_f64() {
let min_string = format!("{:?}", f64::MIN);
let max_string = format!("{:?}", f64::MAX);
let epsilon_string = format!("{:?}", f64::EPSILON);

let tests = &[
(3.0, "3"),
(3.0, "3.0"),
(3.1, "3.1"),
(-1.5, "-1.5"),
(0.5, "0.5"),
(f64::MIN, &min_string),
(f64::MAX, &max_string),
(f64::EPSILON, &epsilon_string),
(f64::MIN, "-1.7976931348623157e308"),
(f64::MAX, "1.7976931348623157e308"),
(f64::EPSILON, "2.220446049250313e-16"),
];
test_encode_ok(tests);
test_pretty_encode_ok(tests);
Expand Down