with great performance hit, a patch to handle binary

(need to get the v8 people to produce something that doesn't require copying
buffers 20 times.)
This commit is contained in:
Ryan 2009-03-09 17:26:42 +01:00
Родитель a8ac42384d
Коммит 7b45c911ef
2 изменённых файлов: 33 добавлений и 8 удалений

21
node.cc
Просмотреть файл

@ -20,6 +20,7 @@ static int exit_code = 0;
static Handle<String> static Handle<String>
ReadFile (const string& name) ReadFile (const string& name)
{ {
FILE* file = fopen(name.c_str(), "rb"); FILE* file = fopen(name.c_str(), "rb");
if (file == NULL) return Handle<String>(); if (file == NULL) return Handle<String>();
@ -27,16 +28,27 @@ ReadFile (const string& name)
int size = ftell(file); int size = ftell(file);
rewind(file); rewind(file);
char* chars = new char[size + 1]; char chars[size+1];
chars[size] = '\0'; chars[size] = '\0';
for (int i = 0; i < size;) { for (int i = 0; i < size;) {
int read = fread(&chars[i], 1, size - i, file); int read = fread(&chars[i], 1, size - i, file);
if(read <= 0) {
perror("read()");
}
i += read; i += read;
} }
uint16_t expanded_base[size+1];
expanded_base[size] = '\0';
for(int i = 0; i < size; i++)
expanded_base[i] = chars[i];
fclose(file); fclose(file);
Handle<String> result = String::New(chars, size);
delete[] chars; HandleScope scope;
return result; Local<String> result = String::New(expanded_base, size);
return scope.Close(result);
} }
static Handle<Value> static Handle<Value>
@ -61,7 +73,6 @@ BlockingFileRead (const Arguments& args)
HandleScope scope; HandleScope scope;
String::Utf8Value filename(args[0]); String::Utf8Value filename(args[0]);
Handle<String> output = ReadFile (*filename); Handle<String> output = ReadFile (*filename);
return scope.Close(output); return scope.Close(output);
} }

Просмотреть файл

@ -160,7 +160,14 @@ HttpRequest::Respond (Handle<Value> data)
} else { } else {
Handle<String> s = data->ToString(); Handle<String> s = data->ToString();
oi_buf *buf = oi_buf_new2(s->Length()); oi_buf *buf = oi_buf_new2(s->Length());
s->WriteAscii(buf->base, 0, s->Length());
uint16_t expanded[s->Length()];
s->Write(expanded, 0, s->Length());
for(int i = 0; i < s->Length(); i++) {
buf->base[i] = expanded[i];
}
output.push_back(buf); output.push_back(buf);
} }
connection.Write(); connection.Write();
@ -349,7 +356,14 @@ HttpRequest::MakeBodyCallback (const char *base, size_t length)
if(length) { if(length) {
// TODO ByteArray? // TODO ByteArray?
Handle<String> chunk = String::New(base, length); //
uint16_t expanded_base[length];
for(int i = 0; i < length; i++) {
expanded_base[i] = base[i];
}
Handle<String> chunk = String::New(expanded_base, length);
argv[0] = chunk; argv[0] = chunk;
} else { } else {
argv[0] = Null(); argv[0] = Null();