8000 [WIP] Cache repository / owner values in persistent handle by srajko · Pull Request #962 · nodegit/nodegit · GitHub
[go: up one dir, main page]

Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions generate/input/descriptor.json
Original file line number Diff line number Diff line change
Expand Up @@ -1767,6 +1767,7 @@
"ignore": true
},
"repository": {
"cacheResult": true,
"functions": {
"git_repository__cleanup": {
"ignore": true
Expand Down
1 change: 1 addition & 0 deletions generate/scripts/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ var Helpers = {
typeDef.filename = typeDef.typeName;
typeDef.isLibgitType = true;
typeDef.dependencies = [];
typeDef.cacheResult = Boolean(typeDefOverrides.cacheResult);
typeDef.selfFreeing = Boolean(typeDefOverrides.selfFreeing);

if (typeDefOverrides.freeFunctionName) {
Expand Down
41 changes: 40 additions & 1 deletion generate/templates/partials/sync_function.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
{%partial doc .%}
NAN_METHOD({{ cppClassName }}::{{ cppFunctionName }}) {
Nan::EscapableHandleScope scope;

{%if return.cacheResult %}
{{ cppClassName }} *thisObj = Nan::ObjectWrap::Unwrap<{{ cppClassName }}>(info.This());
return info.GetReturnValue().Set(scope.Escape(Nan::New(thisObj->{{ cppFunctionName }}_cachedResult)));
{% else %}
{%partial guardArguments .%}

{%each .|returnsInfo 'true' as _return %}
Expand Down Expand Up @@ -35,7 +40,7 @@ if (Nan::ObjectWrap::Unwrap<{{ cppClassName }}>(info.This())->GetValue() != NULL
{% endif %}

giterr_clear();

{
LockMaster lockMaster(true{%each args|argsInfo as arg %}
{%if arg.cType|isPointer%}{%if not arg.isReturn%}
Expand Down Expand Up @@ -125,4 +130,38 @@ if (Nan::ObjectWrap::Unwrap<{{ cppClassName }}>(info.This())->GetValue() != NULL
{%endif%}
{%endif%}
}
{%endif%}
}

{%if return.cacheResult %}
void {{ cppClassName }}::{{ cppFunctionName }}_cache() {
if (!raw) {
{{ cppFunctionName }}_cachedResult.Reset(Nan::Null());
return;
}

LockMaster lockMaster(true{%each args|argsInfo as arg %}
{%if arg.cType|isPointer%}{%if not arg.isReturn%}
,{%if arg.isSelf %}
raw
{%endif%}
{%endif%}{%endif%}
{%endeach%});

{{ return.cType }} result = {{ cFunctionName }}(
{%each args|argsInfo as arg %}
{%if arg.isSelf %}
raw
{%endif%}
{%endeach%}
);

Local<v8::Value> to;

{%each .|returnsInfo as _return %}
{%partial convertToV8 _return %}
{%endeach%}

{{ cppFunctionName }}_cachedResult.Reset(to);
}
{% endif %}
10 changes: 10 additions & 10BC0 amp; 0 deletions generate/templates/templates/class_content.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ using namespace node;
NonSelfFreeingConstructedCount++;
}


{%each functions as function%}
{%if not function.ignore %}
{%if not function.isAsync %}
{%if function.return.cacheResult %}
{{ function.cppFunctionName }}_cache(); // populate cached value
{%endif%}
{%endif%}
{%endif%}
{%endeach%}
}

{{ cppClassName }}::~{{ cppClassName }}() {
Expand Down
8 changes: 8 additions & 0 deletions generate/templates/templates/class_header.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,14 @@ class {{ cppClassName }} : public Nan::ObjectWrap {
private:
{{ function.cppFunctionName }}Baton *baton;
};
{%else%}
{%if function.return.cacheResult %}
// For simple sync functions that return a wrapped object and pass `raw`
// as the the only parameter to libgit2, we cache the results.
// CopyablePersistentTraits are used to get the reset-on-destruct behavior.
void {{ function.cppFunctionName }}_cache();
Nan::Persistent<Value, Nan::CopyablePersistentTraits<Value> > {{ function.cppFunctionName }}_cachedResult;
{%endif%}
{%endif%}

static NAN_METHOD({{ function.cppFunctionName }});
Expand Down
6 changes: 6 additions & 0 deletions test/tests/commit.js
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,12 @@ describe("Commit", function() {
assert.ok(owner instanceof Repository);
});

it("caches its owner", function() {
var owner = this.commit.owner();
var ownerAgain = this.commit.owner();
assert.ok(owner === ownerAgain);
});

it("can walk its repository's history", function(done) {
var historyCount = 0;
var expectedHistoryCount = 364;
Expand Down
2 changes: 1 addition & 1 deletion test/tests/submodule.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ describe("Submodule", function() {
return reference.peel(NodeGit.Object.TYPE.COMMIT);
})
.then(function(commit) {
return submoduleRepo.createBranch("master", commit);
return submoduleRepo.createBranch("master", commit.id());
})
.then(function() {
return submodule.addFinalize();
Expand Down
0