question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Inadvertently removed the `buffer/asBuffer` optimization

See original GitHub issue

Continuing from https://github.com/Level/encoding-down/pull/17#issuecomment-337314850. I found this issue trying to make sense of the buffer option on encodings, and eventually traced it back to levelup.

In levelup@1, we had an optimization for string-based encodings like json:

exports.json = {
  encode: JSON.stringify,
  decode: JSON.parse,
  buffer: false,
  type: 'json'
}

The important property here is buffer: false. This told levelup to prefer to fetch a string from the underlying store, rather than a Buffer. The underlying store would receive { asBuffer: false } in get options and similar places.

This is how the code looked:

LevelUP.prototype.get = function (key_, options, callback) {
  options.asBuffer = this._codec.valueAsBuffer(options)

  this.db.get(key, options, function (err, value) {}
}

When we removed the encoding logic from levelup (#458), the buffer/asBuffer logic was also removed and did not get a replacement elsewhere.

So since levelup@2, underlying stores always see { asBuffer: true }. The means we incur a conversion cost, sometimes even double conversion:

  • leveldown always returns keys and values as Buffers (with an additional memcpy cost, but let’s leave that aside for a moment)
  • memdown as well will convert strings to Buffers (if they were stored as strings, which is true for the json encoding at least)
  • The encodings convert Buffers back to strings (in the case of json it implicitly does JSON.parse(x.toString())).

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
juliangrubercommented, Oct 19, 2017


Since this is about encodings, and needs to know about encodings, can we solve this in encoding-down? This part of the stack knows which option is best, and should then be able to pass it to the underlying store.

Great find, @vweevers!



1reaction
vweeverscommented, Oct 19, 2017

Before we decide and implement a fix, it would be nice to have some failing tests for this behavior.

👍 I’ll rewrite what I have into a test for encoding-down.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Doc
This is to prevent you from losing your edits if you accidentally delete a large chunk of the buffer and don't notice it...
Read more >
Fragment-based screening and discovery of small molecule
3.2.5 Optimization of fragment 1 triazoloquinazolinone series. ... removed with thrombin (10 units of thrombin per mg of TIGIT).
Read more >
(PDF) From Citizens' Participation to Co-governance. Is ...
The chapter examines innovative financial funding mechanisms that can spur private-sector investment in urban heritage regeneration projects. In recent years, ...
Read more >
(PDF) VLSI Design Interview Questions
Adding delay/buffer[as buffer offers lesser delay, we go for spl Delay cells ... As clock-tree runs across the whole chip, optimizing the design...
Read more >
Harris Quantitative Chemical Analysis 8th Edition | David ...
My motive was to remove precipitation titrations from the critical ... The trap prevents liquid from being accidentally sucked into the vacuum ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found