From ed321844e405206d50df1f488299b4ff88c3b6b0 Mon Sep 17 00:00:00 2001 From: zxj Date: Sat, 27 Jul 2024 22:06:43 +0800 Subject: [PATCH] optimize performance --- Cargo.toml | 2 +- src/plugin/console.rs | 9 ++++++++- src/plugin/file.rs | 9 ++++++++- src/plugin/file_split.rs | 9 ++++++++- 4 files changed, 25 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index f8c7055..12d6d9b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,7 +6,7 @@ members = [ [package] name = "fast_log" -version = "1.7.1" +version = "1.7.2" description = "Rust async log High-performance asynchronous logging" readme = "Readme.md" authors = ["ce "] diff --git a/src/plugin/console.rs b/src/plugin/console.rs index ceb9060..64c2a9c 100644 --- a/src/plugin/console.rs +++ b/src/plugin/console.rs @@ -8,7 +8,14 @@ impl LogAppender for ConsoleAppender { if records.len() == 0 { return; } - let mut buffer = String::with_capacity(records.len()); + let mut cap = 0; + if records.len() != 0 { + cap = 0; + for x in records { + cap += x.formated.len(); + } + } + let mut buffer = String::with_capacity(cap); for x in records { buffer.push_str(&x.formated); } diff --git a/src/plugin/file.rs b/src/plugin/file.rs index af82283..9f48c78 100644 --- a/src/plugin/file.rs +++ b/src/plugin/file.rs @@ -30,7 +30,14 @@ impl FileAppender { impl LogAppender for FileAppender { fn do_logs(&mut self, records: &[FastLogRecord]) { let mut log_file = self.file.borrow_mut(); - let mut buf = String::new(); + let mut cap = 0; + if records.len() != 0 { + cap = 0; + for x in records { + cap += x.formated.len(); + } + } + let mut buf = String::with_capacity(cap); for x in records { buf.push_str(&x.formated); match &x.command { diff --git a/src/plugin/file_split.rs b/src/plugin/file_split.rs index 360595d..1c890f9 100644 --- a/src/plugin/file_split.rs +++ b/src/plugin/file_split.rs @@ -491,7 +491,14 @@ impl Keep for KeepType { impl LogAppender for FileSplitAppender { fn do_logs(&mut self, records: &[FastLogRecord]) { //if temp_bytes is full,must send pack - let mut temp = String::with_capacity(records.len() * 10); + let mut cap = 0; + if records.len() != 0 { + cap = 0; + for x in records { + cap += x.formated.len(); + } + } + let mut temp = String::with_capacity(cap); for x in records { match x.command { Command::CommandRecord => {