【问题标题】:how to enable memory mapping while reading feather file in c++如何在 C++ 中读取羽毛文件时启用内存映射
【发布时间】:2021-08-28 03:38:32
【问题描述】:

在 python 和 c++ 中读取相同的羽毛文件时,在 python 中,函数 pyarrow.feather.read_table() 表现得比我用于 c++ 的 API 好。当我进一步调查时,我发现主要区别是因为在 python 中 read_table() API 使用了一个名为 memory_map 的标志(默认设置为 true)。当我禁用此标志时,c++ API 的性能比 python 中的 read_table() 更好。现在正如它所暗示的那样,c++ 默认情况下不使用 memory_mapping,而是为了提高我想使用它的性能。请建议我为 c++ i 使用内存映射的方法,因为在可用于 c++ 的文档中没有找到任何感兴趣的 API。 我正在使用的代码是 -

#!/usr/bin/env python3
import pandas as pd
import pyarrow as pa
import pyarrow.parquet as pq
import pyarrow.feather
import time

for i in range(20):
    start_time = time.time()

    table = pyarrow.feather.read_table('data'+str(i + 1)+'.feather')
    end_time = time.time()
    print("Time taken to read file is : ",(end_time - start_time)*1000,"ms")

C++ 代码是 -

void read_feather_to_table(std::string path,std::shared_ptr<arrow::Table> *feather_table){
    std::shared_ptr <arrow::io::RandomAccessFile> input_file = file_system.OpenInputFile(path).ValueOrDie();
    std::shared_ptr <arrow::ipc::feather::Reader> feather_reader = arrow::ipc::feather::Reader::Open(input_file).ValueOrDie();
    auto t1 = std::chrono::high_resolution_clock::now();
    arrow::Status temp_status = feather_reader -> Read(feather_table);
    auto t2 = std::chrono::high_resolution_clock::now();
    auto ms_int = std::chrono::duration_cast<std::chrono::milliseconds>(t2 - t1);
    std::cout << "Time taken to read file is : "<< ms_int.count()<< "ms\n";
    return;
}

【问题讨论】:

    标签: python c++ pyarrow apache-arrow feather


    【解决方案1】:

    使用 arrow::io::MemoryMappedFile 代替文件系统 API。 (您可以使用arrow::io::MemoryMappedFile::Create 打开文件) 目前没有文件系统可以打开内存映射文件。 MemoryMappedFile 扩展了 arrow::io::InputStream,因此您可以将其传递给 arrow::ipc::feather::Reader::Open

    【讨论】:

    • 在将 MemoryMappedFile 传递给 arrow::ipc::feather::Reader::Open 时,遇到错误,提示“不是羽毛 V1 或箭头 IPC 文件”。现在是一个羽毛V2文件,但是这个文件不应该被接受为箭头IPC文件吗?
    • 另外我认为 create API 不能用于打开文件,因为它会创建一个新文件来覆盖前一个文件。我想要的是打开一个本地文件,那么在这种情况下我们该怎么办?
    • 使用内存映射来创建文件是完全不同的话题。你希望这个文件永远不会被持久化到磁盘上吗?在这种情况下,您可以只使用共享缓冲区吗?如果您尝试使用内存映射将文件持久保存到磁盘,那么我不会打扰。您可能会在那里看到任何好处。是的,已写入箭头 IPC 文件的文件应被接受为 IPC 文件。我不确定您为什么会收到该错误。欢迎您将其作为自己的问题提出,但我们可能需要您尝试阅读的内容的示例。
    猜你喜欢
    • 2021-06-12
    • 2019-05-03
    • 1970-01-01
    • 1970-01-01
    • 2020-11-06
    • 2017-01-24
    • 2015-09-07
    • 2020-11-13
    • 1970-01-01
    相关资源
    最近更新 更多