You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@arrow.apache.org by uw...@apache.org on 2018/07/13 14:29:29 UTC

[arrow] branch master updated: ARROW-2838: [Python] Speed up PandasObjectIsNull

This is an automated email from the ASF dual-hosted git repository.

uwe pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/arrow.git


The following commit(s) were added to refs/heads/master by this push:
     new fa4f2aa  ARROW-2838: [Python] Speed up PandasObjectIsNull
fa4f2aa is described below

commit fa4f2aaa48fe6ceaf0969f3984a1a09f2a8b5c6d
Author: Antoine Pitrou <an...@python.org>
AuthorDate: Fri Jul 13 16:29:22 2018 +0200

    ARROW-2838: [Python] Speed up PandasObjectIsNull
    
    Results for microbenchmarks.PandasObjectIsNull.time_PandasObjectIsNull:
    
    * before:
    
    ```
                   ========= =============
                      type
                   --------- -------------
                      int     1.35±0.02ms
                     float    1.54±0.02ms
                     object     1.25±0ms
                    decimal    20.8±0.5ms
                   ========= =============
    ```
    
    * after:
    
    ```
                   ========= ============
                      type
                   --------- ------------
                      int      811±10μs
                     float     1.54±0ms
                     object    1.37±0ms
                    decimal   22.0±0.7ms
                   ========= ============
    ```
    
    Author: Antoine Pitrou <an...@python.org>
    
    Closes #2258 from pitrou/ARROW-2838-pandas-is-null-perf and squashes the following commits:
    
    0fb6898c <Antoine Pitrou> ARROW-2838:  Speed up PandasObjectIsNull
---
 cpp/src/arrow/python/helpers.cc | 30 ++++++++++++++++++++++++++++--
 cpp/src/arrow/python/helpers.h  |  2 +-
 2 files changed, 29 insertions(+), 3 deletions(-)

diff --git a/cpp/src/arrow/python/helpers.cc b/cpp/src/arrow/python/helpers.cc
index 862157e..99785c8 100644
--- a/cpp/src/arrow/python/helpers.cc
+++ b/cpp/src/arrow/python/helpers.cc
@@ -325,13 +325,39 @@ template Status CIntFromPython(PyObject*, uint16_t*, const std::string&);
 template Status CIntFromPython(PyObject*, uint32_t*, const std::string&);
 template Status CIntFromPython(PyObject*, uint64_t*, const std::string&);
 
+inline bool MayHaveNaN(PyObject* obj) {
+  // Some core types can be very quickly type-checked and do not allow NaN values
+#if PYARROW_IS_PY2
+  const int64_t non_nan_tpflags = Py_TPFLAGS_INT_SUBCLASS | Py_TPFLAGS_LONG_SUBCLASS |
+                                  Py_TPFLAGS_LIST_SUBCLASS | Py_TPFLAGS_TUPLE_SUBCLASS |
+                                  Py_TPFLAGS_STRING_SUBCLASS |
+                                  Py_TPFLAGS_UNICODE_SUBCLASS | Py_TPFLAGS_DICT_SUBCLASS |
+                                  Py_TPFLAGS_BASE_EXC_SUBCLASS | Py_TPFLAGS_TYPE_SUBCLASS;
+#else
+  const int64_t non_nan_tpflags = Py_TPFLAGS_LONG_SUBCLASS | Py_TPFLAGS_LIST_SUBCLASS |
+                                  Py_TPFLAGS_TUPLE_SUBCLASS | Py_TPFLAGS_BYTES_SUBCLASS |
+                                  Py_TPFLAGS_UNICODE_SUBCLASS | Py_TPFLAGS_DICT_SUBCLASS |
+                                  Py_TPFLAGS_BASE_EXC_SUBCLASS | Py_TPFLAGS_TYPE_SUBCLASS;
+#endif
+  return !PyType_HasFeature(Py_TYPE(obj), non_nan_tpflags);
+}
+
 bool PyFloat_IsNaN(PyObject* obj) {
   return PyFloat_Check(obj) && std::isnan(PyFloat_AsDouble(obj));
 }
 
 bool PandasObjectIsNull(PyObject* obj) {
-  return obj == Py_None || obj == numpy_nan || PyFloat_IsNaN(obj) ||
-         (internal::PyDecimal_Check(obj) && internal::PyDecimal_ISNAN(obj));
+  if (!MayHaveNaN(obj)) {
+    return false;
+  }
+  if (obj == Py_None) {
+    return true;
+  }
+  if (PyFloat_IsNaN(obj) ||
+      (internal::PyDecimal_Check(obj) && internal::PyDecimal_ISNAN(obj))) {
+    return true;
+  }
+  return false;
 }
 
 }  // namespace internal
diff --git a/cpp/src/arrow/python/helpers.h b/cpp/src/arrow/python/helpers.h
index ea3cc6a..9e73e3b 100644
--- a/cpp/src/arrow/python/helpers.h
+++ b/cpp/src/arrow/python/helpers.h
@@ -74,7 +74,7 @@ inline bool IsPyInteger(PyObject* obj) {
 // \brief Use pandas missing value semantics to check if a value is null
 bool PandasObjectIsNull(PyObject* obj);
 
-// \brief Check whether obj is nan
+// \brief Check whether obj is a floating-point NaN
 bool PyFloat_IsNaN(PyObject* obj);
 
 inline bool IsPyBinary(PyObject* obj) {