From df4608dd598f2ae22e67e43e49b6f5908c8228b7 Mon Sep 17 00:00:00 2001 From: Michael Ow Date: Tue, 5 Oct 2010 04:52:16 +0000 Subject: [PATCH] ICU-8015 Improve performance of pkgdata tool file parser. X-SVN-Rev: 28763 --- icu4c/source/tools/pkgdata/pkgdata.cpp | 5 +++-- icu4c/source/tools/toolutil/flagparser.c | 26 ++++++++++++++++++------ icu4c/source/tools/toolutil/flagparser.h | 4 ++-- 3 files changed, 25 insertions(+), 10 deletions(-) diff --git a/icu4c/source/tools/pkgdata/pkgdata.cpp b/icu4c/source/tools/pkgdata/pkgdata.cpp index a0e74830a47..857169bcde4 100644 --- a/icu4c/source/tools/pkgdata/pkgdata.cpp +++ b/icu4c/source/tools/pkgdata/pkgdata.cpp @@ -752,6 +752,7 @@ static int32_t initializePkgDataFlags(UPKGOptions *o) { UErrorCode status = U_ZERO_ERROR; int32_t result = 0; int32_t currentBufferSize = SMALL_BUFFER_MAX_SIZE; + int32_t tmpResult = 0; /* Initialize pkgdataFlags */ pkgDataFlags = (char**)uprv_malloc(sizeof(char*) * PKGDATA_FLAGS_SIZE); @@ -785,12 +786,12 @@ static int32_t initializePkgDataFlags(UPKGOptions *o) { fprintf(stdout, "# Reading options file %s\n", o->options); } status = U_ZERO_ERROR; - parseFlagsFile(o->options, pkgDataFlags, currentBufferSize, (int32_t)PKGDATA_FLAGS_SIZE, &status); + tmpResult = parseFlagsFile(o->options, pkgDataFlags, currentBufferSize, (int32_t)PKGDATA_FLAGS_SIZE, &status); if (status == U_BUFFER_OVERFLOW_ERROR) { for (int32_t i = 0; i < PKGDATA_FLAGS_SIZE; i++) { uprv_free(pkgDataFlags[i]); } - currentBufferSize *= 2; + currentBufferSize = tmpResult; } else if (U_FAILURE(status)) { fprintf(stderr,"Unable to open or read \"%s\" option file. status = %s\n", o->options, u_errorName(status)); return -1; diff --git a/icu4c/source/tools/toolutil/flagparser.c b/icu4c/source/tools/toolutil/flagparser.c index 507f2cefadb..718a2551602 100644 --- a/icu4c/source/tools/toolutil/flagparser.c +++ b/icu4c/source/tools/toolutil/flagparser.c @@ -9,7 +9,9 @@ #include "cstring.h" #include "cmemory.h" -#define BUFFER_DEFAULT_SIZE 512 +#define DEFAULT_BUFFER_SIZE 512 + +static int32_t currentBufferSize = DEFAULT_BUFFER_SIZE; static void extractFlag(char* buffer, int32_t bufferSize, char* flag, int32_t flagSize, UErrorCode *status); static int32_t getFlagOffset(const char *buffer, int32_t bufferSize); @@ -17,22 +19,22 @@ static int32_t getFlagOffset(const char *buffer, int32_t bufferSize); /* * Opens the given fileName and reads in the information storing the data in flagBuffer. */ -U_CAPI void U_EXPORT2 +U_CAPI int32_t U_EXPORT2 parseFlagsFile(const char *fileName, char **flagBuffer, int32_t flagBufferSize, int32_t numOfFlags, UErrorCode *status) { - int32_t currentBufferSize = BUFFER_DEFAULT_SIZE; char* buffer = uprv_malloc(sizeof(char) * currentBufferSize); UBool allocateMoreSpace = FALSE; int32_t i; + int32_t result = 0; FileStream *f = T_FileStream_open(fileName, "r"); if (f == NULL) { *status = U_FILE_ACCESS_ERROR; - return; + return -1; } if (buffer == NULL) { *status = U_MEMORY_ALLOCATION_ERROR; - return; + return -1; } do { @@ -58,16 +60,28 @@ parseFlagsFile(const char *fileName, char **flagBuffer, int32_t flagBufferSize, T_FileStream_rewind(f); break; } else { - extractFlag(buffer, currentBufferSize, flagBuffer[i], flagBufferSize, status); if (U_FAILURE(*status)) { + if (*status == U_BUFFER_OVERFLOW_ERROR) { + result = currentBufferSize; + } else { + result = -1; + } break; } } } } while (allocateMoreSpace && U_SUCCESS(*status)); + uprv_free(buffer); + T_FileStream_close(f); + + if (U_SUCCESS(*status) && result == 0) { + currentBufferSize = DEFAULT_BUFFER_SIZE; + } + + return result; } diff --git a/icu4c/source/tools/toolutil/flagparser.h b/icu4c/source/tools/toolutil/flagparser.h index e31c7cfc3db..64056ed7a79 100644 --- a/icu4c/source/tools/toolutil/flagparser.h +++ b/icu4c/source/tools/toolutil/flagparser.h @@ -1,7 +1,7 @@ /* ******************************************************************************* * -* Copyright (C) 2009, International Business Machines +* Copyright (C) 2009-2010, International Business Machines * Corporation and others. All Rights Reserved. * ******************************************************************************* @@ -24,7 +24,7 @@ #include "unicode/utypes.h" -U_CAPI void U_EXPORT2 +U_CAPI int32_t U_EXPORT2 parseFlagsFile(const char *fileName, char **flagBuffer, int32_t flagBufferSize, int32_t numOfFlags, UErrorCode *status); #endif