<p>If I wanted to store lists of data in boltdb that could be fed to html/template, what would be the best performing format? I wanted to avoid JSON reflect overhead if possible.</p>
<hr/>**评论:**<br/><br/>THEHIPP0: <pre><p>I currently use the <a href="https://github.com/gogo/protobuf" rel="nofollow">faster implementation of Protocol buffers</a>, but you should check if <code>gob</code> or even <code>JSON</code> isn't fast enough for you.</p>
<p>To clarify: I only use protobuf because instead of rendering templates I use <a href="http://grpc.io" rel="nofollow">gRPC</a> and had to write the protobuf defintions anyway.</p></pre>benbjohnson: <pre><p>I use gogoprotobuf for encoding when I use Bolt in applications. There are options to use the <code>unsafe</code> package which lets you avoid some allocations.</p>
<p>I use gogoprotobuf for <a href="https://github.com/benbjohnson/scuttlebutt" rel="nofollow">this application</a> and my decoding time is about 12µs per object (and that converts from the internal protobuf type to an application type). Those benchmarks are coming from a small $5/mo DigitalOcean box too so it's not a beefy server.</p>
<p>If you really need performance then you could also use a generated template language like <a href="https://github.com/benbjohnson/ego" rel="nofollow">ego</a>.</p></pre>inhies: <pre><p>JSON is great for debugging while building your application, gob will be faster though.</p></pre>elithrar_: <pre><p>encoding/json is nearly always faster than gob, if you are using once-off encoders—which is likely when encoding templates. gob is sometimes faster when you're using it as intended: for streaming encoding.</p></pre>inhies: <pre><p>I based my comment off of these benchmarks:</p>
<p>BenchmarkGobEncode 1000000 2191 ns/op</p>
<p>BenchmarkJsonEncode 500000 4738 ns/op </p>
<p>BenchmarkGobDecode 1000000 2019 ns/op</p>
<p>BenchmarkJsonDecode 200000 12993 ns/op</p>
<p>From <a href="https://npf.io/2014/07/intro-to-boltdb-painless-performant-persistence/" rel="nofollow">https://npf.io/2014/07/intro-to-boltdb-painless-performant-persistence/</a></p></pre>elithrar_: <pre><p>encoding/json will be much faster than html/template, so I wouldn't discount it. template parsing is (relatively speaking) pretty slow due to escaping requirements. </p>
<p>Alternatively, you could use something like ffjson (<a href="https://github.com/pquerna/ffjson" rel="nofollow">https://github.com/pquerna/ffjson</a>) to generate Marshal/Unmarshal methods on your structs and avoid reflection where possible, noting that reflection isn't always the performance killer it is perceived to be. As I touched on before, template rendering and DB access will be the bottlenecks. </p></pre>guesdo: <pre><p><a href="https://golang.org/pkg/encoding/gob/" rel="nofollow">https://golang.org/pkg/encoding/gob/</a></p>
<p>That might help. Although I would just use JSON or BSON or MsgPack.</p></pre>seufert: <pre><p>I a Go only solution is sufficient then just use Go. It's the most convenient solution to use: No external description of your datastructure needed, best support for datatypes (afaik no time.Time on Protobuf for example) and the performance is much better than json. It's also quite conventient to add or remove fields.</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传