TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Show HN: A Fast, Malloc-Free C++14 Json Parser and Encoder

98 pointsby matt42over 10 years ago

9 comments

jlaroccoover 10 years ago
This uses std::string all over the place, which allocates memory under the hood.<p>I&#x27;m not sure how any JSON parser could avoid memory allocation without a difficult to use interface. Numbers, arrays, strings, and objects are unbounded by the JSON spec, so a truly malloc free library would need to provide a kind of streaming interfaces where things are returned in fixed sized chunks.<p>JSON wouldn&#x27;t be my first choice for data storage in situations where I needed to avoid dynamic memory.
评论 #8610930 未加载
评论 #8609142 未加载
评论 #8609236 未加载
评论 #8609151 未加载
评论 #8609213 未加载
评论 #8609286 未加载
评论 #8610981 未加载
评论 #8609081 未加载
评论 #8609939 未加载
habermanover 10 years ago
&gt; As of today, all json parsers rely dynamic data structures to parse and store the json objects.<p>I&#x27;m not sure that&#x27;s entirely fair. Callback-based parsers like YAJL leave the application free to store the data in whatever data structure they want, or even to stream-process the input without storing in a data structure at all.<p>But regardless, the meta-programming approach described here is interesting and novel. Generating structure-specific parsing code is a well-explored area (for example, Protocol Buffers is designed entirely around this idea), but doing it as C++ metaprogramming is a novel approach (Protocol Buffers relies on compile-time code generation).<p>I don&#x27;t actually understand how the object inspection and compile-time codegen works with this meta-programming approach; will be interesting to dig in a little deeper and learn more.
评论 #8608866 未加载
评论 #8610944 未加载
评论 #8610536 未加载
huhtenbergover 10 years ago
On one hand this is undoubtedly a very clever use of ++ features. On the other hand that <i>heck of a lot</i> of scaffolding (just look in &#x2F;iod directory) and the more scaffolding there is, the more caution is needed in adopting the code.<p>The same goal - not parsing what&#x27;s not needed - can be done with a conventional callback-based C code. You basically go through the json data, parse, say, a field and call the app asking &quot;is this ok? shall I proceed?&quot;. If it&#x27;s a yes, then you indeed proceed and parse out the value chunk and pass it to the app the same way. If it&#x27;s a no, you either abort or skip over the value. The end effect is the same - parsing of an invalid json input is aborted as soon as the app flags the first bad entry; and unwanted fields are never parsed in full.<p>So I seriously doubt that this is a little more than a marketing spin of a proud developer -<p><pre><code> This makes its performances impossible to match in other languages such as C or Java that do not provide static introspection. </code></pre> I am fairly certain that vurtun&#x27;s code [1] can match and most likely beat this lib&#x27;s performance, with ease.<p>[1] <a href="https://news.ycombinator.com/item?id=8609236" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=8609236</a>
评论 #8610957 未加载
评论 #8611002 未加载
denshover 10 years ago
See also: Scala Pickling [1]. Serialization and deserialisation logic optimised for specific datatype is generated purely at compile time using Scala Macros [2].<p>[1] <a href="http://lampwww.epfl.ch/~hmiller/pickling/" rel="nofollow">http:&#x2F;&#x2F;lampwww.epfl.ch&#x2F;~hmiller&#x2F;pickling&#x2F;</a><p>[2] <a href="http://scalamacros.org" rel="nofollow">http:&#x2F;&#x2F;scalamacros.org</a>
twicover 10 years ago
&gt; This makes its performances impossible to match in other languages such as C or Java that do not provide static introspection.<p>A CHALLENGE!<p>So, er, who&#x27;s up for it?<p>You could implement an analogue of this approach in Java. It&#x27;s true that Java doesn&#x27;t have language constructs that would let you do this as part of compilation, but Java has its ways. You could write an annotation processor to do this at compile time, or use a bytecode parser at runtime (this is yucky, but a fairly standard technique these days). Either way, the output would be a pair of synthetic classes which implemented the parser and encoder. A tool like this would be moderately laborious to write, but a straightforward matter of programming.
评论 #8609466 未加载
评论 #8609009 未加载
nlyover 10 years ago
RapidJSON claims to support &quot;in-situ parsing&quot;, which is presumably mostly zero copy, and presumably doesn&#x27;t allocate much either. I&#x27;d like to see benchmarks over comparable code.
评论 #8609195 未加载
dvtover 10 years ago
If you&#x27;re using C++, it&#x27;s by definition malloc-free ;)
评论 #8610543 未加载
rdtscover 10 years ago
&gt; In classic C or C++, you would define a function taking optional arguments as :<p>Is that true? Can classic C have default (optional) arguments?
评论 #8608655 未加载
评论 #8608615 未加载
rurbanover 10 years ago
Sorry, didn&#x27;t read the code yet. But malloc free means stack allocation, thus dangerous to stack attacks. Please clarify.
评论 #8608648 未加载
评论 #8608658 未加载