Skip to content

Commit

Permalink
Only allocate StubInvoker instances when necessary.
Browse files Browse the repository at this point in the history
  • Loading branch information
broneill committed Feb 6, 2025
1 parent f6214c4 commit 837644b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/main/java/org/cojen/dirmi/core/CoreSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -1137,7 +1137,7 @@ final Object objectFor(long id) throws IOException {
final Object objectFor(long id, long typeId) throws IOException {
try {
StubFactory factory = mStubFactories.get(typeId);
return mStubs.putAndSelectStub(factory.newStub(id, stubSupport()));
return mStubs.findStub(id, factory, this);
} catch (NoSuchObjectException e) {
e.remoteAddress(remoteAddress());
throw e;
Expand Down Expand Up @@ -1166,7 +1166,7 @@ final Object objectFor(long id, long typeId, RemoteInfo info) {
mStubFactoriesByClass.putIfAbsent(type, factory);
}

return mStubs.putAndSelectStub(factory.newStub(id, stubSupport()));
return mStubs.findStub(id, factory, this);
}

private void trySendCommandAndId(int command, long id) {
Expand Down
21 changes: 12 additions & 9 deletions src/main/java/org/cojen/dirmi/core/StubMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,21 @@
*/
final class StubMap extends ItemMap<StubInvoker> {
/**
* Returns the given invoker, an existing invoker, or a wrapper.
* Returns a new invoker, an existing invoker, or a wrapper.
*
* @param invoker must be a new instance
* @param factory used when a new invoker must be created
* @param session used when a new invoker must be created
*/
Stub putAndSelectStub(StubInvoker invoker) {
Stub findStub(long id, StubFactory factory, CoreSession session) {
Stub selected;

existing: {
synchronized (this) {
Item[] items = mItems;
int slot = ((int) invoker.id) & (items.length - 1);
int slot = ((int) id) & (items.length - 1);

for (Item existing = items[slot]; existing != null; existing = existing.mNext) {
if (existing.id == invoker.id) {
if (existing.id == id) {
selected = ((StubInvoker) existing).select();
if (selected != null) {
break existing;
Expand All @@ -45,14 +46,16 @@ Stub putAndSelectStub(StubInvoker invoker) {
}
}

// Must init before calling doPut because upon doing so the invoker can be
// obtained by other threads.
StubInvoker invoker = factory.newStub(id, session.stubSupport());

// Must init first because upon calling doPut the invoker can be obtained by
// other threads.
selected = invoker.init();

doPut(items, invoker, slot);

return selected;
}

return selected;
}

selected.invoker().incTransportCount();
Expand Down

0 comments on commit 837644b

Please sign in to comment.