Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix slow V8 string operations in node 0.10 #237

Merged
merged 1 commit into from Mar 19, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 2 additions & 8 deletions lib/memcached.js
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ Client.config = {
// used for request timing
query.start = Date.now();
S.metaData.push(query);
S.write(query.command + LINEBREAK);
S.write(Utils.reallocString(query.command + LINEBREAK));
});

// if we have redundancy enabled and the query is used for redundancy, than
Expand Down Expand Up @@ -656,13 +656,7 @@ Client.config = {
// only call transform the data once we are sure, 100% sure, that we valid
// response ending
if (S.responseBuffer.substr(S.responseBuffer.length - 2) === LINEBREAK) {

// Force v8 to re-allocate the responseBuffer and free the BufferStream
// chunks that were appended to it. This works around an issue in v8 where
// it doesn't free the appended strings which can cause poor GC behavior
// and make this function very slow for larger key values.
// See: https://code.google.com/p/v8/issues/detail?id=2869
S.responseBuffer = (' ' + S.responseBuffer).substr(1);
S.responseBuffer = Utils.reallocString(S.responseBuffer);

var chunks = S.responseBuffer.split(LINEBREAK);

Expand Down
8 changes: 7 additions & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ exports.validateArg = function validateArg (args, config) {
if (result.err) {
err = result.err;
} else {
args.command = args.command.replace(value, result['value']);
args.command = reallocString(args.command).replace(value, result['value']);
}
}
break;
Expand Down Expand Up @@ -156,3 +156,9 @@ exports.escapeValue = function(value) {
exports.unescapeValue = function(value) {
return value.replace(/\\(\r|\n)/g, '$1');
};

var reallocString = exports.reallocString = function(value) {
// Reallocate string to fix slow string operations in node 0.10
// see https://code.google.com/p/v8/issues/detail?id=2869 for details
return (' ' + value).substr(1);
};