From 6309b0bffa3f658f0ff87a5ec02786a835a36011 Mon Sep 17 00:00:00 2001 From: Pavel Kirilin Date: Fri, 12 Sep 2025 13:38:13 +0200 Subject: [PATCH] Added speedup project code. --- .../posts/python-speedup/code/.clang-format | 65 +++++++++++++++++++ content/posts/python-speedup/code/.gitignore | 25 +++++++ .../posts/python-speedup/code/CMakeLists.txt | 34 ++++++++++ .../posts/python-speedup/code/src/speedup.cpp | 56 ++++++++++++++++ content/posts/python-speedup/code/test.py | 35 ++++++++++ content/posts/python-speedup/index.md | 17 +++++ 6 files changed, 232 insertions(+) create mode 100644 content/posts/python-speedup/code/.clang-format create mode 100644 content/posts/python-speedup/code/.gitignore create mode 100644 content/posts/python-speedup/code/CMakeLists.txt create mode 100644 content/posts/python-speedup/code/src/speedup.cpp create mode 100644 content/posts/python-speedup/code/test.py create mode 100644 content/posts/python-speedup/index.md diff --git a/content/posts/python-speedup/code/.clang-format b/content/posts/python-speedup/code/.clang-format new file mode 100644 index 0000000..90314ef --- /dev/null +++ b/content/posts/python-speedup/code/.clang-format @@ -0,0 +1,65 @@ +--- +Language: Cpp +BasedOnStyle: LLVM + +AccessModifierOffset: -2 +AlignAfterOpenBracket: Align +AlignConsecutiveMacros: true +AlignConsecutiveAssignments: true +AlignEscapedNewlines: Right +AlignOperands: false +AlignTrailingComments: true +AllowAllArgumentsOnNextLine: true +AllowAllConstructorInitializersOnNextLine: true +AllowAllParametersOfDeclarationOnNextLine: true +AllowShortBlocksOnASingleLine: true +AllowShortCaseLabelsOnASingleLine: true +AllowShortFunctionsOnASingleLine: Empty +AllowShortIfStatementsOnASingleLine: Never +AllowShortLambdasOnASingleLine: All +AllowShortLoopsOnASingleLine: false +AlwaysBreakAfterDefinitionReturnType: None +AlwaysBreakAfterReturnType: None +AlwaysBreakBeforeMultilineStrings: false +AlwaysBreakTemplateDeclarations: Yes +BreakBeforeBraces: Attach +BreakBeforeTernaryOperators: false +BreakConstructorInitializers: AfterColon +ColumnLimit: 180 +CompactNamespaces: false +ConstructorInitializerAllOnOneLineOrOnePerLine: false +ExperimentalAutoDetectBinPacking: false +FixNamespaceComments: false +IncludeBlocks: Preserve +IndentCaseLabels: true +IndentWidth: 4 +PointerAlignment: Left +ReflowComments: false +SortIncludes: false +SortUsingDeclarations: false +SpaceAfterCStyleCast: false +SpaceAfterLogicalNot: false +SpaceAfterTemplateKeyword: true +SpaceBeforeCtorInitializerColon: true +SpaceBeforeInheritanceColon: true +SpaceBeforeParens: ControlStatements +SpaceBeforeRangeBasedForLoopColon: true +SpaceInEmptyParentheses: false +SpacesBeforeTrailingComments: 1 +SpacesInAngles: false +SpacesInCStyleCastParentheses: false +SpacesInContainerLiterals: false +SpacesInParentheses: false +SpacesInSquareBrackets: false +Standard: Auto +TabWidth: 4 +UseTab: Never + +AllowShortEnumsOnASingleLine: false + +BraceWrapping: + AfterEnum: false + +AlignConsecutiveDeclarations: AcrossEmptyLines + +NamespaceIndentation: All diff --git a/content/posts/python-speedup/code/.gitignore b/content/posts/python-speedup/code/.gitignore new file mode 100644 index 0000000..5b4e973 --- /dev/null +++ b/content/posts/python-speedup/code/.gitignore @@ -0,0 +1,25 @@ +# Created by https://gitignore.org +# CMake.gitignore +*.log +.cache/ +*.so + +CMakeLists.txt.user +CMakeCache.txt +CMakeFiles +CMakeScripts +Testing +Makefile +cmake_install.cmake +install_manifest.txt +compile_commands.json +CTestTestfile.cmake +_deps +CMakeUserPresets.json + +# CLion +# JetBrains specific template is maintained in a separate JetBrains.gitignore that can +# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore +# and can be added to the global gitignore or merged into this file. For a more nuclear +# option (not recommended) you can uncomment the following to ignore the entire idea folder. +#cmake-build-* diff --git a/content/posts/python-speedup/code/CMakeLists.txt b/content/posts/python-speedup/code/CMakeLists.txt new file mode 100644 index 0000000..267ac0f --- /dev/null +++ b/content/posts/python-speedup/code/CMakeLists.txt @@ -0,0 +1,34 @@ +cmake_minimum_required(VERSION 3.30) + +project( + pyspeed + VERSION 0.1.0 + DESCRIPTION "C++ extension for Python speedup" + LANGUAGES CXX +) + +file(GLOB_RECURSE SRCFILES "src/*.cpp") + +set(CMAKE_EXPORT_COMPILE_COMMANDS true) +set(CMAKE_EXECUTABLE_ENABLE_EXPORTS TRUE) + +set(CMAKE_CXX_STANDARD 23) +add_compile_options( + -Wall + -Wextra + -Wpedantic + -Wno-unused-parameter + -Wno-unused-value + -Wno-missing-field-initializers + -Wno-gnu-zero-variadic-macro-arguments + -Wno-narrowing + -Wno-pointer-arith + -Wno-clobbered + -fmacro-prefix-map=${CMAKE_SOURCE_DIR}/= +) + +find_package(PkgConfig REQUIRED) +pkg_check_modules(python REQUIRED IMPORTED_TARGET python3) + +add_library(speedup SHARED src/speedup.cpp) +target_link_libraries(speedup PkgConfig::python) diff --git a/content/posts/python-speedup/code/src/speedup.cpp b/content/posts/python-speedup/code/src/speedup.cpp new file mode 100644 index 0000000..296acaa --- /dev/null +++ b/content/posts/python-speedup/code/src/speedup.cpp @@ -0,0 +1,56 @@ +#include + +PyObject* my_stat_counter(PyObject* self, PyObject* args) { + size_t to_add; + if (!PyArg_ParseTuple(args, "k", &to_add)) { + return (PyObject*)NULL; + } + + // PyObject* state = Py_None; + auto mod_shit = (size_t*)PyModule_GetState(self); + if (*mod_shit == 0) { + *mod_shit = 1; + } + + *mod_shit += to_add; + + return PyLong_FromSize_t(*mod_shit); +} + +static PyMethodDef MyStatMethods[] = { + { + "my_stat_counter", + my_stat_counter, + METH_VARARGS, + "Python interface for speedup", + }, + {NULL, NULL, 0, NULL}, +}; + +#ifdef Py_mod_exec +static struct PyModuleDef_Slot mod_slots[] = { + {Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED}, + {Py_mod_gil, Py_MOD_GIL_NOT_USED}, + {0, NULL}, +}; +#endif + +static struct PyModuleDef mod_def = { + PyModuleDef_HEAD_INIT, /* m_base */ + "speedup", /* m_name */ + "Python interface for speedup module", /* m_doc */ + sizeof(size_t), /* m_size */ + MyStatMethods, /* m_methods */ +#ifdef Py_mod_exec + mod_slots, /* m_slots */ +#else + NULL, +#endif + NULL, /* m_traverse */ + NULL, /* m_clear */ + NULL, /* m_free */ +}; + +PyMODINIT_FUNC PyInit_libspeedup(void) { + return PyModuleDef_Init(&mod_def); +} diff --git a/content/posts/python-speedup/code/test.py b/content/posts/python-speedup/code/test.py new file mode 100644 index 0000000..17622a9 --- /dev/null +++ b/content/posts/python-speedup/code/test.py @@ -0,0 +1,35 @@ +from concurrent import interpreters +from libspeedup import my_stat_counter + + +def calc(): + my_stat_counter(2) + my_stat_counter(2) + a = my_stat_counter(2) + print(a) + + +def main(): + my_stat_counter(1) + my_stat_counter(1) + int1 = interpreters.create() + int2 = interpreters.create() + int3 = interpreters.create() + + t1 = int1.call_in_thread(calc) + t2 = int2.call_in_thread(calc) + t3 = int3.call_in_thread(calc) + + t1.join() + t2.join() + t3.join() + + int1.close() + int2.close() + int3.close() + + print(my_stat_counter(1)) + + +if __name__ == "__main__": + main() diff --git a/content/posts/python-speedup/index.md b/content/posts/python-speedup/index.md new file mode 100644 index 0000000..4d0d057 --- /dev/null +++ b/content/posts/python-speedup/index.md @@ -0,0 +1,17 @@ ++++ +title = "Python speedup with rust" +weight = 1 +date = "2025-09-02" + +[extra] +preview_image = "posts/kube-intro/preview.png" ++++ + + + +## Why care + +It's no secret that python is slow. Because it's interpreted language. +But there is a solution for that problem. + +Python has ABI for loading modules built with C.