Created
April 12, 2010 17:52
-
-
Save ry/363823 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
commit 47d65f65e879565e3f9038738e14da9d8812283c | |
Author: Micheil Smith <micheil@brandedcode.com> | |
Date: Tue Apr 13 02:27:32 2010 +1000 | |
Moving the http.js, net.js FreeList to being standalone. | |
diff --git a/lib/freelist.js b/lib/freelist.js | |
new file mode 100644 | |
index 0000000..a09fb4b | |
--- /dev/null | |
+++ b/lib/freelist.js | |
@@ -0,0 +1,22 @@ | |
+// This is a free list to avoid creating so many of the same object. | |
+exports.FreeList = function(name, max, constructor) { | |
+ this.name = name; | |
+ this.constructor = constructor; | |
+ this.max = max; | |
+ this.list = []; | |
+} | |
+ | |
+ | |
+exports.FreeList.prototype.alloc = function () { | |
+ //debug("alloc " + this.name + " " + this.list.length); | |
+ return this.list.length ? this.list.shift() | |
+ : this.constructor.apply(this, arguments); | |
+}; | |
+ | |
+ | |
+exports.FreeList.prototype.free = function (obj) { | |
+ //debug("free " + this.name + " " + this.list.length); | |
+ if (this.list.length < this.max) { | |
+ this.list.push(obj); | |
+ } | |
+}; | |
diff --git a/lib/http.js b/lib/http.js | |
index 554f047..19a761c 100644 | |
--- a/lib/http.js | |
+++ b/lib/http.js | |
@@ -11,14 +11,13 @@ var sys = require('sys'); | |
var net = require('net'); | |
var events = require('events'); | |
+var FreeList = require('freelist').FreeList; | |
var HTTPParser = process.binding('http_parser').HTTPParser; | |
-var parserFreeList = []; | |
- | |
-function newParser (type) { | |
+var parsers = new FreeList('parsers', 1000, function (type) { | |
var parser; | |
- if (parserFreeList.length) { | |
- parser = parserFreeList.shift(); | |
+ if (this.list.length) { | |
+ parser = this.list.shift(); | |
parser.reinitialize(type); | |
} else { | |
parser = new HTTPParser(type); | |
@@ -111,11 +110,7 @@ function newParser (type) { | |
}; | |
} | |
return parser; | |
-} | |
- | |
-function freeParser (parser) { | |
- if (parserFreeList.length < 1000) parserFreeList.push(parser); | |
-} | |
+}); | |
var CRLF = "\r\n"; | |
@@ -496,7 +491,8 @@ function connectionListener (socket) { | |
// we need to keep track of the order they were sent. | |
var responses = []; | |
- var parser = newParser('request'); | |
+ var parser = parsers.alloc('request'); | |
+ parser.socket = socket; | |
socket.ondata = function (d, start, end) { | |
parser.execute(d, start, end - start); | |
@@ -505,7 +501,7 @@ function connectionListener (socket) { | |
socket.onend = function () { | |
parser.finish(); | |
// unref the parser for easy gc | |
- freeParser(parser); | |
+ parsers.free(parser); | |
if (responses.length == 0) { | |
socket.close(); | |
@@ -514,7 +510,6 @@ function connectionListener (socket) { | |
} | |
}; | |
- parser.socket = socket; | |
// The following callback is issued after the headers have been read on a | |
// new message. In this callback we setup the response object and pass it | |
// to the user. | |
@@ -542,7 +537,7 @@ function Client ( ) { | |
var requests = []; | |
var currentRequest; | |
- var parser = newParser('response'); | |
+ var parser = parsers.alloc('response'); | |
parser.socket = this; | |
self._reconnect = function () { | |
@@ -579,6 +574,7 @@ function Client ( ) { | |
self.addListener("end", function () { | |
parser.finish(); | |
+ parsers.free(parser); | |
debug("self got end closing. readyState = " + self.readyState); | |
self.close(); | |
diff --git a/lib/net.js b/lib/net.js | |
index e5460ce..daee550 100644 | |
--- a/lib/net.js | |
+++ b/lib/net.js | |
@@ -18,6 +18,7 @@ var binding = process.binding('net'); | |
// yet convinced that every use-case can be fit into that abstraction, so | |
// waiting to implement it until I get more experience with this. | |
var Buffer = require('buffer').Buffer; | |
+var FreeList = require('freelist').FreeList; | |
var IOWatcher = process.IOWatcher; | |
var assert = process.assert; | |
@@ -202,35 +203,6 @@ var timeout = new (function () { | |
}; | |
})(); | |
- | |
- | |
- | |
- | |
-// This is a free list to avoid creating so many of the same object. | |
- | |
-function FreeList (name, max, constructor) { | |
- this.name = name; | |
- this.constructor = constructor; | |
- this.max = max; | |
- this.list = []; | |
-} | |
- | |
- | |
-FreeList.prototype.alloc = function () { | |
- //debug("alloc " + this.name + " " + this.list.length); | |
- return this.list.length ? this.list.shift() | |
- : this.constructor.apply(this, arguments); | |
-}; | |
- | |
- | |
-FreeList.prototype.free = function (obj) { | |
- //debug("free " + this.name + " " + this.list.length); | |
- if (this.list.length < this.max) { | |
- this.list.push(obj); | |
- } | |
-}; | |
- | |
- | |
var ioWatchers = new FreeList("iowatcher", 100, function () { | |
return new IOWatcher(); | |
}); | |
diff --git a/src/node.cc b/src/node.cc | |
index 452c111..94bfa97 100644 | |
--- a/src/node.cc | |
+++ b/src/node.cc | |
@@ -1193,6 +1193,7 @@ static Handle<Value> Binding(const Arguments& args) { | |
exports->Set(String::New("dns"), String::New(native_dns)); | |
exports->Set(String::New("events"), String::New(native_events)); | |
exports->Set(String::New("file"), String::New(native_file)); | |
+ exports->Set(String::New("freelist"), String::New(native_freelist)); | |
exports->Set(String::New("fs"), String::New(native_fs)); | |
exports->Set(String::New("http"), String::New(native_http)); | |
exports->Set(String::New("http_old"), String::New(native_http_old)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment