From 76d29eb5912b91f9222c8e542673902fb87d2ae0 Mon Sep 17 00:00:00 2001 From: bott Date: Fri, 26 Oct 2018 03:36:54 +0200 Subject: Improve bench reducing the number of calls to writer.write_str --- askama_derive/src/generator.rs | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/askama_derive/src/generator.rs b/askama_derive/src/generator.rs index bc009f9..49c0d02 100644 --- a/askama_derive/src/generator.rs +++ b/askama_derive/src/generator.rs @@ -597,18 +597,27 @@ impl<'a> Generator<'a> { fn write_lit(&mut self, buf: &mut Buffer, lws: &'a str, val: &str, rws: &'a str) { assert!(self.next_ws.is_none()); - if !lws.is_empty() { + let lws_write = if !lws.is_empty() { if self.skip_ws { self.skip_ws = false; + false } else if val.is_empty() { assert!(rws.is_empty()); self.next_ws = Some(lws); + false } else { - buf.writeln(&format!("writer.write_str({:#?})?;", lws)); + true } - } + } else { + false + }; + if !val.is_empty() { - buf.writeln(&format!("writer.write_str({:#?})?;", val)); + if lws_write { + buf.writeln(&format!("writer.write_str({:#?})?;", [lws, val].join(""))); + } else { + buf.writeln(&format!("writer.write_str({:#?})?;", val)); + } } if !rws.is_empty() { self.next_ws = Some(rws); -- cgit