bkietz commented on code in PR #35769:
URL: https://github.com/apache/arrow/pull/35769#discussion_r1376284554


##########
go/arrow/datatype_viewheader.go:
##########
@@ -0,0 +1,138 @@
+// 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.
+
+package arrow
+
+import (
+       "bytes"
+       "unsafe"
+
+       "github.com/apache/arrow/go/v14/arrow/endian"
+       "github.com/apache/arrow/go/v14/arrow/internal/debug"
+       "github.com/apache/arrow/go/v14/arrow/memory"
+)
+
+const (
+       StringViewPrefixLen  = 4
+       stringViewInlineSize = 12
+)
+
+func IsViewInline(length int) bool {
+       return length < stringViewInlineSize
+}
+
+// ViewHeader is a variable length string (utf8) or byte slice with
+// a 4 byte prefix and inline optimization for small values (12 bytes
+// or fewer). This is similar to Go's standard string but limited by
+// a length of Uint32Max and up to the first four bytes of the string
+// are copied into the struct. This prefix allows failing comparisons
+// early and can reduce CPU cache working set when dealing with short
+// strings.
+//
+// There are two situations:
+//
+//             Entirely inlined string data
+//                     |----|------------|
+//                             ^    ^
+//                             |    |
+//                           size  inline string data, zero padded
+//
+//             Reference into buffer
+//                     |----|----|----|----|
+//                             ^    ^     ^     ^
+//                             |    |     |     |
+//                           size prefix buffer index and offset to 
out-of-line portion
+//
+// Adapted from TU Munich's UmbraDB [1], Velox, DuckDB.
+//
+// [1]: https://db.in.tum.de/~freitag/papers/p29-neumann-cidr20.pdf
+type ViewHeader struct {
+       size uint32
+       // the first 4 bytes of this are the prefix for the string
+       // if size <= StringHeaderInlineSize, then the entire string
+       // is in the data array and is zero padded.
+       // if size > StringHeaderInlineSize, the next 8 bytes are 2 uint32
+       // values which are the buffer index and offset in that buffer
+       // containing the full string.
+       data [stringViewInlineSize]byte
+}
+
+func (sh *ViewHeader) IsInline() bool {
+       return sh.size <= uint32(stringViewInlineSize)
+}
+
+func (sh *ViewHeader) Len() int { return int(sh.size) }
+func (sh *ViewHeader) Prefix() [StringViewPrefixLen]byte {
+       return *(*[4]byte)(unsafe.Pointer(&sh.data))
+}
+
+func (sh *ViewHeader) BufferIndex() uint32 {
+       return endian.Native.Uint32(sh.data[StringViewPrefixLen:])
+}
+
+func (sh *ViewHeader) BufferOffset() uint32 {
+       return endian.Native.Uint32(sh.data[StringViewPrefixLen+4:])
+}
+
+func (sh *ViewHeader) InlineBytes() (data []byte) {
+       debug.Assert(sh.IsInline(), "calling InlineBytes on non-inline 
StringHeader")

Review Comment:
   It just seemed that since `/Inline(Bytes|String)/` is always used inside `if 
IsInline {}`, it might be preferable to write `if inline := sh.InlineBytes(); 
inline != nil {}`



-- 
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