On Wed, Sep 08, 2021 at 03:05:41PM +0200, Claudio Jeker wrote:
> Looking at profiling information and the code made me realize that these
> recallocarray calls growing the array by one every time are unnecessary.
> The size of the array is known in advance so use that information and
> build it up ahead of time.
>
> In the roa case the IP list is double nested and so one still needs to
> resize the array but for mft the file-and-hash list is only defined once
> per MFT and so calloc can be used there.
>
> Works for me, the speed-up is not really noticable but I think this change
> is still worth it.
I'm ok with this.
Should we assert after the for loops that we added as many filehashes or
addresses as allocated? This is currently more obvious than after this
change.
> --
> :wq Claudio
>
> Index: roa.c
> ===================================================================
> RCS file: /cvs/src/usr.sbin/rpki-client/roa.c,v
> retrieving revision 1.23
> diff -u -p -r1.23 roa.c
> --- roa.c 1 Aug 2021 22:29:49 -0000 1.23
> +++ roa.c 7 Sep 2021 13:19:19 -0000
> @@ -109,10 +109,6 @@ roa_parse_addr(const ASN1_OCTET_STRING *
> }
> }
>
> - p->res->ips = recallocarray(p->res->ips, p->res->ipsz, p->res->ipsz + 1,
> - sizeof(struct roa_ip));
> - if (p->res->ips == NULL)
> - err(1, NULL);
> res = &p->res->ips[p->res->ipsz++];
>
> res->addr = addr;
> @@ -180,6 +176,12 @@ roa_parse_ipfam(const ASN1_OCTET_STRING
> "failed ASN.1 sequence parse", p->fn);
> goto out;
> }
> +
> + /* will be called multiple times so use recallocarray */
> + p->res->ips = recallocarray(p->res->ips, p->res->ipsz,
> + p->res->ipsz + sk_ASN1_TYPE_num(sseq), sizeof(struct roa_ip));
> + if (p->res->ips == NULL)
> + err(1, NULL);
>
> for (i = 0; i < sk_ASN1_TYPE_num(sseq); i++) {
> t = sk_ASN1_TYPE_value(sseq, i);
> Index: mft.c
> ===================================================================
> RCS file: /cvs/src/usr.sbin/rpki-client/mft.c,v
> retrieving revision 1.36
> diff -u -p -r1.36 mft.c
> --- mft.c 13 Jul 2021 18:39:39 -0000 1.36
> +++ mft.c 7 Sep 2021 12:59:57 -0000
> @@ -194,12 +194,6 @@ mft_parse_filehash(struct parse *p, cons
> }
>
> /* Insert the filename and hash value. */
> -
> - p->res->files = recallocarray(p->res->files, p->res->filesz,
> - p->res->filesz + 1, sizeof(struct mftfile));
> - if (p->res->files == NULL)
> - err(1, NULL);
> -
> fent = &p->res->files[p->res->filesz++];
>
> fent->file = fn;
> @@ -231,6 +225,10 @@ mft_parse_flist(struct parse *p, const A
> "failed ASN.1 sequence parse", p->fn);
> goto out;
> }
> +
> + p->res->files = calloc(sk_ASN1_TYPE_num(seq), sizeof(struct mftfile));
> + if (p->res->files == NULL)
> + err(1, NULL);
>
> for (i = 0; i < sk_ASN1_TYPE_num(seq); i++) {
> t = sk_ASN1_TYPE_value(seq, i);
>