From 1bdef34e5463295bf5f77ae0bfd4cb9d10ce3b05 Mon Sep 17 00:00:00 2001 From: yevgenypi Date: Thu, 30 Jun 2022 21:03:09 +0200 Subject: [PATCH] small performance improve (#37) --- go-bindings/threshold.cpp | 1 + go-bindings/utils.cpp | 2 ++ go-bindings/utils.hpp | 1 + src/threshold.cpp | 8 +++----- 4 files changed, 7 insertions(+), 5 deletions(-) diff --git a/go-bindings/threshold.cpp b/go-bindings/threshold.cpp index 17960bcb978e62..834e428c3e9d96 100644 --- a/go-bindings/threshold.cpp +++ b/go-bindings/threshold.cpp @@ -24,6 +24,7 @@ std::vector toVectorHashes(void** elems, const size_t len) { std::vector vec; + vec.reserve(len); for (int i = 0 ; i < len; ++i) { vec.push_back( bls::Bytes((uint8_t*)elems[i], HashSize) diff --git a/go-bindings/utils.cpp b/go-bindings/utils.cpp index 0ba85f16dcec08..f7a1ea90beeaba 100644 --- a/go-bindings/utils.cpp +++ b/go-bindings/utils.cpp @@ -21,6 +21,7 @@ template std::vector toBLSVector(void** elems, const size_t len) { std::vector vec; + vec.reserve(len); for (int i = 0 ; i < len; ++i) { T* el = (T*)elems[i]; vec.push_back(*el); @@ -30,6 +31,7 @@ std::vector toBLSVector(void** elems, const size_t len) { std::vector toVectorBytes(void** elems, const size_t len, const std::vector vecElemsLens) { std::vector vec; + vec.reserve(len); for (int i = 0 ; i < len; ++i) { uint8_t* elPtr = (uint8_t*)elems[i]; vec.push_back(bls::Bytes(elPtr, vecElemsLens[i])); diff --git a/go-bindings/utils.hpp b/go-bindings/utils.hpp index d3658caeccc017..0ab817d02f5b45 100644 --- a/go-bindings/utils.hpp +++ b/go-bindings/utils.hpp @@ -23,6 +23,7 @@ template std::vector toBLSVector(void** elems, const size_t len) { std::vector vec; + vec.reserve(len); for (int i = 0 ; i < len; ++i) { const T* el = (T*)elems[i]; vec.push_back(*el); diff --git a/src/threshold.cpp b/src/threshold.cpp index 6ed2d5c0ea92e8..5f43ac69f8c655 100644 --- a/src/threshold.cpp +++ b/src/threshold.cpp @@ -164,8 +164,7 @@ namespace bls { BLSType Poly::Evaluate(const std::vector& vecIn, const Bytes& id) { typedef PolyOps Ops; Ops ops; - std::vector vec = vecIn; - if (vec.size() < 2) { + if (vecIn.size() < 2) { throw std::length_error("At least 2 coefficients required"); } @@ -174,9 +173,8 @@ namespace bls { bn_read_bin(x, id.begin(), Poly::nIdSize); ops.ModOrder(x); - BLSType y = vecIn[vec.size() - 1]; - - for (int i = (int) vec.size() - 2; i >= 0; i--) { + BLSType y = vecIn.back(); + for (int i = (int) vecIn.size() - 2; i >= 0; i--) { y = ops.Mul(y, x); y = ops.Add(y, vecIn[i]); }