From a011c3243f6f1508c5b3412659db829b73e08867 Mon Sep 17 00:00:00 2001 From: Sakthipriyan Vairamani Date: Wed, 3 Jun 2015 03:41:04 +0530 Subject: [PATCH] fs: minor refactoring 1. Remove a few unnecessary variables to reduce LoC. 2. Remove redundant `var` definitions of variables in same function. 3. Refactor variables which are defined inside a block and used outside as well. 4. Refactor effect-less code. 5. In `rethrow` function, instead of assigning to `err` and throwing `err` directly throw `backtrace` object. 6. Reassign a defined parameter while also mentioning arguments in the body is one of the optimization killers. So, changing `callback` to `callback_` and declaring a new variable called `callback` in the body. PR-URL: https://github.com/nodejs/io.js/pull/1870 Reviewed-By: Ben Noordhuis Reviewed-By: Trevor Norris --- lib/fs.js | 57 ++++++++++++++++++++++++++++--------------------------- 1 file changed, 29 insertions(+), 28 deletions(-) diff --git a/lib/fs.js b/lib/fs.js index 8d40a1268185e1..32369c009ae2cb 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -49,8 +49,7 @@ function rethrow() { if (err) { backtrace.stack = err.name + ': ' + err.message + backtrace.stack.substr(backtrace.name.length); - err = backtrace; - throw err; + throw backtrace; } }; } @@ -267,27 +266,25 @@ function ReadFileContext(callback, encoding) { } ReadFileContext.prototype.read = function() { - var fd = this.fd; - var size = this.size; var buffer; var offset; var length; - if (size === 0) { + if (this.size === 0) { buffer = this.buffer = new SlowBuffer(kReadFileBufferLength); offset = 0; length = kReadFileBufferLength; } else { buffer = this.buffer; offset = this.pos; - length = size - this.pos; + length = this.size - this.pos; } var req = new FSReqWrap(); req.oncomplete = readFileAfterRead; req.context = this; - binding.read(fd, buffer, offset, length, -1, req); + binding.read(this.fd, buffer, offset, length, -1, req); }; ReadFileContext.prototype.close = function(err) { @@ -302,8 +299,7 @@ function readFileAfterOpen(err, fd) { var context = this.context; if (err) { - var callback = context.callback; - callback(err); + context.callback(err); return; } @@ -417,7 +413,7 @@ fs.readFileSync = function(path, options) { if (size === 0) { buffers = []; } else { - var threw = true; + threw = true; try { buffer = new Buffer(size); threw = false; @@ -427,16 +423,18 @@ fs.readFileSync = function(path, options) { } var done = false; + var bytesRead; + while (!done) { - var threw = true; + threw = true; try { if (size !== 0) { - var bytesRead = fs.readSync(fd, buffer, pos, size - pos); + bytesRead = fs.readSync(fd, buffer, pos, size - pos); } else { // the kernel lies about many files. // Go ahead and try to read some bytes. buffer = new Buffer(8192); - var bytesRead = fs.readSync(fd, buffer, 0, 8192); + bytesRead = fs.readSync(fd, buffer, 0, 8192); if (bytesRead) { buffers.push(buffer.slice(0, bytesRead)); } @@ -529,8 +527,8 @@ function modeNum(m, def) { return undefined; } -fs.open = function(path, flags, mode, callback) { - callback = makeCallback(arguments[arguments.length - 1]); +fs.open = function(path, flags, mode, callback_) { + var callback = makeCallback(arguments[arguments.length - 1]); mode = modeNum(mode, 0o666); if (!nullCheck(path, callback)) return; @@ -585,10 +583,12 @@ fs.read = function(fd, buffer, offset, length, position, callback) { fs.readSync = function(fd, buffer, offset, length, position) { var legacy = false; + var encoding; + if (!(buffer instanceof Buffer)) { // legacy string interface (fd, length, position, encoding, callback) legacy = true; - var encoding = arguments[3]; + encoding = arguments[3]; assertEncoding(encoding); @@ -623,6 +623,7 @@ fs.write = function(fd, buffer, offset, length, position, callback) { callback(err, written || 0, buffer); } + var req = new FSReqWrap(); if (buffer instanceof Buffer) { // if no position is passed then assume null if (typeof position === 'function') { @@ -630,7 +631,6 @@ fs.write = function(fd, buffer, offset, length, position, callback) { position = null; } callback = maybeCallback(callback); - var req = new FSReqWrap(); req.oncomplete = strWrapper; return binding.writeBuffer(fd, buffer, offset, length, position, req); } @@ -647,7 +647,6 @@ fs.write = function(fd, buffer, offset, length, position, callback) { length = 'utf8'; } callback = maybeCallback(position); - var req = new FSReqWrap(); req.oncomplete = bufWrapper; return binding.writeString(fd, buffer, offset, length, req); }; @@ -721,8 +720,10 @@ fs.truncateSync = function(path, len) { } // allow error to be thrown, but still close fd. var fd = fs.openSync(path, 'r+'); + var ret; + try { - var ret = fs.ftruncateSync(fd, len); + ret = fs.ftruncateSync(fd, len); } finally { fs.closeSync(fd); } @@ -875,7 +876,7 @@ function preprocessSymlinkDestination(path, type, linkPath) { } } -fs.symlink = function(destination, path, type_, callback) { +fs.symlink = function(destination, path, type_, callback_) { var type = (typeof type_ === 'string' ? type_ : null); var callback = makeCallback(arguments[arguments.length - 1]); @@ -968,9 +969,9 @@ if (constants.hasOwnProperty('O_SYMLINK')) { // prefer to return the chmod error, if one occurs, // but still try to close, and report closing errors if they occur. - var err, err2; + var err, err2, ret; try { - var ret = fs.fchmodSync(fd, mode); + ret = fs.fchmodSync(fd, mode); } catch (er) { err = er; } @@ -1088,8 +1089,8 @@ fs.futimesSync = function(fd, atime, mtime) { binding.futimes(fd, atime, mtime); }; -function writeAll(fd, buffer, offset, length, position, callback) { - callback = maybeCallback(arguments[arguments.length - 1]); +function writeAll(fd, buffer, offset, length, position, callback_) { + var callback = maybeCallback(arguments[arguments.length - 1]); // write(fd, buffer, offset, length, position, callback) fs.write(fd, buffer, offset, length, position, function(writeErr, written) { @@ -1112,7 +1113,7 @@ function writeAll(fd, buffer, offset, length, position, callback) { }); } -fs.writeFile = function(path, data, options, callback) { +fs.writeFile = function(path, data, options, callback_) { var callback = maybeCallback(arguments[arguments.length - 1]); if (!options || typeof options === 'function') { @@ -1634,8 +1635,8 @@ function ReadStream(path, options) { this.flags = options.flags === undefined ? 'r' : options.flags; this.mode = options.mode === undefined ? 0o666 : options.mode; - this.start = options.start === undefined ? undefined : options.start; - this.end = options.end === undefined ? undefined : options.end; + this.start = options.start; + this.end = options.end; this.autoClose = options.autoClose === undefined ? true : options.autoClose; this.pos = undefined; @@ -1804,7 +1805,7 @@ function WriteStream(path, options) { this.flags = options.flags === undefined ? 'w' : options.flags; this.mode = options.mode === undefined ? 0o666 : options.mode; - this.start = options.start === undefined ? undefined : options.start; + this.start = options.start; this.pos = undefined; this.bytesWritten = 0;