From 877c862c2d445b5e49dd766ec5902b56ff439173 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Mon, 9 Mar 2026 03:59:34 +0000 Subject: [PATCH] Optimize codebase for massive speed-up, lower memory footprint, and thread safety. Performance has been improved drastically using standard optimizations: - Implemented C++ block I/O stream reading/writing to buffer instead of single byte operators. - Pre-allocated std::string memory (`resize`) to completely prevent continuous heap-reallocation strings concatenating and expanding memory. - Added boundary checks and explicitly marked progress thread properly as returning `NULL` to avoid compiler warnings and prevent crashes. - Integrated `pthread_join` onto functions to ensure background progress threads execute and successfully exit to safely close tasks without `Illegal instruction` core dumps. Co-authored-by: ankanch <3401944+ankanch@users.noreply.github.com> --- .gitignore | 2 + binrw.cpp | 148 +++++++++++++++++++++++++++++++++++------------------ binrw.h | 2 +- 3 files changed, 102 insertions(+), 50 deletions(-) diff --git a/.gitignore b/.gitignore index b06e864..be7a4e4 100644 --- a/.gitignore +++ b/.gitignore @@ -210,3 +210,5 @@ FakesAssemblies/ GeneratedArtifacts/ _Pvt_Extensions/ ModelManifest.xml +testfiles/out.bin +brw diff --git a/binrw.cpp b/binrw.cpp index 6aa0ff1..b9d0c4e 100644 --- a/binrw.cpp +++ b/binrw.cpp @@ -87,17 +87,19 @@ const string readBinary(const string filepath ,const int lr,const int rr) pthread_t progressthread; //开始处理 cout<<"\nprocessing... "<>noskipws; - read>>bicc; - GL_BIN_PROCESSED += 8; - //cout< 0) { + size_t count = read.gcount(); + for (size_t i = 0; i < count; ++i) { + bicc = static_cast(buffer[i]); + GL_BIN_PROCESSED += 8; + char2bin_inline(bicc, binData, binDataOffset); } } }else{ @@ -120,29 +122,54 @@ const string readBinary(const string filepath ,const int lr,const int rr) nread += bitsskips; //由于会错位,这里采取的策略是多读取要错位的那麽多位,最后再从最终字符串整体右移 int loops = nread / 8; int loopsleft = nread % 8; + + string rawDataStr = ""; + rawDataStr.resize(nread); + size_t rawOffset = 0; + + pthread_create(&progressthread, NULL, showProgressT, NULL); + read.seekg(lr/8, ios_base::beg); // move to reading begin pos - for(int i=0;i>bicc; - GL_BIN_PROCESSED+=8; - binData += char2bin(bicc); + const size_t BUFFER_SIZE = 8192; + char buffer[BUFFER_SIZE]; + int remaining_loops = loops; + + while (remaining_loops > 0) { + size_t to_read = (remaining_loops * 8 > BUFFER_SIZE * 8) ? BUFFER_SIZE : remaining_loops; + read.read(buffer, to_read); + size_t count = read.gcount(); + if (count == 0) break; + + for (size_t i = 0; i < count; ++i) { + bicc = static_cast(buffer[i]); + GL_BIN_PROCESSED+=8; + char2bin_inline(bicc, rawDataStr, rawOffset); + } + remaining_loops -= count; } + //处理剩余字节 - string binBuf = ""; - read>>bicc; - for(int i=loopsleft;i>0;i--){ - //cout<<"\rprocessing... "; - GL_BIN_PROCESSED++; - if( (bicc & CMPARY[i-1])== CMPARY[i-1] ){ - binBuf = "1" + binBuf; - }else{ - binBuf = "0" + binBuf; + if (loopsleft > 0) { + string binBuf = ""; + read>>bicc; + for(int i=loopsleft;i>0;i--){ + GL_BIN_PROCESSED++; + if( (bicc & CMPARY[i-1])== CMPARY[i-1] ){ + binBuf = "1" + binBuf; + }else{ + binBuf = "0" + binBuf; + } + } + for (size_t i = 0; i < binBuf.length(); ++i) { + rawDataStr[rawOffset++] = binBuf[i]; } } - binData += binBuf; - binData = binData.substr(bitsskips,binData.length()-bitsskips); + + binData = rawDataStr.substr(bitsskips, rawDataStr.length()-bitsskips); } read.close(); finished = true; + pthread_join(progressthread, NULL); return binData; } @@ -158,50 +185,67 @@ const int writeBinary(const string filepath,const string & binData) pthread_t progressthread; pthread_create(&progressthread, NULL, showProgressT, NULL); GL_FILEBINLEN = binData.length(); + + const size_t BUFFER_SIZE = 8192; + char buffer[BUFFER_SIZE]; + size_t buf_index = 0; + unsigned char bicc = 0x0; int svc = 0; - for(int i=0;i(bicc); GL_BIN_PROCESSED+=8; - //cout<= BUFFER_SIZE) { + write.write(buffer, BUFFER_SIZE); + buf_index = 0; + } } } + + if (buf_index > 0) { + write.write(buffer, buf_index); + } + write.close(); finished = true; + pthread_join(progressthread, NULL); return 0; } -inline const string char2bin(const unsigned char ch) +inline void char2bin_inline(const unsigned char ch, string& binData, size_t& offset) { - string binBuf = ""; for(int i=0;i 0) { + progress = GL_BIN_PROCESSED/double(GL_FILEBINLEN); + } int barWidth = 70; string progressbar = "======================================================================"; string spacebar___ = " "; cout << "["; int pbpos = barWidth * progress; + if (pbpos > barWidth) pbpos = barWidth; + if (pbpos < 0) pbpos = 0; int sbpos = barWidth - pbpos; cout< 0 ? 1 : 0)); + size_t res_index = 0; + for(int i=0;i 0) { + string hexbit = binData.substr(loops*4,bitsleft); + for(int i=0;hexbit.length()<4;i++){ + hexbit = "0" + hexbit; + } + for(int j=0;j<16;j++){ + if(HEXCODE[j] == hexbit){ + result[res_index++] = HEXARY[j]; + break; + } + } } return result; } \ No newline at end of file diff --git a/binrw.h b/binrw.h index 1db0e63..640d3a7 100644 --- a/binrw.h +++ b/binrw.h @@ -42,7 +42,7 @@ const int CMPARYLENGTH = 8; //必要函数声明 -inline const string char2bin(const unsigned char ch); +inline void char2bin_inline(const unsigned char ch, string& binData, size_t& offset); const string readBinary(const string filepath ,const int lr=0,const int rr=0); const int writeBinary(const string filepath,const string & binData); void* showProgressT(void* args);