-
Notifications
You must be signed in to change notification settings - Fork 265
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf(python): Optimize sequence buffer writing via the c-api (#2078)
## What does this PR do? Optimize the writing of float and bool type sequences via the C API ## Related issues ## Does this PR introduce any user-facing change? - [ ] Does this PR introduce any public API change? - [ ] Does this PR introduce any binary protocol compatibility change? ## Benchmark ``` fury_large_float_tuple: Mean +- std dev: [base] 32.8 ms +- 0.9 ms -> [opt] 28.4 ms +- 0.8 ms: 1.16x faster fury_large_boolean_tuple: Mean +- std dev: [base] 22.1 ms +- 0.2 ms -> [opt] 13.6 ms +- 0.6 ms: 1.63x faster Geometric mean: 1.37x faster ```
- Loading branch information
1 parent
8f71b54
commit 40a7b25
Showing
7 changed files
with
167 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
|
||
load("@rules_cc//cc:defs.bzl", "cc_library", "cc_test") | ||
load("@com_github_grpc_grpc//bazel:cython_library.bzl", "pyx_library") | ||
|
||
cc_library( | ||
name = "_pyfury", | ||
srcs = ["pyfury.cc"], | ||
hdrs = ["pyfury.h"], | ||
alwayslink=True, | ||
linkstatic=True, | ||
strip_include_prefix = "/cpp", | ||
deps = [ | ||
"//cpp/fury/util:fury_util", | ||
"@local_config_python//:python_headers", | ||
], | ||
visibility = ["//visibility:public"], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
#include "fury/python/pyfury.h" | ||
|
||
static PyObject **PySequenceGetItems(PyObject *collection) { | ||
if (PyList_CheckExact(collection)) { | ||
return ((PyListObject *)collection)->ob_item; | ||
} else if (PyTuple_CheckExact(collection)) { | ||
return ((PyTupleObject *)collection)->ob_item; | ||
} | ||
return nullptr; | ||
} | ||
|
||
namespace fury { | ||
int Fury_PyBooleanSequenceWriteToBuffer(PyObject *collection, Buffer *buffer, | ||
Py_ssize_t start_index) { | ||
PyObject **items = PySequenceGetItems(collection); | ||
if (items == nullptr) { | ||
return -1; | ||
} | ||
Py_ssize_t size = Py_SIZE(collection); | ||
for (Py_ssize_t i = 0; i < size; i++) { | ||
bool b = items[i] == Py_True; | ||
buffer->UnsafePut(start_index, b); | ||
start_index += sizeof(bool); | ||
} | ||
return 0; | ||
} | ||
|
||
int Fury_PyFloatSequenceWriteToBuffer(PyObject *collection, Buffer *buffer, | ||
Py_ssize_t start_index) { | ||
PyObject **items = PySequenceGetItems(collection); | ||
if (items == nullptr) { | ||
return -1; | ||
} | ||
Py_ssize_t size = Py_SIZE(collection); | ||
for (Py_ssize_t i = 0; i < size; i++) { | ||
auto *f = (PyFloatObject *)items[i]; | ||
buffer->UnsafePut(start_index, f->ob_fval); | ||
start_index += sizeof(double); | ||
} | ||
return 0; | ||
} | ||
} // namespace fury |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
#pragma once | ||
#include "Python.h" | ||
#include "fury/util/buffer.h" | ||
|
||
namespace fury { | ||
int Fury_PyBooleanSequenceWriteToBuffer(PyObject *collection, Buffer *buffer, | ||
Py_ssize_t start_index); | ||
int Fury_PyFloatSequenceWriteToBuffer(PyObject *collection, Buffer *buffer, | ||
Py_ssize_t start_index); | ||
} // namespace fury |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters