Skip to content

Instantly share code, notes, and snippets.

@ThisIsMissEm
Created April 13, 2010 16:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ThisIsMissEm/550269276be5e840eb52 to your computer and use it in GitHub Desktop.
Save ThisIsMissEm/550269276be5e840eb52 to your computer and use it in GitHub Desktop.
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment