From 26314e743e2c4f38eb6c5824bf51209099000f9f Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Fri, 8 May 2015 19:06:38 +0200 Subject: [PATCH] imp(API): faster null-value removal Previously reserialization of token streams with removed null values was performed on a byte-per-byte basis, which was quite inefficient to say the least. Now it uses `io::copy` to copy in chunks of 65kb, which makes out our throughput and should deliver about 150MB/s at least. --- src/mako/api/lib/mbuild.mako | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/mako/api/lib/mbuild.mako b/src/mako/api/lib/mbuild.mako index 3c62bb1cb90..d5ce19689de 100644 --- a/src/mako/api/lib/mbuild.mako +++ b/src/mako/api/lib/mbuild.mako @@ -483,6 +483,9 @@ match result { % if URL_ENCODE in special_cases: use url::percent_encoding::{percent_encode, FORM_URLENCODED_ENCODE_SET}; % endif + % if request_value: + use json_tools::{TokenReader, Lexer, BufferType, TokenType, FilterTypedKeyValuePairs}; + % endif use std::io::{Read, Seek}; use hyper::header::{ContentType, ContentLength, Authorization, UserAgent, Location}; let mut dd = DefaultDelegate; @@ -666,14 +669,16 @@ else { let mut request_value_reader = { let json_cache = json::to_string(&self.${property(REQUEST_VALUE_PROPERTY_NAME)}).unwrap(); - io::Cursor::new(json_tools::TokenReader::new( - json_tools::FilterTypedKeyValuePairs::new( - json_tools::Lexer::new( + let mut mem_dst = io::Cursor::new(Vec::with_capacity(json_cache.len())); + io::copy(&mut TokenReader::new( + FilterTypedKeyValuePairs::new( + Lexer::new( json_cache.bytes(), - json_tools::BufferType::Span), - json_tools::TokenType::Null), - Some(&json_cache)).bytes().filter_map(|v|v.ok()).collect::${'>'}() - ) + BufferType::Span), + TokenType::Null), + Some(&json_cache)), &mut mem_dst).unwrap(); + mem_dst.seek(io::SeekFrom::Start(0)).unwrap(); + mem_dst }; let request_size = request_value_reader.seek(io::SeekFrom::End(0)).unwrap(); request_value_reader.seek(io::SeekFrom::Start(0)).unwrap();