From 98c60babcb14a7251547c22eaa93c4a58ea1c7bf Mon Sep 17 00:00:00 2001 From: youben11 Date: Tue, 10 Dec 2024 09:52:44 +0100 Subject: [PATCH] perf(compiler): avoid big copy of server keys we inline the server key serialization so that we only make small copies --- .../compiler/lib/Common/Keysets.cpp | 23 +++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/compilers/concrete-compiler/compiler/lib/Common/Keysets.cpp b/compilers/concrete-compiler/compiler/lib/Common/Keysets.cpp index 1ff4be51c..8a5964ed2 100644 --- a/compilers/concrete-compiler/compiler/lib/Common/Keysets.cpp +++ b/compilers/concrete-compiler/compiler/lib/Common/Keysets.cpp @@ -164,9 +164,28 @@ Keyset Keyset::fromProto(concreteprotocol::Keyset::Reader reader) { Message Keyset::toProto() const { auto output = Message(); - auto serverProto = server.toProto(); + // we inlined call to server.toProto() to avoid a single big copy of the + // server keyset. With this, we only do copies of individual keys. + auto serverKeyset = output.asBuilder().initServer(); + serverKeyset.initLweBootstrapKeys(server.lweBootstrapKeys.size()); + for (size_t i = 0; i < server.lweBootstrapKeys.size(); i++) { + serverKeyset.getLweBootstrapKeys().setWithCaveats( + i, server.lweBootstrapKeys[i].toProto().asReader()); + } + + serverKeyset.initLweKeyswitchKeys(server.lweKeyswitchKeys.size()); + for (size_t i = 0; i < server.lweKeyswitchKeys.size(); i++) { + serverKeyset.getLweKeyswitchKeys().setWithCaveats( + i, server.lweKeyswitchKeys[i].toProto().asReader()); + } + + serverKeyset.initPackingKeyswitchKeys(server.packingKeyswitchKeys.size()); + for (size_t i = 0; i < server.packingKeyswitchKeys.size(); i++) { + serverKeyset.getPackingKeyswitchKeys().setWithCaveats( + i, server.packingKeyswitchKeys[i].toProto().asReader()); + } + // client serialization is not inlined as keys aren't that big auto clientProto = client.toProto(); - output.asBuilder().setServer(serverProto.asReader()); output.asBuilder().setClient(clientProto.asReader()); return output; }