You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@datasketches.apache.org by GitBox <gi...@apache.org> on 2022/07/26 08:37:52 UTC

[GitHub] [datasketches-cpp] jmalkin opened a new pull request, #297: Add python serde support for varopt sketches

jmalkin opened a new pull request, #297:
URL: https://github.com/apache/datasketches-cpp/pull/297

   The immediate result is for varopt, but having this working should be an important part of adding tuple support.
   
   I'm least confident in the error recovery during deserialization. I think it's most likely that Python will throw an error which propagates through the c++ code. In this case I'm intercepting it to try to decrement reference counts for items we've already allocated but it's hard to tell if that's actually preventing a leak or not. Happy for suggestions on how to clean up that code.
   
   Also, fixed a size bug in varopt.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@datasketches.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@datasketches.apache.org
For additional commands, e-mail: commits-help@datasketches.apache.org


[GitHub] [datasketches-cpp] jmalkin commented on a diff in pull request #297: Add python serde support for varopt sketches

Posted by GitBox <gi...@apache.org>.
jmalkin commented on code in PR #297:
URL: https://github.com/apache/datasketches-cpp/pull/297#discussion_r946462690


##########
python/src/py_serde.cpp:
##########
@@ -0,0 +1,112 @@
+/*
+ * 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 <cstring>
+#include "memory_operations.hpp"
+
+#include "py_serde.hpp"
+
+#include <pybind11/pybind11.h>
+
+namespace py = pybind11;
+
+void init_serde(py::module& m) {
+  py::class_<datasketches::py_object_serde, datasketches::PyObjectSerDe /* <--- trampoline*/>(m, "PyObjectSerDe")
+    .def(py::init<>())
+    .def("get_size", &datasketches::py_object_serde::get_size, py::arg("item"),
+        "Returns the size in bytes of an item")
+    .def("to_bytes", &datasketches::py_object_serde::to_bytes, py::arg("item"),
+        "Retuns a bytes object with a serialized version of an item")
+    .def("from_bytes", &datasketches::py_object_serde::from_bytes, py::arg("data"), py::arg("offset"),
+        "Reads a bytes object starting from the given offest and returns a tuple of the reconstructed "
+        "object and the number of additional bytes read")
+    ;
+}    
+
+namespace datasketches {
+  size_t py_object_serde::size_of_item(const py::object& item) const {
+    return get_size(item);
+  }
+
+  size_t py_object_serde::serialize(void* ptr, size_t capacity, const py::object* items, unsigned num) const {
+    size_t bytes_written = 0;
+    pybind11::gil_scoped_acquire acquire;
+    for (unsigned i = 0; i < num; ++i) {
+      std::string bytes = to_bytes(items[i]); // implicit cast from py::bytes
+      check_memory_size(bytes_written + bytes.size(), capacity);
+      memcpy(ptr, bytes.c_str(), bytes.size());
+      ptr = static_cast<char*>(ptr) + bytes.size();
+      bytes_written += bytes.size();
+    }
+    pybind11::gil_scoped_release release;
+    return bytes_written;
+  }
+
+  size_t py_object_serde::deserialize(const void* ptr, size_t capacity, py::object* items, unsigned num) const {
+    size_t bytes_read = 0;
+    unsigned i = 0;
+    bool failure = false;
+    bool error_from_python = false;
+    pybind11::gil_scoped_acquire acquire;
+
+    // copy data into bytes only once
+    py::bytes bytes(static_cast<const char*>(ptr), capacity);
+    for (; i < num && !failure; ++i) {
+      py::tuple bytes_and_len;
+      try {
+        bytes_and_len = from_bytes(bytes, bytes_read);
+      } catch (py::error_already_set &e) {
+        failure = true;
+        error_from_python = true;
+        break;
+      }
+
+      // TODO: check bytes_and_len size is exactly 2?

Review Comment:
   Wasn't sure, so thx. Removed.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@datasketches.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@datasketches.apache.org
For additional commands, e-mail: commits-help@datasketches.apache.org


[GitHub] [datasketches-cpp] jmalkin merged pull request #297: Add python serde support for varopt sketches

Posted by GitBox <gi...@apache.org>.
jmalkin merged PR #297:
URL: https://github.com/apache/datasketches-cpp/pull/297


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@datasketches.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@datasketches.apache.org
For additional commands, e-mail: commits-help@datasketches.apache.org


[GitHub] [datasketches-cpp] coveralls commented on pull request #297: Add python serde support for varopt sketches

Posted by GitBox <gi...@apache.org>.
coveralls commented on PR #297:
URL: https://github.com/apache/datasketches-cpp/pull/297#issuecomment-1195195416

   ## Pull Request Test Coverage Report for [Build 2738143781](https://coveralls.io/builds/51156326)
   
   * **1** of **1**   **(100.0%)**  changed or added relevant line in **1** file are covered.
   * No unchanged relevant lines lost coverage.
   * Overall coverage increased (+**0.006%**) to **92.347%**
   
   ---
   
   
   
   |  Totals | [![Coverage Status](https://coveralls.io/builds/51156326/badge)](https://coveralls.io/builds/51156326) |
   | :-- | --: |
   | Change from base [Build 2713877899](https://coveralls.io/builds/51063674): |  0.006% |
   | Covered Lines: | 2196 |
   | Relevant Lines: | 2378 |
   
   ---
   ##### 💛  - [Coveralls](https://coveralls.io)
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@datasketches.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@datasketches.apache.org
For additional commands, e-mail: commits-help@datasketches.apache.org


[GitHub] [datasketches-cpp] jmalkin commented on a diff in pull request #297: Add python serde support for varopt sketches

Posted by GitBox <gi...@apache.org>.
jmalkin commented on code in PR #297:
URL: https://github.com/apache/datasketches-cpp/pull/297#discussion_r946462365


##########
python/src/py_serde.cpp:
##########
@@ -0,0 +1,112 @@
+/*
+ * 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 <cstring>
+#include "memory_operations.hpp"
+
+#include "py_serde.hpp"
+
+#include <pybind11/pybind11.h>
+
+namespace py = pybind11;

Review Comment:
   Fixed: they're all just `py::` now



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@datasketches.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@datasketches.apache.org
For additional commands, e-mail: commits-help@datasketches.apache.org


[GitHub] [datasketches-cpp] AlexanderSaydakov commented on a diff in pull request #297: Add python serde support for varopt sketches

Posted by GitBox <gi...@apache.org>.
AlexanderSaydakov commented on code in PR #297:
URL: https://github.com/apache/datasketches-cpp/pull/297#discussion_r946279362


##########
python/src/py_serde.cpp:
##########
@@ -0,0 +1,112 @@
+/*
+ * 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 <cstring>
+#include "memory_operations.hpp"
+
+#include "py_serde.hpp"
+
+#include <pybind11/pybind11.h>
+
+namespace py = pybind11;

Review Comment:
   pybind11 prefix is also used below



##########
python/src/py_serde.cpp:
##########
@@ -0,0 +1,112 @@
+/*
+ * 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 <cstring>
+#include "memory_operations.hpp"
+
+#include "py_serde.hpp"
+
+#include <pybind11/pybind11.h>
+
+namespace py = pybind11;
+
+void init_serde(py::module& m) {
+  py::class_<datasketches::py_object_serde, datasketches::PyObjectSerDe /* <--- trampoline*/>(m, "PyObjectSerDe")
+    .def(py::init<>())
+    .def("get_size", &datasketches::py_object_serde::get_size, py::arg("item"),
+        "Returns the size in bytes of an item")
+    .def("to_bytes", &datasketches::py_object_serde::to_bytes, py::arg("item"),
+        "Retuns a bytes object with a serialized version of an item")
+    .def("from_bytes", &datasketches::py_object_serde::from_bytes, py::arg("data"), py::arg("offset"),
+        "Reads a bytes object starting from the given offest and returns a tuple of the reconstructed "
+        "object and the number of additional bytes read")
+    ;
+}    
+
+namespace datasketches {
+  size_t py_object_serde::size_of_item(const py::object& item) const {
+    return get_size(item);
+  }
+
+  size_t py_object_serde::serialize(void* ptr, size_t capacity, const py::object* items, unsigned num) const {
+    size_t bytes_written = 0;
+    pybind11::gil_scoped_acquire acquire;
+    for (unsigned i = 0; i < num; ++i) {
+      std::string bytes = to_bytes(items[i]); // implicit cast from py::bytes
+      check_memory_size(bytes_written + bytes.size(), capacity);
+      memcpy(ptr, bytes.c_str(), bytes.size());
+      ptr = static_cast<char*>(ptr) + bytes.size();
+      bytes_written += bytes.size();
+    }
+    pybind11::gil_scoped_release release;
+    return bytes_written;
+  }
+
+  size_t py_object_serde::deserialize(const void* ptr, size_t capacity, py::object* items, unsigned num) const {
+    size_t bytes_read = 0;
+    unsigned i = 0;
+    bool failure = false;
+    bool error_from_python = false;
+    pybind11::gil_scoped_acquire acquire;
+
+    // copy data into bytes only once
+    py::bytes bytes(static_cast<const char*>(ptr), capacity);
+    for (; i < num && !failure; ++i) {
+      py::tuple bytes_and_len;
+      try {
+        bytes_and_len = from_bytes(bytes, bytes_read);
+      } catch (py::error_already_set &e) {
+        failure = true;
+        error_from_python = true;
+        break;
+      }
+
+      // TODO: check bytes_and_len size is exactly 2?

Review Comment:
   are you going to remove this? I am not sure the check is necessary.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@datasketches.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@datasketches.apache.org
For additional commands, e-mail: commits-help@datasketches.apache.org