Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

net: remove unconditional getpeername() call

Speeds up http_simple benchmark by about 1.0%
  • Loading branch information...
bnoordhuis committed Oct 6, 2011
1 parent 065c6f4 commit 38dce40eca7c7d5b44f29f967633004bcca4f7a4
Showing with 21 additions and 18 deletions.
  1. +21 −18 lib/net_uv.js
@@ -339,6 +339,27 @@ Socket.prototype.setEncoding = function(encoding) {
};


Socket.prototype._getpeername = function() {
if (!this._handle || !this._handle.getpeername) {
return {};
}
if (!this._peername) {
this._peername = this._handle.getpeername();
}
return this._peername;
};


Socket.prototype.__defineGetter__('remoteAddress', function() {
return this._getpeername().address;
});


Socket.prototype.__defineGetter__('remotePort', function() {
return this._getpeername().port;
});


Socket.prototype.write = function(data /* [encoding], [fd], [cb] */) {
var encoding, fd, cb;

@@ -690,7 +711,6 @@ Server.prototype.address = function() {
function onconnection(clientHandle) {
var handle = this;
var self = handle.socket;
var peername;

debug('onconnection');

@@ -704,29 +724,12 @@ function onconnection(clientHandle) {
return;
}

// Todo: implement this for unix sockets
if (clientHandle.getpeername) {
peername = clientHandle.getpeername();
if (!peername.address || !peername.port) {
var err = errnoException(errno, 'accept');
clientHandle.close();
self.emit('error', err);
return;
}
}

var socket = new Socket({
handle: clientHandle,
allowHalfOpen: self.allowHalfOpen
});
socket.readable = socket.writable = true;

if (peername) {
socket.remoteAddress = peername.address;
socket.remotePort = peername.port;
// TODO: set family as well
}

socket.resume();

self.connections++;

0 comments on commit 38dce40

Please sign in to comment.
You can’t perform that action at this time.