Skip to content

Optimize C++ implementation #43

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

Closed
wants to merge 1 commit into from
Closed
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
20 changes: 12 additions & 8 deletions .github/workflows/cibuildwheel.yml
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ jobs:


steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
with:
submodules: recursive
- uses: actions/setup-python@v5
Expand All @@ -201,29 +201,32 @@ jobs:
python-version: '3.10'
- name: Set up QEMU
if: runner.os == 'Linux'
uses: docker/setup-qemu-action@v1
uses: docker/setup-qemu-action@v3
with:
platforms: all
- name: Build wheels
uses: pypa/cibuildwheel@v2.17.0
env:
CIBW_MANYLINUX_X86_64_IMAGE: ${{ matrix.manylinux_image }}
CIBW_BUILD: cp${{ matrix.python }}-${{ matrix.platform_id }}
CIBW_BEFORE_BUILD: pip install pybind11
CIBW_BEFORE_BUILD: |
pip install pybind11
sudo apt-get update && sudo apt-get install -y zlib1g-dev liblzma-dev
CIBW_TEST_COMMAND: pytest {project}/tests -vv
CIBW_TEST_REQUIRES: pytest numpy
CIBW_BUILD_VERBOSITY: 1
CIBW_ARCHS: ${{ matrix.arch }}
MACOSX_DEPLOYMENT_TARGET: ${{ matrix.macosx_deployment_target }}
- uses: actions/upload-artifact@v2
- uses: actions/upload-artifact@v4
with:
name: artifact-${{ matrix.os }}-py${{ matrix.python }}
path: ./wheelhouse/*.whl

build_sdist:
name: Build source distribution
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
with:
submodules: recursive
- uses: actions/setup-python@v5
Expand All @@ -232,8 +235,9 @@ jobs:
python-version: '3.10'
- name: Build sdist
run: python setup.py sdist
- uses: actions/upload-artifact@v2
- uses: actions/upload-artifact@v4
with:
name: artifact-sdist
path: dist/*.tar.gz

upload_pypi:
Expand All @@ -242,9 +246,9 @@ jobs:
# upload to PyPI on every tag starting with 'v'
if: github.event_name == 'push' && startsWith(github.event.ref, 'refs/tags/v')
steps:
- uses: actions/download-artifact@v2
- uses: actions/download-artifact@v4
with:
name: artifact
pattern: artifact-*
path: dist
- uses: pypa/gh-action-pypi-publish@v1.4.2
with:
Expand Down
30 changes: 15 additions & 15 deletions cpp/prtree.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,10 @@ auto list_list_to_arrays(vec<vec<T>> out_ll)
}
vec<T> out;
out.reserve(sum);
for (const auto &v : out_ll)
out.insert(out.end(), v.begin(), v.end());
for (auto &v : out_ll)
out.insert(out.end(),
std::make_move_iterator(v.begin()),
std::make_move_iterator(v.end()));

return make_tuple(
std::move(as_pyarray(out_s)),
Expand All @@ -104,14 +106,14 @@ static const float REBUILD_THRE = 1.25;
#define unlikely(x) (x)
#endif

std::string compress(std::string &data)
inline std::string compress(const std::string &data)
{
std::string output;
snappy::Compress(data.data(), data.size(), &output);
return output;
}

std::string decompress(std::string &data)
inline std::string decompress(const std::string &data)
{
std::string output;
snappy::Uncompress(data.data(), data.size(), &output);
Expand Down Expand Up @@ -702,25 +704,22 @@ class PRTreeElement
template <class T, int B = 6, int D = 2>
void bfs(const std::function<void(std::unique_ptr<PRTreeLeaf<T, B, D>> &)> &func, vec<PRTreeElement<T, B, D>> &flat_tree, const BB<D> target)
{
queue<size_t> que;
auto qpush_if_intersect = [&](const size_t &i)
vec<size_t> que;
que.reserve(flat_tree.size());
auto qpush_if_intersect = [&](size_t i)
{
PRTreeElement<T, B, D> &r = flat_tree[i];
// std::cout << "i " << (long int) i << " : " << (bool) r.leaf << std::endl;
if (r(target))
{
// std::cout << " is pushed" << std::endl;
que.emplace(i);
que.push_back(i);
}
};

// std::cout << "size: " << flat_tree.size() << std::endl;
qpush_if_intersect(0);
while (!que.empty())
size_t qhead = 0;
while (qhead < que.size())
{
size_t idx = que.front();
// std::cout << "idx: " << (long int) idx << std::endl;
que.pop();
size_t idx = que[qhead++];
PRTreeElement<T, B, D> &elem = flat_tree[idx];

if (elem.leaf)
Expand All @@ -733,7 +732,8 @@ void bfs(const std::function<void(std::unique_ptr<PRTreeLeaf<T, B, D>> &)> &func
for (size_t offset = 0; offset < B; offset++)
{
size_t jdx = idx * B + offset + 1;
qpush_if_intersect(jdx);
if (jdx < flat_tree.size())
qpush_if_intersect(jdx);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pytest==7.1.2
pybind11==2.9.0
cmake==3.22.4
cmake==3.22.4
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
numpy>=1.16,<2.0
numpy>=1.16,<2.0
3 changes: 2 additions & 1 deletion run_profile.sh
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/env bash
set -e

rm -rf build dist .pytest_cache
Expand All @@ -8,4 +9,4 @@ python docs/run_profile.py
so_path=src/python_prtree/PRTree.cpython-310-x86_64-linux-gnu.so
google-pprof --callgrind $so_path build.prof > cg_build.prof
google-pprof --callgrind $so_path find_all.prof > cg_find_all.prof
google-pprof --callgrind $so_path insert.prof > cg_insert.prof
google-pprof --callgrind $so_path insert.prof > cg_insert.prof
1 change: 1 addition & 0 deletions run_test.sh
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/env bash
set -e

rm -rf build dist .pytest_cache
Expand Down
Loading