Skip to content

Commit 74954ce

Browse files
thughesry
authored andcommitted
Add string class that uses ExternalAsciiStringResource.
Change the natives to use this class instead of creating completely new strings. Reduces memory usage by about 1 MB.
1 parent 81d3de7 commit 74954ce

8 files changed

+74
-13
lines changed

cmake/node_build.cmake

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ set(node_sources
6262
src/node_script.cc
6363
src/node_os.cc
6464
src/node_dtrace.cc
65+
src/node_string.cc
6566
src/node_natives.h
6667
${node_extra_src})
6768

src/node.cc

+4-3
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
#include <node_stdio.h>
4949
#include <node_javascript.h>
5050
#include <node_version.h>
51+
#include <node_string.h>
5152
#ifdef HAVE_OPENSSL
5253
# include <node_crypto.h>
5354
#endif
@@ -1269,7 +1270,7 @@ static void ReportException(TryCatch &try_catch, bool show_line) {
12691270
}
12701271

12711272
// Executes a str within the current v8 context.
1272-
Local<Value> ExecuteString(Local<String> source, Local<Value> filename) {
1273+
Local<Value> ExecuteString(Handle<String> source, Handle<Value> filename) {
12731274
HandleScope scope;
12741275
TryCatch try_catch;
12751276

@@ -2036,8 +2037,8 @@ static void Load(int argc, char *argv[]) {
20362037

20372038
TryCatch try_catch;
20382039

2039-
Local<Value> f_value = ExecuteString(String::New(MainSource()),
2040-
String::New("node.js"));
2040+
Local<Value> f_value = ExecuteString(MainSource(),
2041+
IMMUTABLE_STRING("node.js"));
20412042
if (try_catch.HasCaught()) {
20422043
ReportException(try_catch, true);
20432044
exit(10);

src/node_javascript.cc

+4-7
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
#include <v8.h>
22
#include "node.h"
33
#include "node_natives.h"
4+
#include "node_string.h"
45
#include <string.h>
56
#include <strings.h>
67

78
using namespace v8;
89

910
namespace node {
1011

11-
const char* MainSource() {
12-
return node_native;
12+
Handle<String> MainSource() {
13+
return BUILTIN_ASCII_ARRAY(node_native, sizeof(node_native)-1);
1314
}
1415

1516
void DefineJavaScript(v8::Handle<v8::Object> target) {
@@ -18,14 +19,10 @@ void DefineJavaScript(v8::Handle<v8::Object> target) {
1819
for (int i = 0; natives[i].name; i++) {
1920
if (natives[i].source != node_native) {
2021
Local<String> name = String::New(natives[i].name);
21-
// TODO: Use ExternalAsciiStringResource for source
22-
// Might need to do some assertions in js2c about chars > 128
23-
Local<String> source = String::New(natives[i].source);
22+
Handle<String> source = BUILTIN_ASCII_ARRAY(natives[i].source, natives[i].source_len);
2423
target->Set(name, source);
2524
}
2625
}
2726
}
2827

29-
30-
3128
} // namespace node

src/node_javascript.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
namespace node {
44

55
void DefineJavaScript(v8::Handle<v8::Object> target);
6-
const char* MainSource();
6+
v8::Handle<v8::String> MainSource();
77

88
} // namespace node

src/node_string.cc

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include "node_string.h"
2+
3+
namespace node {
4+
5+
using namespace v8;
6+
7+
Handle<String> ImmutableAsciiSource::CreateFromLiteral(
8+
const char *string_literal,
9+
size_t length) {
10+
HandleScope scope;
11+
12+
Local<String> ret = String::NewExternal(new ImmutableAsciiSource(
13+
string_literal,
14+
length));
15+
return scope.Close(ret);
16+
}
17+
18+
}

src/node_string.h

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#ifndef SRC_NODE_STRING_H_
2+
#define SRC_NODE_STRING_H_
3+
4+
#include <v8.h>
5+
6+
namespace node {
7+
8+
#define IMMUTABLE_STRING(string_literal) \
9+
::node::ImmutableAsciiSource::CreateFromLiteral( \
10+
string_literal "", sizeof(string_literal) - 1)
11+
#define BUILTIN_ASCII_ARRAY(array, len) \
12+
::node::ImmutableAsciiSource::CreateFromLiteral(array, len)
13+
14+
class ImmutableAsciiSource : public v8::String::ExternalAsciiStringResource {
15+
public:
16+
static v8::Handle<v8::String> CreateFromLiteral(const char *string_literal,
17+
size_t length);
18+
19+
ImmutableAsciiSource(const char *src, size_t src_len)
20+
: buffer_(src),
21+
buf_len_(src_len) {
22+
}
23+
24+
~ImmutableAsciiSource() {
25+
}
26+
27+
const char *data() const {
28+
return buffer_;
29+
}
30+
31+
size_t length() const {
32+
return buf_len_;
33+
}
34+
35+
private:
36+
const char *buffer_;
37+
size_t buf_len_;
38+
};
39+
40+
} // namespace node
41+
42+
#endif // SRC_NODE_STRING_H_

tools/js2c.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def ToCArray(filename, lines):
5353

5454
value = ord(chr)
5555

56-
if value > 128:
56+
if value >= 128:
5757
print 'non-ascii value ' + filename + ':' + str(row) + ':' + str(col)
5858
sys.exit(1);
5959

@@ -220,6 +220,7 @@ def ReadMacros(lines):
220220
struct _native {
221221
const char* name;
222222
const char* source;
223+
size_t source_len;
223224
};
224225
225226
static const struct _native natives[] = {
@@ -236,7 +237,7 @@ def ReadMacros(lines):
236237

237238

238239
NATIVE_DECLARATION = """\
239-
{ "%(id)s", %(id)s_native },
240+
{ "%(id)s", %(id)s_native, sizeof(%(id)s_native)-1 },
240241
"""
241242

242243
SOURCE_DECLARATION = """\

wscript

+1
Original file line numberDiff line numberDiff line change
@@ -801,6 +801,7 @@ def build(bld):
801801
src/node_script.cc
802802
src/node_os.cc
803803
src/node_dtrace.cc
804+
src/node_string.cc
804805
"""
805806

806807
if sys.platform.startswith("win32"):

0 commit comments

Comments
 (0)