Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
slab_allocator: remove SlabAllocator
Browse files Browse the repository at this point in the history
Now that Buffer instantiation has improved, the SlabAllocator is an
unnecessary layer of complexity preventing further performance
optimizations.

Currently there is a small performance loss with very small stream
requests, but this will soon be addressed.
  • Loading branch information
trevnorris committed Jul 3, 2013
1 parent c1db1ec commit ec90e6e
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 227 deletions.
2 changes: 0 additions & 2 deletions node.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@
'src/smalloc.cc',
'src/string_bytes.cc',
'src/stream_wrap.cc',
'src/slab_allocator.cc',
'src/tcp_wrap.cc',
'src/timer_wrap.cc',
'src/tty_wrap.cc',
Expand Down Expand Up @@ -140,7 +139,6 @@
'src/tcp_wrap.h',
'src/udp_wrap.h',
'src/req_wrap.h',
'src/slab_allocator.h',
'src/string_bytes.h',
'src/stream_wrap.h',
'src/tree.h',
Expand Down
127 changes: 0 additions & 127 deletions src/slab_allocator.cc

This file was deleted.

49 changes: 0 additions & 49 deletions src/slab_allocator.h

This file was deleted.

37 changes: 14 additions & 23 deletions src/stream_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
#include "node.h"
#include "node_buffer.h"
#include "handle_wrap.h"
#include "slab_allocator.h"
#include "stream_wrap.h"
#include "pipe_wrap.h"
#include "tcp_wrap.h"
Expand All @@ -33,8 +32,6 @@
#include <stdlib.h> // abort()
#include <limits.h> // INT_MAX

#define SLAB_SIZE (1024 * 1024)


namespace node {

Expand All @@ -49,6 +46,7 @@ using v8::Number;
using v8::Object;
using v8::Persistent;
using v8::String;
using v8::Uint32;
using v8::Value;


Expand All @@ -58,23 +56,13 @@ static Persistent<String> write_queue_size_sym;
static Persistent<String> onread_sym;
static Persistent<String> oncomplete_sym;
static Persistent<String> handle_sym;
static SlabAllocator* slab_allocator;
static bool initialized;


static void DeleteSlabAllocator(void*) {
delete slab_allocator;
slab_allocator = NULL;
}


void StreamWrap::Initialize(Handle<Object> target) {
if (initialized) return;
initialized = true;

slab_allocator = new SlabAllocator(SLAB_SIZE);
AtExit(DeleteSlabAllocator, NULL);

HandleScope scope(node_isolate);

HandleWrap::Initialize(target);
Expand Down Expand Up @@ -592,8 +580,7 @@ void StreamWrapCallbacks::AfterWrite(WriteWrap* w) {

uv_buf_t StreamWrapCallbacks::DoAlloc(uv_handle_t* handle,
size_t suggested_size) {
char* buf = slab_allocator->Allocate(wrap_->object_, suggested_size);
return uv_buf_init(buf, suggested_size);
return uv_buf_init(new char[suggested_size], suggested_size);
}


Expand All @@ -604,26 +591,30 @@ void StreamWrapCallbacks::DoRead(uv_stream_t* handle,
HandleScope scope(node_isolate);

if (nread < 0) {
// If libuv reports an error or EOF it *may* give us a buffer back. In that
// case, return the space to the slab.
if (buf.base != NULL)
slab_allocator->Shrink(Self(), buf.base, 0);
delete[] buf.base;

SetErrno(uv_last_error(uv_default_loop()));
MakeCallback(Self(), onread_sym, 0, NULL);
return;
}

Local<Object> slab = slab_allocator->Shrink(wrap_->object_, buf.base, nread);
if (nread == 0) {
if (buf.base != NULL)
delete[] buf.base;
return;
}

// TODO(trevnorris): not kosher to use new/delete w/ realloc
buf.base = static_cast<char*>(realloc(buf.base, nread));

if (nread == 0) return;
assert(static_cast<size_t>(nread) <= buf.len);

int argc = 3;
Local<Value> argv[4] = {
slab,
Integer::NewFromUnsigned(buf.base - Buffer::Data(slab), node_isolate),
Integer::NewFromUnsigned(nread, node_isolate)
Buffer::Use(buf.base, nread),
Uint32::New(0, node_isolate),
Uint32::New(nread, node_isolate)
};

Local<Object> pending_obj;
Expand Down
3 changes: 0 additions & 3 deletions src/stream_wrap.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,6 @@ class StreamWrap : public HandleWrap {
void UpdateWriteQueueSize();

private:
static inline char* NewSlab(v8::Handle<v8::Object> global, v8::Handle<v8::Object> wrap_obj);

// Callbacks for libuv
static void AfterWrite(uv_write_t* req, int status);
static uv_buf_t OnAlloc(uv_handle_t* handle, size_t suggested_size);
Expand All @@ -151,7 +149,6 @@ class StreamWrap : public HandleWrap {
template <enum encoding encoding>
static v8::Handle<v8::Value> WriteStringImpl(const v8::Arguments& args);

size_t slab_offset_;
uv_stream_t* stream_;

StreamWrapCallbacks default_callbacks_;
Expand Down
Loading

0 comments on commit ec90e6e

Please sign in to comment.