Skip to content

Commit

Permalink
Speed improvements
Browse files Browse the repository at this point in the history
Changes:
- Rewrite Box.
  - Process files during loading file list.
  - Delay file hash check. Use File.changed() to check if file changed.
  - Remove watch delay.
  - Parallel processing.
- Save rendered content in warehouse. It really saves a lot of time.
- Rewrite generate console.
  - Cache rendered content so we don't have to render it again.
  - Load file list from cache. Don't delete other files in public folder. Resolve #1310
  - Remove file stat check.
  - Parallel generating.
- Resolve race conditions.

Known issues:
- Parallel processing and parallel generating may cause race conditions. We have to solve this in warehouse.
- Tests are not updated yet.
  • Loading branch information
tommy351 committed Dec 1, 2015
1 parent 79c7ec2 commit 9ba40f4
Show file tree
Hide file tree
Showing 16 changed files with 669 additions and 807 deletions.
81 changes: 4 additions & 77 deletions lib/box/file.js
Original file line number Diff line number Diff line change
@@ -1,76 +1,20 @@
'use strict';

var fs = require('hexo-fs');
var Promise = require('bluebird');

var escapeBOM = fs.escapeBOM;
var escapeEOL = fs.escapeEOL;

function File(data) {
this.source = data.source;
this.path = data.path;
this.type = data.type;
this.params = data.params;
this.content = data.content;
this.stats = data.stats;
}

function wrapReadOptions(options) {
options = options || {};
if (typeof options === 'string') options = {encoding: options};
if (!options.hasOwnProperty('encoding')) options.encoding = 'utf8';
if (!options.hasOwnProperty('cache')) options.cache = true;
if (!options.hasOwnProperty('escape')) options.escape = true;

return options;
}

function escapeContent(str) {
return escapeBOM(escapeEOL(str));
this.type = data.type;
}

File.prototype.read = function(options, callback) {
if (!callback && typeof options === 'function') {
callback = options;
options = {};
}

var self = this;
var content = this.content;

options = wrapReadOptions(options);

return new Promise(function(resolve, reject) {
if (!options.cache || !content) {
return fs.readFile(self.source, options).then(resolve, reject);
}

var encoding = options.encoding;
if (!encoding) return resolve(content);

var result = content.toString(encoding);
if (options.escape) return resolve(escapeContent(result));

resolve(result);
}).asCallback(callback);
return fs.readFile(this.source, options).asCallback(callback);
};

File.prototype.readSync = function(options) {
var content = this.content;

options = wrapReadOptions(options);

if (!options.cache || !content) {
return fs.readFileSync(this.source, options);
}

var encoding = options.encoding;
if (!encoding) return content;

var result = content.toString(encoding);
if (options.escape) return escapeContent(result);

return result;
return fs.readFileSync(this.source, options);
};

File.prototype.stat = function(options, callback) {
Expand All @@ -79,27 +23,10 @@ File.prototype.stat = function(options, callback) {
options = {};
}

options = options || {};

var stats = this.stats;
var cache = options.hasOwnProperty('cache') ? options.cache : true;
var self = this;

return new Promise(function(resolve, reject) {
if (stats && cache) return resolve(stats);

fs.stat(self.source).then(resolve, reject);
}).asCallback(callback);
return fs.stat(this.source).asCallback(callback);
};

File.prototype.statSync = function(options) {
options = options || {};

var cache = options.hasOwnProperty('cache') ? options.cache : true;
var stats = this.stats;

if (stats && cache) return stats;

return fs.statSync(this.source);
};

Expand Down
Loading

3 comments on commit 9ba40f4

@Xuanwo
Copy link
Contributor

@Xuanwo Xuanwo commented on 9ba40f4 Dec 4, 2015

Choose a reason for hiding this comment

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

I think we should add a speed test for hexo which has about 500 simple articles, 300 articles with image and 200 articles with complex struct.

@tommy351
Copy link
Member Author

Choose a reason for hiding this comment

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

I use your blog for test currently. XD

BTW, hot processing is much faster in this commit because the rendered content is cached. Obviously rendering is the bottleneck.

Definitions:

  • Cold processing: 1st-time processing (without cache)
  • Hot processing: 2nd-time processing (with cache)

Hexo 3.1:

  • Cold processing: 36s
  • Hot processing: 40s (same to cold processing)

This commit:

  • Cold processing: 33s
  • Hot processing: 4.3s

However many tests are failed due to this commit and I'm still fixing. orz

@Xuanwo
Copy link
Contributor

@Xuanwo Xuanwo commented on 9ba40f4 Dec 4, 2015

Choose a reason for hiding this comment

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

Orz, I need write more article for test, 2333

Come on, let's make a faster & better hexo~

Please sign in to comment.