Skip to content

Commit 88fb025

Browse files
YousefSalamafacebook-github-bot
authored andcommitted
backingstore: directly return IOBuf in FFI layer without intermediately converting to Vec<u8>
Summary: Change `backingstore` FFI layer to directly return `IOBuf` instead of intermediately converting to `Vec<u8>` and back to `IOBuf` again in the C++ code. Reviewed By: liubov-dmitrieva Differential Revision: D72132358 fbshipit-source-id: ff5d94f39d17ac381a44aa4424289add5040203a
1 parent 4f233f9 commit 88fb025

File tree

8 files changed

+37
-65
lines changed

8 files changed

+37
-65
lines changed

eden/scm/lib/backingstore/BUCK

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ rust_library(
9292
"fbsource//third-party/rust:parking_lot",
9393
"fbsource//third-party/rust:tracing",
9494
"fbsource//third-party/rust:tracing-subscriber",
95+
"//common/rust/folly:iobuf",
96+
"//eden/scm/lib/blob:scm-blob",
9597
"//eden/scm/lib/config/loader:configloader",
9698
"//eden/scm/lib/constructors:constructors",
9799
"//eden/scm/lib/eagerepo:eagerepo",

eden/scm/lib/backingstore/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ sapling-repo = { version = "0.1.0", path = "../repo" }
3333
sapling-storemodel = { version = "0.1.0", path = "../storemodel" }
3434
sapling-tracing-collector = { version = "0.1.0", path = "../tracing-collector" }
3535
sapling-types = { version = "0.1.0", path = "../types" }
36+
scm-blob = { version = "0.1.0", path = "../blob" }
3637
tracing = { version = "0.1.41", features = ["attributes", "valuable"] }
3738
tracing-subscriber = { version = "0.3.18", features = ["chrono", "env-filter", "json", "local-time", "parking_lot", "registry"] }
3839

eden/scm/lib/backingstore/include/ffi.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ void sapling_backingstore_get_blob_batch_handler(
8989
std::shared_ptr<GetBlobBatchResolver> resolver,
9090
size_t index,
9191
rust::String error,
92-
rust::Box<Blob> blob);
92+
std::unique_ptr<folly::IOBuf> blob);
9393

