History

avoid unnecessary allocation when creating SHA1 paths in loose ODB

Sebastian Thiel committed 6 years ago

Commit 09d8d3a1 parent 4ff21684 files · +25 −31Browse files

Showing the changes to git-odb/src (1 of 4 files). Show all

Unified · Split

git-odb/src/loose/object.rs+3−16
@@ -3,7 +3,6 @@ use crate::{ · 2 unmodified lines
3 zlib,3 zlib,
4};4};
5use git_object as object;5use git_object as object;
6use hex::ToHex;
7use object::borrowed;6use object::borrowed;
8use quick_error::quick_error;7use quick_error::quick_error;
9use smallvec::SmallVec;8use smallvec::SmallVec;
@@ -128,21 +127,9 @@ pub fn parse_header(input: &[u8]) -> Result<(object::Kind, usize, usize), Error> · 118 unmodified lines
128}127}
129 128
130fn sha1_path(id: &[u8; 20], mut root: PathBuf) -> PathBuf {129fn sha1_path(id: &[u8; 20], mut root: PathBuf) -> PathBuf {
131 struct Buf([u8; 40], usize);130 let mut buf = [0u8; 40];
132 let mut buf = Buf([0u8; 40], 0);131 hex::encode_to_slice(id, &mut buf).expect("no failure as everything is preset by now");
133 132 let buf = std::str::from_utf8(&buf).expect("ascii only in hex");
134 impl std::fmt::Write for Buf {
135 fn write_str(&mut self, s: &str) -> std::fmt::Result {
136 self.0[self.1..self.1 + buf.len()].copy_from_slice(buf);
137 self.1 += buf.len();
138 Ok(())
139 }
140 }
141
142 {
143 id.write_hex(&mut buf)
144 .expect("no failure as everything is preset by now");
145 }
146 root.push(&buf[..2]);133 root.push(&buf[..2]);
147 root.push(&buf[2..]);134 root.push(&buf[2..]);
148 root135 root

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t