-
Notifications
You must be signed in to change notification settings - Fork 451
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GitObjects: use new SideChannelStream to hash during download
The loose object download needs to verify the objects being sent across the wire before moving them to their "real" locations in the object directory. The previous implementation wrote them to disk then read from that file to hash the contents. Instead, create a new "SideChannelStream" that injects between the responseStream and the inflater, but sends all of the data that is read out to the filestream. Here is a diagram of the stream sequence: responseStream -> sideChannelStream -> inflateStream -> hashingStream \ | \ \|/ tempFileStream devNull By copying all data from the hashingStream into devNull, we ensure that the responseStream is drained and written to the tempFileStream while simultaneously inflating and hashing the object contents. We then amortize this computation during the window that we are downloading data over the network, minimizing the potential performance impact. Helped-by: Matthew Cheetham <[email protected]> Signed-off-by: Derrick Stolee <[email protected]>
- Loading branch information
1 parent
b8e2283
commit ded306b
Showing
3 changed files
with
74 additions
and
11 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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 |
---|---|---|
@@ -0,0 +1,60 @@ | ||
using System; | ||
using System.IO; | ||
|
||
namespace GVFS.Common.Git | ||
{ | ||
/// <summary> | ||
/// As you read from a SideChannelStream, we read from the inner | ||
/// 'from' stream and write that data to the inner 'to' stream | ||
/// before passing the bytes out to the reader. | ||
/// </summary> | ||
public class SideChannelStream : Stream | ||
{ | ||
protected readonly Stream from; | ||
protected readonly Stream to; | ||
|
||
public SideChannelStream(Stream from, Stream to) | ||
{ | ||
this.from = from; | ||
this.to = to; | ||
} | ||
|
||
public override bool CanRead => true; | ||
|
||
public override bool CanSeek => false; | ||
|
||
public override bool CanWrite => false; | ||
|
||
public override long Length => 0; | ||
|
||
public override long Position { get => 0; set => throw new NotImplementedException(); } | ||
|
||
public override void Flush() | ||
{ | ||
this.from.Flush(); | ||
this.to.Flush(); | ||
} | ||
|
||
public override int Read(byte[] buffer, int offset, int count) | ||
{ | ||
int n = this.from.Read(buffer, offset, count); | ||
this.to.Write(buffer, offset, n); | ||
return n; | ||
} | ||
|
||
public override long Seek(long offset, SeekOrigin origin) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
public override void SetLength(long value) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
public override void Write(byte[] buffer, int offset, int count) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
} | ||
} |
This file contains 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