From 9e61092fc6bf0068d4d0c5fe0b2fe4e9a46f7e49 Mon Sep 17 00:00:00 2001
From: Niels <niels.lohmann@gmail.com>
Date: Tue, 30 Dec 2014 10:54:01 +0100
Subject: [PATCH] + overworked parser

---
 Makefile.am    |  5 ++++-
 json_parser.py |  6 ++++++
 src/JSON.cc    | 11 +++++++++--
 3 files changed, 19 insertions(+), 3 deletions(-)
 create mode 100755 json_parser.py

diff --git a/Makefile.am b/Makefile.am
index 49140d75..ce095de3 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -7,7 +7,7 @@ json_unit_CXXFLAGS = $(FLAGS) -std=c++11
 json_unit_CPPFLAGS = -I$(top_srcdir)/src -I$(top_srcdir)/test -Dprivate=public
 
 json_parser_SOURCES = src/JSON.cc src/JSON.h benchmark/parse.cc
-json_parser_CXXFLAGS = $(FLAGS) -std=c++11
+json_parser_CXXFLAGS = -O3 -std=c++11 -flto
 json_parser_CPPFLAGS = -I$(top_srcdir)/src -I$(top_srcdir)/benchmark
 
 cppcheck:
@@ -24,3 +24,6 @@ pretty:
 	   --align-reference=type --add-brackets --convert-tabs --close-templates \
 	   --lineend=linux --preserve-date --suffix=none \
 	   $(SOURCES)
+
+parser:
+	make CXXFLAGS="" json_parser
\ No newline at end of file
diff --git a/json_parser.py b/json_parser.py
new file mode 100755
index 00000000..4e97b2ca
--- /dev/null
+++ b/json_parser.py
@@ -0,0 +1,6 @@
+#!/usr/bin/env python
+
+import sys
+import json
+
+j = json.load(sys.stdin)
diff --git a/src/JSON.cc b/src/JSON.cc
index 5445404e..43ae8026 100644
--- a/src/JSON.cc
+++ b/src/JSON.cc
@@ -1776,8 +1776,15 @@ Initialize the JSON parser given an input stream \p _is.
 JSON::Parser::Parser(std::istream& _is)
 {
     // copy stream to string
-    std::istreambuf_iterator<char> eos;
-    std::string string_input(std::istreambuf_iterator<char>(_is), eos);
+    std::string input_line, string_input;
+
+    // from http://www.manticmoo.com/articles/jeff/programming/c++/making-io-streams-efficient-in-c++.php
+    //  Don't sync C++ and C I/O
+    std::ios_base::sync_with_stdio(false);
+    while(_is) {
+      std::getline(_is, input_line);
+      string_input += input_line;
+    }
 
     _length = string_input.size();
     _buffer = new char[_length + 1];