jvanstraten commented on a change in pull request #12116: URL: https://github.com/apache/arrow/pull/12116#discussion_r806689894
########## File path: cpp/src/arrow/memory_pool.cc ########## @@ -603,14 +643,109 @@ class BaseMemoryPoolImpl : public MemoryPool { stats_.UpdateAllocatedBytes(-size); } - void ReleaseUnused() override { Allocator::ReleaseUnused(); } + protected: + virtual Status AllocateImmutableZeros(int64_t size, uint8_t** out) { +#ifdef USE_MMAP_FOR_IMMUTABLE_ZEROS + if (size > 0) { + *out = static_cast<uint8_t*>(mmap( + nullptr, size, PROT_READ, MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, -1, 0)); + if (*out == MAP_FAILED) { + auto err = errno; + return Status::OutOfMemory("Failed to allocate zero buffer of size ", size, ": ", + strerror(err)); + } + return Status::OK(); + } +#endif + RETURN_NOT_OK(Allocate(size, out)); + std::memset(*out, 0, size); + return Status::OK(); + } + + void FreeImmutableZeros(uint8_t* buffer, int64_t size) override { +#ifdef USE_MMAP_FOR_IMMUTABLE_ZEROS + if (size > 0) { + munmap(buffer, size); + return; + } +#endif + Free(buffer, size); + } + + public: + Result<std::shared_ptr<Buffer>> GetImmutableZeros(int64_t size) override { + // Thread-safely get the current largest buffer of zeros. Review comment: > That is true. However, it shouldn't be a concern if we can ensure that the pages don't actually allocate physical memory (or almost none of it, such as `/dev/zero`). I don't think we can in general, unfortunately. I would have no idea how to do it on something as ubiquitous as Windows (or if it can be done at all), and I'm sure that in general there are more exotic operating systems and architectures that simply can't do it. Also, for 32-bit systems/builds (if Arrow supports those) virtual memory is also in relatively short supply. > However, changing the pointer requires use of dedicated atomic access functions: [...] Ah, great, those functions are exactly what I had missed! I improved my allocation algorithm accordingly here: e628688 -- 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: github-unsubscr...@arrow.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org