Repository: segmentio/parquet-go
Pull request #241 (2022-06-16 04:26:53)

Rename byte array functions in the encoding/plain package
Minor change doing some naming and code cleanup on functions of the `encoding/plain` package.


Patch:
diff --git a/bloom.go b/bloom.go
index b2ea7ec..8918e54 100644
--- a/bloom.go
+++ b/bloom.go
@@ -177,7 +177,7 @@ func (splitBlockEncoding) EncodeByteArray(dst, src []byte) ([]byte, error) {
 	filter := bloom.MakeSplitBlockFilter(dst)
 	buffer := make([]uint64, 0, filterEncodeBufferSize)
 
-	err := plain.RangeByteArrays(src, func(value []byte) error {
+	err := plain.RangeByteArray(src, func(value []byte) error {
 		if len(buffer) == cap(buffer) {
 			filter.InsertBulk(buffer)
 			buffer = buffer[:0]
diff --git a/column_buffer.go b/column_buffer.go
index dfb07c3..f3587ac 100644
--- a/column_buffer.go
+++ b/column_buffer.go
@@ -1507,7 +1507,7 @@ func (col *byteArrayColumnBuffer) WriteByteArrays(values []byte) (int, error) {
 func (col *byteArrayColumnBuffer) writeByteArrays(values []byte) (count, bytes int, err error) {
 	baseCount, baseBytes := len(col.offsets), len(col.values)
 
-	err = plain.RangeByteArrays(values, func(value []byte) error {
+	err = plain.RangeByteArray(values, func(value []byte) error {
 		col.append(unsafecast.BytesToString(value))
 		return nil
 	})
diff --git a/column_index.go b/column_index.go
index efbc9eb..8df2702 100644
--- a/column_index.go
+++ b/column_index.go
@@ -693,13 +693,13 @@ func isMaxByteArrayValue(value []byte) bool {
 
 func splitByteArrays(data []byte) [][]byte {
 	length := 0
-	plain.RangeByteArrays(data, func([]byte) error {
+	plain.RangeByteArray(data, func([]byte) error {
 		length++
 		return nil
 	})
 	buffer := make([]byte, 0, len(data)-(4*length))
 	values := make([][]byte, 0, length)
-	plain.RangeByteArrays(data, func(value []byte) error {
+	plain.RangeByteArray(data, func(value []byte) error {
 		offset := len(buffer)
 		buffer = append(buffer, value...)
 		values = append(values, buffer[offset:])
diff --git a/encoding/plain/plain.go b/encoding/plain/plain.go
index 43191f1..cd4aeac 100644
--- a/encoding/plain/plain.go
+++ b/encoding/plain/plain.go
@@ -71,7 +71,7 @@ func (e *Encoding) EncodeDouble(dst, src []byte) ([]byte, error) {
 }
 
 func (e *Encoding) EncodeByteArray(dst []byte, src []byte) ([]byte, error) {
-	if err := ValidateByteArrays(src); err != nil {
+	if err := ValidateByteArray(src); err != nil {
 		return dst[:0], encoding.Error(e, err)
 	}
 	return append(dst[:0], src...), nil
@@ -124,7 +124,7 @@ func (e *Encoding) DecodeDouble(dst, src []byte) ([]byte, error) {
 }
 
 func (e *Encoding) DecodeByteArray(dst, src []byte) ([]byte, error) {
-	if err := ValidateByteArrays(src); err != nil {
+	if err := ValidateByteArray(src); err != nil {
 		return dst[:0], encoding.Error(e, err)
 	}
 	return append(dst[:0], src...), nil
@@ -246,8 +246,8 @@ const (
 	errTooLarge
 )
 
-func ValidateByteArrays(b []byte) error {
-	switch validateByteArrays(b) {
+func ValidateByteArray(b []byte) error {
+	switch validateByteArray(b) {
 	case errTooShort:
 		return ErrTooShort(len(b))
 	case errTooLarge:
@@ -257,7 +257,7 @@ func ValidateByteArrays(b []byte) error {
 	}
 }
 
-func RangeByteArrays(b []byte, do func([]byte) error) (err error) {
+func RangeByteArray(b []byte, do func([]byte) error) (err error) {
 	for len(b) > 0 {
 		var v []byte
 		if v, b, err = NextByteArray(b); err != nil {
diff --git a/encoding/plain/plain_amd64.go b/encoding/plain/plain_amd64.go
index 96efec1..b537771 100644
--- a/encoding/plain/plain_amd64.go
+++ b/encoding/plain/plain_amd64.go
@@ -3,4 +3,4 @@
 package plain
 
 //go:noescape
-func validateByteArrays([]byte) status
+func validateByteArray([]byte) status
diff --git a/encoding/plain/plain_amd64.s b/encoding/plain/plain_amd64.s
index 748470c..5f29fd7 100644
--- a/encoding/plain/plain_amd64.s
+++ b/encoding/plain/plain_amd64.s
@@ -2,45 +2,40 @@
 
 #include "textflag.h"
 
-// validateByteArrays is implemented in assembly because it is the bottleneck in
+// validateByteArray is implemented in assembly because it is the bottleneck in
 // many cases when encoding byte arrays. This version of the function yields
 // ~50% better throughput than the code generated by the Go compiler.
 //
-// func validateByteArrays([]byte) status
-TEXT ·validateByteArrays(SB), NOSPLIT, $0-32
+// func validateByteArray([]byte) status
+TEXT ·validateByteArray(SB), NOSPLIT, $0-32
     MOVQ arg_base+0(FP), AX
     MOVQ arg_len+8(FP), BX
 
     CMPQ BX, $0
     JE done
 
-    XORQ SI, SI
-loop:
-    MOVQ BX, CX
-    SUBQ SI, CX
-    CMPQ CX, $4
+    CMPQ BX, $4
     JB errTooShort
 
-    MOVLQZX (AX)(SI*1), DX
-    ADDQ $4, SI
-    SUBQ $4, CX
-    CMPQ DX, CX
-    JA errTooShort
-    CMPQ DX, $0x7FFFFFFF
+    ADDQ AX, BX // end
+loop:
+    MOVL (AX), CX
+
+    CMPL CX, $0x7FFFFFFF
     JA errTooLarge
 
-    ADDQ DX, SI
-    CMPQ SI, BX
-    JNE loop
+    LEAQ 4(AX)(CX*1), AX
+    CMPQ AX, BX
+    JA errTooShort
+    JB loop
 done:
-    MOVQ $0, CX
-    MOVQ CX, ret+24(FP)
+    XORQ AX, AX
+return:
+    MOVQ AX, ret+24(FP)
     RET
 errTooShort:
-    MOVQ $1, CX
-    MOVQ CX, ret+24(FP)
-    RET
+    MOVQ $1, AX
+    JMP return
 errTooLarge:
-    MOVQ $2, CX
-    MOVQ CX, ret+24(FP)
-    RET
+    MOVQ $2, AX
+    JMP return
diff --git a/encoding/plain/plain_purego.go b/encoding/plain/plain_purego.go
index 4b4e7ad..5f9a50d 100644
--- a/encoding/plain/plain_purego.go
+++ b/encoding/plain/plain_purego.go
@@ -2,7 +2,7 @@
 
 package plain
 
-func validateByteArrays(b []byte) status {
+func validateByteArray(b []byte) status {
 	for i := 0; i < len(b); {
 		r := len(b) - i
 		if r < ByteArrayLengthSize {


Test patch:
diff --git a/encoding/delta/length_byte_array_test.go b/encoding/delta/length_byte_array_test.go
index c7c14b6..32f7755 100644
--- a/encoding/delta/length_byte_array_test.go
+++ b/encoding/delta/length_byte_array_test.go
@@ -28,7 +28,7 @@ func TestDecodeLengthByteArray(t *testing.T) {
 	}
 
 	index := 0
-	err = plain.RangeByteArrays(dst, func(got []byte) error {
+	err = plain.RangeByteArray(dst, func(got []byte) error {
 		want := characters[:index%len(characters)]
 
 		if want != string(got) {
diff --git a/encoding/plain/plain_test.go b/encoding/plain/plain_test.go
index fa610dd..84eb7b3 100644
--- a/encoding/plain/plain_test.go
+++ b/encoding/plain/plain_test.go
@@ -33,14 +33,14 @@ func TestAppendBoolean(t *testing.T) {
 	}
 }
 
-func TestValidateByteArrays(t *testing.T) {
+func TestValidateByteArray(t *testing.T) {
 	t.Run("ok", func(t *testing.T) {
 		var b []byte
 		b = plain.AppendByteArrayString(b, "Hello")
 		b = plain.AppendByteArrayString(b, "World")
 		b = plain.AppendByteArrayString(b, "!")
 
-		if err := plain.ValidateByteArrays(b); err != nil {
+		if err := plain.ValidateByteArray(b); err != nil {
 			t.Error(err)
 		}
 	})
@@ -51,7 +51,7 @@ func TestValidateByteArrays(t *testing.T) {
 		b = plain.AppendByteArrayString(b, "World")
 		b = plain.AppendByteArrayString(b, "!")
 
-		if plain.ValidateByteArrays(b[:len(b)-1]) == nil {
+		if plain.ValidateByteArray(b[:len(b)-1]) == nil {
 			t.Error("expected non-nil error")
 		}
 	})