zeroshade commented on a change in pull request #11206:
URL: https://github.com/apache/arrow/pull/11206#discussion_r718718767



##########
File path: go/arrow/memory/cgo_allocator.go
##########
@@ -0,0 +1,108 @@
+// 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.
+
+// +build cgo
+// +build ccalloc
+
+package memory
+
+import (
+       "runtime"
+
+       cga "github.com/apache/arrow/go/arrow/memory/internal/cgoalloc"
+)
+
+// CgoArrowAllocator is an allocator which exposes the C++ memory pool class
+// from the Arrow C++ Library as an allocator for memory buffers to use in Go.
+// The build tag 'ccalloc' must be used in order to include it as it requires
+// linking against the arrow library.
+//
+// The primary reason to use this would be as an allocator when dealing with
+// exporting data across the cdata interface in order to ensure that the memory
+// is allocated safely on the C side so it can be held on the CGO side beyond
+// the context of a single function call. If the memory in use isn't allocated
+// on the C side, then it is not safe for any pointers to data to be held 
outside
+// of Go beyond the context of a single Cgo function call as it will be 
invisible
+// to the Go garbage collector and could potentially get moved without being 
updated.

Review comment:
       So that's also a possibility, rather than using the memorypool exposed 
by the arrow lib we could instead create our own allocator (or utilize a 
different one) that allocates memory using C always rather than the 
DefaultGoAllocator using Go-allocated memory. But that then changes some 
semantics in terms of memory usage and potential performance characteristics 
because CGO calls have a higher overhead. 
   
   If you aren't making calls to C or passing data around then using the Go 
allocator is perfectly fine and performant (though if you're doing *a lot* of 
allocations, there are a few libraries which have shown significant benefits to 
using things like jemalloc and manually managing the memory via C calls but 
this has to be done carefully to avoid paying large overheads in CGO calls and 
avoid introducing extra dependencies that may not be needed). 
   
   I can definitely see a future enhancement to make the default allocator do 
this, but I didn't want to change the default allocation behavior yet.




-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to