Generalize SSO Detection

Summary: Previously hard-coded values for how long a string has to be for SSO, now just check if data pointer is inside string stuct

Reviewed By: chadaustin

Differential Revision: D15720268

fbshipit-source-id: 988e45648e8b96332587f8d2f021642407d3dac3
This commit is contained in:
Jake Crouch 2019-06-07 18:48:48 -07:00 committed by Facebook Github Bot
parent 596243f44b
commit 29da6b4730
2 changed files with 16 additions and 46 deletions

View File

@ -1,44 +0,0 @@
/*
* Copyright (c) 2019-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/
#include "eden/fs/utils/Memory.h"
#include <folly/FBString.h>
#include <folly/String.h>
#include <folly/portability/Stdlib.h>
namespace facebook {
namespace eden {
size_t estimateIndirectMemoryUsage(const std::string& path) {
size_t length = path.length();
#ifdef _LIBCPP_VERSION
constexpr size_t kStdStringSsoLength = sizeof(std::string) - 2;
#elif defined(__GLIBCXX__) || defined(__GLIBCPP__)
constexpr size_t kStdStringSsoLength = (sizeof(std::string) >> 1) - 1;
#else
constexpr size_t kStdStringSsoLength = 0;
NOT_IMPLEMENTED();
#endif
if (length <= kStdStringSsoLength) {
return 0;
} else {
return folly::goodMallocSize(path.capacity());
}
}
size_t estimateIndirectMemoryUsage(const folly::fbstring& path) {
size_t length = path.length();
constexpr size_t kFbStringSsoLength = 23;
if (length <= kFbStringSsoLength) {
return 0;
} else {
return folly::goodMallocSize(path.capacity());
}
}
} // namespace eden
} // namespace facebook

View File

@ -13,7 +13,21 @@
namespace facebook {
namespace eden {
size_t estimateIndirectMemoryUsage(const std::string& path);
size_t estimateIndirectMemoryUsage(const folly::fbstring& path);
template <typename StringType>
bool isStringStorageEmbedded(const StringType& t) {
const void* tbegin = &t;
const void* tend = &t + 1;
return std::less_equal<const void*>{}(tbegin, t.data()) &&
std::less<const void*>{}(t.data(), tend);
}
template <typename StringType>
size_t estimateIndirectMemoryUsage(const StringType& s) {
if (isStringStorageEmbedded(s)) {
return 0;
} else {
return folly::goodMallocSize(s.capacity());
}
}
} // namespace eden
} // namespace facebook