Skip to content

Commit

Permalink
Bug 1297801 - part 1 - commonize the managee->array functions in IPDL…
Browse files Browse the repository at this point in the history
… generated code; r=billm

There's no reason to generate identical code for every kind of managed
protocol; we can have a single instance that operates on void* and cast
our way to victory.  This change saves ~60K of text on x86-64 Linux.
  • Loading branch information
froydnj committed Aug 30, 2016
1 parent 195c214 commit 3a05bed
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 18 deletions.
12 changes: 12 additions & 0 deletions ipc/glue/ProtocolUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -464,5 +464,17 @@ void ArrayLengthReadError(const char* aElementName)
NS_RUNTIMEABORT(message.get());
}

void
TableToArray(const nsTHashtable<nsPtrHashKey<void>>& aTable,
nsTArray<void*>& aArray)
{
uint32_t i = 0;
void** elements = aArray.AppendElements(aTable.Count());
for (auto iter = aTable.ConstIter(); !iter.Done(); iter.Next()) {
elements[i] = iter.Get()->GetKey();
++i;
}
}

} // namespace ipc
} // namespace mozilla
25 changes: 23 additions & 2 deletions ipc/glue/ProtocolUtils.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
* vim: sw=4 ts=4 et :
*/
/* This Source Code Form is subject to the terms of the Mozilla Public
Expand Down Expand Up @@ -604,10 +604,31 @@ CreateEndpoints(const PrivateIPDLInterface& aPrivate,
return NS_OK;
}

void
TableToArray(const nsTHashtable<nsPtrHashKey<void>>& aTable,
nsTArray<void*>& aArray);

} // namespace ipc

template<typename Protocol>
using ManagedContainer = nsTHashtable<nsPtrHashKey<Protocol>>;
class ManagedContainer : public nsTHashtable<nsPtrHashKey<Protocol>>
{
typedef nsTHashtable<nsPtrHashKey<Protocol>> BaseClass;

public:
// Having the core logic work on void pointers, rather than typed pointers,
// means that we can have one instance of this code out-of-line, rather
// than several hundred instances of this code out-of-lined. (Those
// repeated instances don't necessarily get folded together by the linker
// because they contain member offsets and such that differ between the
// functions.) We do have to pay for it with some eye-bleedingly bad casts,
// though.
void ToArray(nsTArray<Protocol*>& aArray) const {
::mozilla::ipc::TableToArray(*reinterpret_cast<const nsTHashtable<nsPtrHashKey<void>>*>
(static_cast<const BaseClass*>(this)),
reinterpret_cast<nsTArray<void*>&>(aArray));
}
};

template<typename Protocol>
Protocol*
Expand Down
20 changes: 4 additions & 16 deletions ipc/ipdl/ipdl/lower.py
Original file line number Diff line number Diff line change
Expand Up @@ -3086,22 +3086,10 @@ def forLoopOverHashtable(hashtable, itervar, const=False):
params=[ Decl(_cxxArrayType(p.managedCxxType(managed, self.side), ref=1),
arrvar.name) ],
const=1))
ivar = ExprVar('i')
elementsvar = ExprVar('elements')
itervar = ExprVar('iter')
meth.addstmt(StmtDecl(Decl(Type.UINT32, ivar.name),
init=ExprLiteral.ZERO))
meth.addstmt(StmtDecl(Decl(Type(_actorName(managed.name(), self.side), ptrptr=1), elementsvar.name),
init=ExprCall(ExprSelect(arrvar, '.', 'AppendElements'),
args=[ ExprCall(ExprSelect(p.managedVar(managed, self.side),
'.', 'Count')) ])))
foreachaccumulate = forLoopOverHashtable(p.managedVar(managed, self.side),
itervar, const=True)
foreachaccumulate.addstmt(StmtExpr(
ExprAssn(ExprIndex(elementsvar, ivar),
actorFromIter(itervar))))
foreachaccumulate.addstmt(StmtExpr(ExprPrefixUnop(ivar, '++')))
meth.addstmt(foreachaccumulate)
meth.addstmt(StmtExpr(
ExprCall(ExprSelect(p.managedVar(managed, self.side),
'.', 'ToArray'),
args=[ arrvar ])))

refmeth = MethodDefn(MethodDecl(
p.managedMethod(managed, self.side).name,
Expand Down

0 comments on commit 3a05bed

Please sign in to comment.