secret
Created

  • Download Gist
gistfile1.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
From 64440087d78cd064e67c4886a05619999b2c1b92 Mon Sep 17 00:00:00 2001
From: Micheil Smith <micheil@brandedcode.com>
Date: Wed, 14 Apr 2010 02:10:58 +1000
Subject: [PATCH] Implementing Freelist in a functional style, thanks Tim Caswell
 
---
lib/freelist.js | 40 +++++++++++++++++++++-------------------
lib/http.js | 4 ++--
lib/net.js | 4 ++--
3 files changed, 25 insertions(+), 23 deletions(-)
 
diff --git a/lib/freelist.js b/lib/freelist.js
index a09fb4b..217b9ee 100644
--- a/lib/freelist.js
+++ b/lib/freelist.js
@@ -1,22 +1,24 @@
// 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 = [];
-}
+// Think of it as a pool of reusable objects.
+module.exports = function FreeList(name, max, constructor) {
+ var list = [];
+
+ return {
+ name: name,
+ // Grabs an object from the pool or creates one if it's empty.
+ alloc: function alloc() {
+ //debug("alloc " + name + " " + list.length);
+ return list.length ? list.pop()
+ : constructor.apply(this, arguments);
+ },
-
-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);
- }
+ // Puts an object back in the object pool
+ free: function free(obj) {
+ //debug("free " + name + " " + list.length);
+ var length = list.length;
+ if (length < max) {
+ list[length] = obj;
+ }
+ }
+ };
};
diff --git a/lib/http.js b/lib/http.js
index 28e3e3e..e4f78cd 100644
--- a/lib/http.js
+++ b/lib/http.js
@@ -11,10 +11,10 @@ var sys = require('sys');
var net = require('net');
var events = require('events');
-var FreeList = require('freelist').FreeList;
+var FreeList = require('freelist');
var HTTPParser = process.binding('http_parser').HTTPParser;
-var parsers = new FreeList('parsers', 1000, function () {
+var parsers = FreeList('parsers', 1000, function () {
var parser = new HTTPParser('request');
parser.onMessageBegin = function () {
diff --git a/lib/net.js b/lib/net.js
index 72eb878..fcd74fc 100644
--- a/lib/net.js
+++ b/lib/net.js
@@ -22,7 +22,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 FreeList = require('freelist');
var IOWatcher = process.IOWatcher;
var assert = process.assert;
@@ -206,7 +206,7 @@ var timeout = new (function () {
};
})();
-var ioWatchers = new FreeList("iowatcher", 100, function () {
+var ioWatchers = FreeList("iowatcher", 100, function () {
return new IOWatcher();
});
--
1.7.0.4

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.