From 525714a88bef5743016431c171cdc61bad45427e Mon Sep 17 00:00:00 2001 From: Mohamed Heikal Date: Thu, 18 Jul 2019 11:14:31 -0400 Subject: [PATCH] Precalculate the required number of characters for path shortening It does not make sense for aapt2 to calculate the optimal number of chars in the shortened path when it only depends on the number of resources rather than anything apk specific. This cl precalculates the number of chars and uses if conditions instead. Test: manual testing + aapt2_tests still passes. Change-Id: If2dc86952a6b6e01ce02dc42754c037d07d57325 --- tools/aapt2/optimize/ResourcePathShortener.cpp | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/tools/aapt2/optimize/ResourcePathShortener.cpp b/tools/aapt2/optimize/ResourcePathShortener.cpp index 845262bcb0d96..9009b71205a1f 100644 --- a/tools/aapt2/optimize/ResourcePathShortener.cpp +++ b/tools/aapt2/optimize/ResourcePathShortener.cpp @@ -16,7 +16,6 @@ #include "optimize/ResourcePathShortener.h" -#include #include #include "androidfw/StringPiece.h" @@ -50,18 +49,15 @@ std::string ShortenFileName(const android::StringPiece& file_path, int output_le } -// Calculate the optimal hash length such that an average of 10% of resources -// collide in their shortened path. +// Return the optimal hash length such that at most 10% of resources collide in +// their shortened path. // Reference: http://matt.might.net/articles/counting-hash-collisions/ int OptimalShortenedLength(int num_resources) { - int num_chars = 2; - double N = 64*64; // hash space when hash is 2 chars long - double max_collisions = num_resources * 0.1; - while (num_resources - N + N * pow((N - 1) / N, num_resources) > max_collisions) { - N *= 64; - num_chars++; + if (num_resources > 4000) { + return 3; + } else { + return 2; } - return num_chars; } std::string GetShortenedPath(const android::StringPiece& shortened_filename,