There are undeniable benefits of binding Python and C++ to take advantage of the best features of both languages. This is especially relevant to the HEP and other scientific communities that have invested heavily in the C++ frameworks and are rapidly moving their data analyses to Python. Version 2 of Awkward Array, a Scikit-HEP Python library, introduces a set of header-only C++ libraries that do not depend on any application binary interface. Users can directly include these libraries in their compilation rather than linking against platform-specific libraries. This new development makes the integration of Awkward Arrays into other projects easier and more portable as the implementation is easily separable from the rest of the Awkward Array codebase. The code is minimal, it does not include all of the code needed to use Awkward Arrays in Python, nor does it include references to Python or pybind11. The C++ users can use it to make arrays and then copy them to Python without any specialized data types - only raw buffers, strings, and integers. This C++ code also simplifies the process of just-in-time (JIT) compilation in ROOT. This implementation approach solves some of the drawbacks, like packaging projects where native dependencies can be challenging. In this paper, we demonstrate the technique to integrate C++ and Python by using a header-only approach. We also describe the implementation of a new LayoutBuilder and a GrowableBuffer. Furthermore, examples of wrapping the C++ data into Awkward Arrays and exposing Awkward Arrays to C++ without copying them are discussed.
翻译:将Python与C++绑定以充分利用两种语言的最佳特性,其益处毋庸置疑。这对高能物理(HEP)及在C++框架投入大量资源、并正迅速将数据分析迁移至Python的其他科学领域尤为重要。Scikit-HEP Python库的Awkward Array 2.0版本引入了一套纯头文件的C++库,该库不依赖任何应用程序二进制接口。用户可直接将这些库包含到编译中,无需链接特定于平台的库。这一新开发使Awkward Arrays能够更简便、更可移植地集成到其他项目中,因为其实现可轻松与Awkward Array代码库的其他部分分离。该代码极为精简,不包含在Python中使用Awkward Array所需的全部代码,也不涉及对Python或pybind11的引用。C++用户可用其创建数组,然后将其复制到Python——整个过程无需任何特殊数据类型,只需原始缓冲区、字符串和整数。这份C++代码还简化了ROOT中的即时(JIT)编译流程。这种实现方法解决了部分痛点,例如对原生依赖有挑战性的项目打包问题。本文演示了通过纯头文件方法集成C++与Python的技术。我们还介绍了新型LayoutBuilder和GrowableBuffer的实现。此外,讨论了将C++数据封装为Awkward Arrays以及在不复制数据的情况下将Awkward Arrays暴露给C++的示例。