9494
void sapling_backingstore_get_file_aux_batch_handler(
9595
std::shared_ptr<GetFileAuxBatchResolver> resolver,

eden/scm/lib/backingstore/src/SaplingNativeBackingStore.cpp

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -158,23 +158,10 @@ folly::Try<std::unique_ptr<folly::IOBuf>> SaplingNativeBackingStore::getBlob(
158158
FetchMode fetch_mode) {
159159
XLOGF(DBG7, "Importing blob node={} from hgcache", folly::hexlify(node));
160160
return folly::makeTryWith([&] {
161-
auto opt_blob = sapling_backingstore_get_blob(
161+
return sapling_backingstore_get_blob(
162162
*store_.get(),
163163
rust::Slice<const uint8_t>{node.data(), node.size()},
164164
fetch_mode);
165-
166-
if (!opt_blob.present) {
167-
return std::unique_ptr<folly::IOBuf>(nullptr);
168-
} else {
169-
auto blob = opt_blob.blob.into_raw();
170-
return folly::IOBuf::takeOwnership(
171-
reinterpret_cast<void*>(blob->bytes.data()),
172-
blob->bytes.size(),
173-
[](void* /* buf */, void* blob) mutable {
174-
auto vec = rust::Box<Blob>::from_raw(reinterpret_cast<Blob*>(blob));
175-
},
176-
reinterpret_cast<void*>(blob));
177-
}
178165
});
179166
}
180167

eden/scm/lib/backingstore/src/backingstore.rs

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ use log::warn;
3131
use metrics::ods;
3232
use repo::repo::Repo;
3333
use repo::RepoMinimalInfo;
34+
use scm_blob::ScmBlob;
3435
use storemodel::BoxIterator;
35-
use storemodel::Bytes;
3636
use storemodel::FileAuxData;
3737
use storemodel::FileStore;
3838
use storemodel::TreeAuxData;
@@ -176,7 +176,7 @@ impl BackingStore {
176176
}
177177

178178
#[instrument(level = "trace", skip(self))]
179-
pub fn get_blob(&self, fctx: FetchContext, node: &[u8]) -> Result<Option<Vec<u8>>> {
179+
pub fn get_blob(&self, fctx: FetchContext, node: &[u8]) -> Result<Option<ScmBlob>> {
180180
self.maybe_reload().filestore.single(fctx, node)
181181
}
182182

@@ -186,7 +186,7 @@ impl BackingStore {
186186
#[instrument(level = "trace", skip(self, resolve))]
187187
pub fn get_blob_batch<F>(&self, fctx: FetchContext, keys: Vec<Key>, resolve: F)
188188
where
189-
F: Fn(usize, Result<Option<Vec<u8>>>),
189+
F: Fn(usize, Result<Option<ScmBlob>>),
190190
{
191191
self.maybe_reload()
192192
.filestore
@@ -567,24 +567,19 @@ where
567567
}
568568

569569
/// Read file content.
570-
impl LocalRemoteImpl<Bytes, Vec<u8>> for Arc<dyn FileStore> {
571-
fn get_local_single(&self, path: &RepoPath, id: HgId) -> Result<Option<Bytes>> {
572-
Ok(self
573-
.get_local_content(path, id)?
574-
.map(|blob| blob.into_bytes()))
570+
impl LocalRemoteImpl<ScmBlob> for Arc<dyn FileStore> {
571+
fn get_local_single(&self, path: &RepoPath, id: HgId) -> Result<Option<ScmBlob>> {
572+
self.get_local_content(path, id)
575573
}
576-
fn get_single(&self, fctx: FetchContext, path: &RepoPath, id: HgId) -> Result<Bytes> {
577-
Ok(self.get_content(fctx, path, id)?.into_bytes())
574+
fn get_single(&self, fctx: FetchContext, path: &RepoPath, id: HgId) -> Result<ScmBlob> {
575+
self.get_content(fctx, path, id)
578576
}
579577
fn get_batch_iter(
580578
&self,
581579
fctx: FetchContext,
582580
keys: Vec<Key>,
583-
) -> Result<BoxIterator<Result<(Key, Bytes)>>> {
584-
Ok(Box::new(
585-
self.get_content_iter(fctx, keys)?
586-
.map(|r| r.map(|(k, v)| (k, v.into_bytes()))),
587-
))
581+
) -> Result<BoxIterator<Result<(Key, ScmBlob)>>> {
582+
Ok(Box::new(self.get_content_iter(fctx, keys)?))
588583
}
589584
}
590585

eden/scm/lib/backingstore/src/ffi.cpp

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,21 +52,13 @@ void sapling_backingstore_get_blob_batch_handler(
5252
std::shared_ptr<GetBlobBatchResolver> resolver,
5353
size_t index,
5454
rust::String error,
55-
rust::Box<Blob> blob) {
55+
std::unique_ptr<folly::IOBuf> blob) {
5656
using ResolveResult = folly::Try<std::unique_ptr<folly::IOBuf>>;
5757

5858
resolver->resolve(
5959
index, folly::makeTryWith([&] {
6060
if (error.empty()) {
61-
auto result = blob.into_raw();
62-
return ResolveResult{folly::IOBuf::takeOwnership(
63-
reinterpret_cast<void*>(result->bytes.data()),
64-
result->bytes.size(),
65-
[](void* /* buf */, void* blob) mutable {
66-
auto box =
67-
rust::Box<Blob>::from_raw(reinterpret_cast<Blob*>(blob));
68-
},
69-
reinterpret_cast<void*>(result))};
61+
return ResolveResult{std::move(blob)};
7062
} else {
7163
return ResolveResult{SaplingFetchError{std::string(error)}};
7264
}

eden/scm/lib/backingstore/src/ffi.rs

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ use anyhow::anyhow;
1414
use anyhow::Error;
1515
use anyhow::Result;
1616
use cxx::SharedPtr;
17+
use cxx::UniquePtr;
18+
use iobuf::IOBuf;
1719
use storemodel::FileAuxData as ScmStoreFileAuxData;
1820
use types::fetch_cause::FetchCause;
1921
use types::fetch_mode::FetchMode;
@@ -105,15 +107,6 @@ pub(crate) mod ffi {
105107
files: Vec<String>,
106108
}
107109

108-
pub struct Blob {
109-
pub(crate) bytes: Vec<u8>,
110-
}
111-
112-
pub struct OptionalBlob {
113-
present: bool,
114-
blob: Box<Blob>,
115-
}
116-
117110
pub struct FileAuxData {
118111
total_size: u64,
119112
content_sha1: [u8; 20],
@@ -123,6 +116,9 @@ pub(crate) mod ffi {
123116
unsafe extern "C++" {
124117
include!("eden/scm/lib/backingstore/include/ffi.h");
125118

119+
#[namespace = "folly"]
120+
type IOBuf = iobuf::IOBuf;
121+
126122
type GetTreeBatchResolver;
127123
type GetTreeAuxBatchResolver;
128124
type GetBlobBatchResolver;
@@ -146,7 +142,7 @@ pub(crate) mod ffi {
146142
resolve_state: SharedPtr<GetBlobBatchResolver>,
147143
index: usize,
148144
error: String,
149-
blob: Box<Blob>,
145+
blob: UniquePtr<IOBuf>,
150146
);
151147

152148
unsafe fn sapling_backingstore_get_file_aux_batch_handler(
@@ -202,7 +198,7 @@ pub(crate) mod ffi {
202198
store: &BackingStore,
203199
node: &[u8],
204200
fetch_mode: FetchMode,
205-
) -> Result<OptionalBlob>;
201+
) -> Result<UniquePtr<IOBuf>>;
206202

207203
pub fn sapling_backingstore_get_blob_batch(
208204
store: &BackingStore,
@@ -403,20 +399,14 @@ pub fn sapling_backingstore_get_blob(
403399
store: &BackingStore,
404400
node: &[u8],
405401
fetch_mode: ffi::FetchMode,
406-
) -> Result<ffi::OptionalBlob> {
402+
) -> Result<UniquePtr<IOBuf>> {
407403
// the cause is not propagated for this API
408404
match store.get_blob(
409405
FetchContext::new_with_cause(FetchMode::from(fetch_mode), FetchCause::EdenUnknown),
410406
node,
411407
)? {
412-
Some(blob) => Ok(ffi::OptionalBlob {
413-
blob: Box::new(ffi::Blob { bytes: blob }),
414-
present: true,
415-
}),
416-
None => Ok(ffi::OptionalBlob {
417-
blob: Box::new(ffi::Blob { bytes: Vec::new() }),
418-
present: false,
419-
}),
408+
Some(blob) => Ok(blob.into_iobuf().into()),
409+
None => Ok(UniquePtr::null()),
420410
}
421411
}
422412

@@ -436,11 +426,8 @@ pub fn sapling_backingstore_get_blob_batch(
436426
let result = result.and_then(|opt| opt.ok_or_else(|| Error::msg("no blob found")));
437427
let resolver = resolver.clone();
438428
let (error, blob) = match result {
439-
Ok(blob) => (String::default(), Box::new(ffi::Blob { bytes: blob })),
440-
Err(error) => (
441-
format!("{:?}", error),
442-
Box::new(ffi::Blob { bytes: vec![] }),
443-
),
429+
Ok(blob) => (String::default(), blob.into_iobuf().into()),
430+
Err(error) => (format!("{:?}", error), UniquePtr::null()),
444431
};
445432
unsafe { ffi::sapling_backingstore_get_blob_batch_handler(resolver, idx, error, blob) };
446433
},

eden/scm/lib/blob/src/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ impl ScmBlob {
3333
}
3434
}
3535

36+
#[cfg(fbcode_build)]
37+
pub fn into_iobuf(self) -> iobuf::IOBufShared {
38+
match self {
39+
Self::Bytes(bytes) => iobuf::IOBufShared::from(bytes),
40+
Self::IOBuf(buf) => buf,
41+
}
42+
}
43+
3644
pub fn len(&self) -> usize {
3745
match self {
3846
Self::Bytes(bytes) => bytes.len(),

0 commit comments

Comments
 (0)