mirror of https://github.com/lukechilds/node.git
Browse Source
Change the natives to use this class instead of creating completely new strings. Reduces memory usage by about 1 MB.v0.7.4-release
Tom Hughes
14 years ago
committed by
Ryan Dahl
8 changed files with 74 additions and 13 deletions
@ -0,0 +1,18 @@ |
|||
#include "node_string.h" |
|||
|
|||
namespace node { |
|||
|
|||
using namespace v8; |
|||
|
|||
Handle<String> ImmutableAsciiSource::CreateFromLiteral( |
|||
const char *string_literal, |
|||
size_t length) { |
|||
HandleScope scope; |
|||
|
|||
Local<String> ret = String::NewExternal(new ImmutableAsciiSource( |
|||
string_literal, |
|||
length)); |
|||
return scope.Close(ret); |
|||
} |
|||
|
|||
} |
@ -0,0 +1,42 @@ |
|||
#ifndef SRC_NODE_STRING_H_ |
|||
#define SRC_NODE_STRING_H_ |
|||
|
|||
#include <v8.h> |
|||
|
|||
namespace node { |
|||
|
|||
#define IMMUTABLE_STRING(string_literal) \ |
|||
::node::ImmutableAsciiSource::CreateFromLiteral( \ |
|||
string_literal "", sizeof(string_literal) - 1) |
|||
#define BUILTIN_ASCII_ARRAY(array, len) \ |
|||
::node::ImmutableAsciiSource::CreateFromLiteral(array, len) |
|||
|
|||
class ImmutableAsciiSource : public v8::String::ExternalAsciiStringResource { |
|||
public: |
|||
static v8::Handle<v8::String> CreateFromLiteral(const char *string_literal, |
|||
size_t length); |
|||
|
|||
ImmutableAsciiSource(const char *src, size_t src_len) |
|||
: buffer_(src), |
|||
buf_len_(src_len) { |
|||
} |
|||
|
|||
~ImmutableAsciiSource() { |
|||
} |
|||
|
|||
const char *data() const { |
|||
return buffer_; |
|||
} |
|||
|
|||
size_t length() const { |
|||
return buf_len_; |
|||
} |
|||
|
|||
private: |
|||
const char *buffer_; |
|||
size_t buf_len_; |
|||
}; |
|||
|
|||
} // namespace node
|
|||
|
|||
#endif // SRC_NODE_STRING_H_
|
Loading…
Reference in new issue