Skip to content

Commit

Permalink
Use binary repeat_counts file for better performance, Changed repeat_…
Browse files Browse the repository at this point in the history
…counts from uint32_t to size_t for better alignment in the file, Changed repeat_counts save time to every 1minute as destructor not working
  • Loading branch information
Your Name committed Jan 25, 2025
1 parent 1291ed2 commit 9b724eb
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
26 changes: 18 additions & 8 deletions sherpa-onnx/csrc/offline-tts-cache-mechanism.cc
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,13 @@ std::vector<float> OfflineTtsCacheMechanism::GetWavFile(
repeat_counts_[text_hash]++; // Increment the repeat count
}

// Save the repeat counts every 10 minutes
//auto now = std::chrono::steady_clock::now();
//if (std::chrono::duration_cast<std::chrono::seconds>(
//now - last_save_time_).count() >= 10 * 60) {
// Save the repeat counts every minute
auto now = std::chrono::steady_clock::now();
if (std::chrono::duration_cast<std::chrono::seconds>(
now - last_save_time_).count() >= 1 * 60) {
SaveRepeatCounts();
//last_save_time_ = now;
//}
last_save_time_ = now;
}

return samples;
}
Expand Down Expand Up @@ -206,14 +206,17 @@ void OfflineTtsCacheMechanism::LoadRepeatCounts() {
// Read each entry
for (size_t i = 0; i < num_entries; ++i) {
std::size_t text_hash;
int32_t count;
std::size_t count;
ifs.read(reinterpret_cast<char*>(&text_hash), sizeof(text_hash));
ifs.read(reinterpret_cast<char*>(&count), sizeof(count));
repeat_counts_[text_hash] = count;
}
}

void OfflineTtsCacheMechanism::SaveRepeatCounts() {
// Start timing
auto start_time = std::chrono::steady_clock::now();

std::string repeat_count_file = cache_dir_ + "/repeat_counts.bin";

// Open the file for writing in binary mode
Expand All @@ -233,6 +236,13 @@ void OfflineTtsCacheMechanism::SaveRepeatCounts() {
ofs.write(reinterpret_cast<const char*>(&entry.first), sizeof(entry.first));
ofs.write(reinterpret_cast<const char*>(&entry.second), sizeof(entry.second));
}

// End timing
auto end_time = std::chrono::steady_clock::now();
auto elapsed_time = std::chrono::duration_cast<std::chrono::milliseconds>(end_time - start_time).count();

// Print the time taken
SHERPA_ONNX_LOGE("SaveRepeatCounts took %lld milliseconds", elapsed_time);
}

void OfflineTtsCacheMechanism::RemoveWavFile(const std::size_t &text_hash) {
Expand Down Expand Up @@ -294,7 +304,7 @@ void OfflineTtsCacheMechanism::EnsureCacheLimit() {

std::size_t OfflineTtsCacheMechanism::GetLeastRepeatedFile() {
std::size_t least_repeated_file = 0;
int32_t min_count = std::numeric_limits<int32_t>::max();
std::size_t min_count = std::numeric_limits<std::size_t>::max();

for (const auto &entry : repeat_counts_) {
if (entry.second <= 1) {
Expand Down
2 changes: 1 addition & 1 deletion sherpa-onnx/csrc/offline-tts-cache-mechanism.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class OfflineTtsCacheMechanism {
int32_t used_cache_size_bytes_;

// Map of text hash to repeat count
std::unordered_map<std::size_t, int32_t> repeat_counts_;
std::unordered_map<std::size_t, std::size_t> repeat_counts_;

// Vector of cached file names
std::vector<std::size_t> cache_vector_;
Expand Down

0 comments on commit 9b724eb

Please sign in to comment.