Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

250x faster iterator seek #329

Merged
merged 1 commit into from
Dec 19, 2016
Merged

250x faster iterator seek #329

merged 1 commit into from
Dec 19, 2016

Conversation

peakji
Copy link
Member

@peakji peakji commented Dec 19, 2016

Sorry for the scary title, but I'm not kidding! 🤓

Benchmarks first (on my MacBook @ 1.2 GHz Intel Core M):

SCAN: 5147.625ms
SKIP: 9171.527ms

SCAN_NEW: 5143.952ms
SKIP_NEW: 36.186ms

@kesla s amazing #96 brought super fast read streams, but I see some modules using something like:

function fuzzyGet (gt, lt){
	db.createReadStream({
		gt: gt,
		lt: lt,
		limit: 1
	}).on(...)
}

The buffering mechanism is wasting IO in this use case.

I want to keep the good parts of both worlds, so I've added a new bool landed to iterator, and let it ignore highWaterMark for once after seeking:

2016-12-19 12 14 48

Now we can do really really fast skip scan, sorted set intersection and K-way merging, with multiple seeks from #323.

This change is transparent to levelup and users, and will not affect normal iterating without iterator.seek.

@peakji
Copy link
Member Author

peakji commented Dec 19, 2016

Heres the benchmark script, please ignore the nasty semicolons (the pride and joy of a C programmer) 😜

const leveldown = require("/Users/PeakJi/Gits/leveldown");
const DBPATH = __dirname + "/tempdb";

var targets = [];
var db = leveldown(DBPATH);
db.open(function(err) {
	var batch = db.batch();
	for (var i = 0; i < 0xFFFFF; i++) {
		var buf = Buffer.allocUnsafe(4);
		buf.writeUInt32BE(i, 0, true);
		if (i > 0 && i % 1000 == 0)
			targets.push(buf);
		batch.put(buf, buf);
	}
	batch.write(function(err) {
		console.time("SKIP");
		skip(function(err) {
			console.timeEnd("SKIP");
			db.close(function(err) {
				leveldown.destroy(DBPATH, function(err) {

				});
			});
		});
	});
});

function scan(end) {
	var ite = db.iterator();
	ite.next(function iterate(err, key, val) {
		if (key)
			ite.next(iterate);
		else {
			ite.end(function(err) {
				end();
			});
		}
	});
}

function skip(end) {
	var ite = db.iterator();
	var run = function(i) {
		if (i == targets.length) {
			ite.end(function(err) {
				end();
			});
			return;
		}
		ite.seek(targets[i]);
		ite.next(function(err, key, val) {
			run(i + 1);
		});
	};
	run(0);
}

Copy link
Member

@juliangruber juliangruber left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

impressive improvement!

@ralphtheninja
Copy link
Member

Now we can do really really fast skip scan, sorted set intersection and K-way merging, with multiple seeks from #323.

What's K-way merging?

@peakji
Copy link
Member Author

peakji commented Dec 19, 2016

@ralphtheninja Its actually called K-Way Merge Algorithms or K-way merge sort, useful in external sorting and inverted index.

@dominictarr
Copy link
Contributor

wow, great work @peakji

@ralphtheninja
Copy link
Member

@peakji I'm just going through the code again. If I understand correctly, the first .next() you do after a .seek() will ignore highWaterMark and not buffer the results in order to optimize .seek(), .next(), .seek(), .next() etc, but once you do .seek(), .next(), .next(), .next() etc then you will buffer. Correct?

@peakji
Copy link
Member Author

peakji commented Jun 20, 2018

@ralphtheninja 💯👌

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants