-
Notifications
You must be signed in to change notification settings - Fork 122
fix(libra): refactor ClientStorage::read_pack_obj, optimize the pack obj parse time.
#1426
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,17 +16,22 @@ use mercury::internal::pack::Pack; | |
| use mercury::utils::read_sha1; | ||
| use once_cell::sync::Lazy; | ||
| use regex::Regex; | ||
| use std::collections::HashSet; | ||
| use std::collections::{HashMap, HashSet}; | ||
| use std::io::prelude::*; | ||
| use std::io::{BufReader, Cursor}; | ||
| use std::path::{Path, PathBuf}; | ||
| use std::str::FromStr; | ||
| use std::sync::{Arc, Mutex}; | ||
| use std::{fs, io}; | ||
|
|
||
| static PACK_OBJ_CACHE: Lazy<Mutex<LruCache<String, CacheObject>>> = Lazy::new(|| { | ||
| // `lazy_static!` may affect IDE's code completion | ||
| Mutex::new(LruCache::new(1024 * 1024 * 200)) | ||
| }); | ||
|
|
||
| static PACK_FILE_CACHE: Lazy<Mutex<HashMap<String, Vec<u8>>>> = | ||
| Lazy::new(|| Mutex::new(HashMap::new())); | ||
|
|
||
| #[derive(Default)] | ||
| pub struct ClientStorage { | ||
| base_path: PathBuf, | ||
|
|
@@ -465,19 +470,35 @@ impl ClientStorage { | |
|
|
||
| /// Read object from pack file, with offset | ||
| fn read_pack_obj(pack_file: &Path, offset: u64) -> Result<CacheObject, GitError> { | ||
| let cache_key = format!("{:?}-{}", pack_file.file_name().unwrap(), offset); | ||
| let file_name = pack_file.file_name().unwrap().to_str().unwrap().to_owned(); | ||
| let cache_key = format!("{:?}-{}", file_name, offset); | ||
| // read cache | ||
| if let Some(cached) = PACK_OBJ_CACHE.lock().unwrap().get(&cache_key) { | ||
| return Ok(cached.clone()); | ||
| } | ||
|
|
||
| let file = fs::File::open(pack_file)?; | ||
| let mut pack_reader = io::BufReader::new(&file); | ||
| pack_reader.seek(io::SeekFrom::Start(offset))?; | ||
| let mut pack = Pack::new(None, None, None, false); | ||
| let obj = { | ||
| let mut offset = offset as usize; | ||
| pack.decode_pack_object(&mut pack_reader, &mut offset)? // offset will be updated! | ||
| let mut cache = PACK_FILE_CACHE.lock().unwrap(); | ||
| let pack_file_buf = match cache.get(&file_name) { | ||
| None => { | ||
| let file = fs::File::open(pack_file)?; | ||
| let mut pack_reader = io::BufReader::new(&file); | ||
|
|
||
| let mut buf: Vec<u8> = Vec::new(); | ||
| pack_reader.read_to_end(&mut buf)?; | ||
|
Comment on lines
+487
to
+488
|
||
| cache.insert(file_name.clone(), buf); | ||
| cache.get(&file_name).unwrap() | ||
| } | ||
| Some(buf) => buf, | ||
| }; | ||
|
|
||
| let pack_cursor = Cursor::new(pack_file_buf); | ||
| let mut pack_reader = BufReader::new(pack_cursor); | ||
| pack_reader.seek(io::SeekFrom::Start(offset))?; | ||
| { | ||
| let mut offset = offset as usize; | ||
| Pack::decode_pack_object(&mut pack_reader, &mut offset)? // offset will be updated! | ||
| } | ||
| }; | ||
| let full_obj = match obj.object_type() { | ||
| ObjectType::OffsetDelta => { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The cache key format is inconsistent between lines 473-474. Line 473 extracts
file_nameas a string, but line 474 uses{:?}formatting which will add quotes around the string. This could cause cache misses. Use{}instead of{:?}in the format string.