Skip to content

[Chore] Format cpp and py via clang-format and ruff #41

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 3 commits into from
Aug 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
167 changes: 167 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
---
Language: Cpp
BasedOnStyle: Google
ColumnLimit: 80
AccessModifierOffset: -1
AlignAfterOpenBracket: Align
AlignConsecutiveMacros: false
AlignConsecutiveAssignments: false
AlignConsecutiveDeclarations: false
AlignEscapedNewlines: Left
AlignOperands: true
AlignTrailingComments: true
AllowAllArgumentsOnNextLine: true
AllowAllConstructorInitializersOnNextLine: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: Never
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: All
AllowShortLambdasOnASingleLine: All
AllowShortIfStatementsOnASingleLine: WithoutElse
AllowShortLoopsOnASingleLine: true
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: true
AlwaysBreakTemplateDeclarations: Yes
BinPackArguments: true
BinPackParameters: true
BraceWrapping:
AfterCaseLabel: false
AfterClass: false
AfterControlStatement: false
AfterEnum: false
AfterFunction: false
AfterNamespace: false
AfterObjCDeclaration: false
AfterStruct: false
AfterUnion: false
AfterExternBlock: false
BeforeCatch: false
BeforeElse: false
IndentBraces: false
SplitEmptyFunction: true
SplitEmptyRecord: true
SplitEmptyNamespace: true
BreakBeforeBinaryOperators: None
BreakBeforeBraces: Attach
BreakBeforeInheritanceComma: false
BreakInheritanceList: BeforeColon
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: false
BreakConstructorInitializers: BeforeColon
BreakAfterJavaFieldAnnotations: false
BreakStringLiterals: true
CommentPragmas: '^ IWYU pragma:'
CompactNamespaces: false
ConstructorInitializerAllOnOneLineOrOnePerLine: true
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true
DeriveLineEnding: true
DerivePointerAlignment: true
DisableFormat: false
ExperimentalAutoDetectBinPacking: false
FixNamespaceComments: true
ForEachMacros:
- foreach
- Q_FOREACH
- BOOST_FOREACH
IncludeBlocks: Regroup
IncludeCategories:
- Regex: '^<ext/.*\.h>'
Priority: 2
SortPriority: 0
- Regex: '^<.*\.h>'
Priority: 1
SortPriority: 0
- Regex: '^<.*'
Priority: 2
SortPriority: 0
- Regex: '.*'
Priority: 3
SortPriority: 0
IncludeIsMainRegex: '([-_](test|unittest))?$'
IncludeIsMainSourceRegex: ''
IndentCaseLabels: true
IndentGotoLabels: true
IndentPPDirectives: None
IndentWidth: 2
IndentWrappedFunctionNames: false
JavaScriptQuotes: Leave
JavaScriptWrapImports: true
KeepEmptyLinesAtTheStartOfBlocks: false
MacroBlockBegin: ''
MacroBlockEnd: ''
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
ObjCBinPackProtocolList: Never
ObjCBlockIndentWidth: 2
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: true
PenaltyBreakAssignment: 2
PenaltyBreakBeforeFirstCallParameter: 1
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakString: 1000
PenaltyBreakTemplateDeclaration: 10
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 200
PointerAlignment: Left
RawStringFormats:
- Language: Cpp
Delimiters:
- cc
- CC
- cpp
- Cpp
- CPP
- 'c++'
- 'C++'
CanonicalDelimiter: ''
BasedOnStyle: google
- Language: TextProto
Delimiters:
- pb
- PB
- proto
- PROTO
EnclosingFunctions:
- EqualsProto
- EquivToProto
- PARSE_PARTIAL_TEXT_PROTO
- PARSE_TEST_PROTO
- PARSE_TEXT_PROTO
- ParseTextOrDie
- ParseTextProtoOrDie
CanonicalDelimiter: ''
BasedOnStyle: google
ReflowComments: true
SortIncludes: true
SortUsingDeclarations: true
SpaceAfterCStyleCast: false
SpaceAfterLogicalNot: false
SpaceAfterTemplateKeyword: true
SpaceBeforeAssignmentOperators: true
SpaceBeforeCpp11BracedList: false
SpaceBeforeCtorInitializerColon: true
SpaceBeforeInheritanceColon: true
SpaceBeforeParens: ControlStatements
SpaceBeforeRangeBasedForLoopColon: true
SpaceInEmptyBlock: false
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 2
SpacesInAngles: false
SpacesInConditionalStatement: false
SpacesInContainerLiterals: true
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
SpaceBeforeSquareBrackets: false
Standard: Auto
StatementMacros:
- Q_UNUSED
- QT_REQUIRE_VERSION
TabWidth: 8
UseCRLF: false
UseTab: Never
...
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -231,4 +231,5 @@ sftp-config.json
# Custom files
CMakeFiles/*
*.pyc
!scripts/smart_build.py
!scripts/smart_build.py
uv.lock
6 changes: 3 additions & 3 deletions libcachesim/synthetic_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,12 +181,12 @@ def set_read_pos(self, pos: float) -> None:
def get_read_pos(self) -> float:
"""Get current read position"""
return float(self.current_pos)
def get_working_set_size(self) -> Tuple[int, int]:

def get_working_set_size(self) -> tuple[int, int]:
"""Calculate working set size"""
wss_obj, wss_byte = 0, 0
if self.current_pos > 0:
unique_ids = np.unique(self.obj_ids[:self.current_pos])
unique_ids = np.unique(self.obj_ids[: self.current_pos])
wss_obj = len(unique_ids)
wss_byte = wss_obj * self.obj_size
return wss_obj, wss_byte
Expand Down
11 changes: 10 additions & 1 deletion libcachesim/trace_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,16 @@
from urllib.parse import urlparse

from .protocols import ReaderProtocol
from .libcachesim_python import TraceType, SamplerType, Request, ReaderInitParam, Reader, Sampler, ReadDirection, cal_working_set_size
from .libcachesim_python import (
TraceType,
SamplerType,
Request,
ReaderInitParam,
Reader,
Sampler,
ReadDirection,
cal_working_set_size,
)
from ._s3_cache import get_data_loader

logger = logging.getLogger(__name__)
Expand Down
55 changes: 32 additions & 23 deletions src/export_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace libcachesim {

namespace py = pybind11;

const cache_obj_t* LHD_HIT_MARKER = reinterpret_cast<cache_obj_t *>(0x1);
const cache_obj_t* LHD_HIT_MARKER = reinterpret_cast<cache_obj_t*>(0x1);

// Custom deleters for smart pointers
struct CacheDeleter {
Expand Down Expand Up @@ -258,7 +258,7 @@ auto make_cache_wrapper(const std::string& fn_name) {
cache_t* ptr = InitFn(cc_params, params_cstr);
return std::unique_ptr<cache_t, CacheDeleter>(ptr);
},
"cc_params"_a, "cache_specific_params"_a = "");
"cc_params"_a, "cache_specific_params"_a = "");
};
}

Expand All @@ -281,18 +281,24 @@ void export_cache(py::module& m) {
"req"_a)
.def(
"find",
[](cache_t& self, const request_t& req, const bool update_cache) -> py::object {
[](cache_t& self, const request_t& req,
const bool update_cache) -> py::object {
cache_obj_t* obj = self.find(&self, &req, update_cache);
// Return None if obj is null (not found)
if (obj == nullptr) {
return py::none();
return py::none();
}
// NOTE(haocheng): For LHD only, return a dummy object for hit
if (obj == LHD_HIT_MARKER) {
cache_obj_t* dummy_obj = static_cast<cache_obj_t*>(calloc(1, sizeof(cache_obj_t)));
dummy_obj->obj_id = req.obj_id;
dummy_obj->obj_size = req.obj_size;
return py::cast(std::unique_ptr<cache_obj_t, CacheObjectDeleter>(dummy_obj));
cache_obj_t* dummy_obj =
static_cast<cache_obj_t*>(calloc(1, sizeof(cache_obj_t)));
if (dummy_obj == nullptr) {
throw std::bad_alloc();
}
dummy_obj->obj_id = req.obj_id;
dummy_obj->obj_size = req.obj_size;
return py::cast(
std::unique_ptr<cache_obj_t, CacheObjectDeleter>(dummy_obj));
}
return py::cast(obj, py::return_value_policy::reference);
},
Expand All @@ -303,23 +309,25 @@ void export_cache(py::module& m) {
return self.can_insert(&self, &req);
},
"req"_a)
.def(
"insert",
[](cache_t& self, const request_t& req) -> std::optional<cache_obj_t*> {
cache_obj_t* inserted = self.insert(&self, &req);
if (inserted == nullptr) {
return std::nullopt;
}
return inserted;
},
"req"_a,
py::return_value_policy::reference // optional still respected
)

.def(
"insert",
[](cache_t& self,
const request_t& req) -> std::optional<cache_obj_t*> {
cache_obj_t* inserted = self.insert(&self, &req);
if (inserted == nullptr) {
return std::nullopt;
}
return inserted;
},
"req"_a,
py::return_value_policy::reference // optional still respected
)

.def(
"need_eviction",
[](cache_t& self, const request_t& req) {
return self.get_occupied_byte(&self) + req.obj_size > self.cache_size;
return self.get_occupied_byte(&self) + req.obj_size >
self.cache_size;
},
"req"_a)
.def(
Expand Down Expand Up @@ -370,7 +378,8 @@ void export_cache(py::module& m) {
params->default_ttl = default_ttl;
params->hashpower = hashpower;
params->consider_obj_metadata = consider_obj_metadata;
return std::unique_ptr<common_cache_params_t, CommonCacheParamsDeleter>(params);
return std::unique_ptr<common_cache_params_t,
CommonCacheParamsDeleter>(params);
}),
"cache_size"_a, "default_ttl"_a = 86400 * 300, "hashpower"_a = 24,
"consider_obj_metadata"_a = false)
Expand Down
14 changes: 8 additions & 6 deletions src/export_reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,16 @@ struct SamplerDeleter {
}
};


void export_reader(py::module& m) {
/* Helper function(s) */
m.def("cal_working_set_size", [](reader_t& reader) {
int64_t wss_obj = 0, wss_byte = 0;
cal_working_set_size(&reader, &wss_obj, &wss_byte);
return std::make_tuple(wss_obj, wss_byte);
}, "reader"_a);
m.def(
"cal_working_set_size",
[](reader_t& reader) {
int64_t wss_obj = 0, wss_byte = 0;
cal_working_set_size(&reader, &wss_obj, &wss_byte);
return std::make_tuple(wss_obj, wss_byte);
},
"reader"_a);

// Sampler type enumeration
py::enum_<sampler_type>(m, "SamplerType")
Expand Down
Loading
Loading