nebius/SWE-rebench-V2-PRs huggingface dataset:
Repository: godbus/dbus
Pull request #153 (2019-04-18 11:42:17)

Improve array memory allocations
#148 

jsouthworth: Thanks, this looks good so far. Are you looking for comments in this PR or do you think this chunk of work is complete enough to be merged. The reason I ask is there is a "// TODO: "s", "g", "o", "v", "a...":" in the sigByteSize function.
amenzhinsky: Removed the comment, forgot to do it before submitting this PR.

Patch:
diff --git a/decoder.go b/decoder.go
index c7fd4e4..ede9157 100644
--- a/decoder.go
+++ b/decoder.go
@@ -188,8 +188,14 @@ func (dec *decoder) decode(s string, depth int) interface{} {
 		if depth >= 64 {
 			panic(FormatError("input exceeds container depth limit"))
 		}
+		sig := s[1:]
 		length := dec.decode("u", depth).(uint32)
-		v := reflect.MakeSlice(reflect.SliceOf(typeFor(s[1:])), 0, 0)
+		// capacity can be determined only for fixed-size element types
+		var capacity int
+		if s := sigByteSize(sig); s != 0 {
+			capacity = int(length) / s
+		}
+		v := reflect.MakeSlice(reflect.SliceOf(typeFor(sig)), 0, capacity)
 		// Even for empty arrays, the correct padding must be included
 		align := alignment(typeFor(s[1:]))
 		if len(s) > 1 && s[1] == '(' {
@@ -227,6 +233,51 @@ func (dec *decoder) decode(s string, depth int) interface{} {
 	}
 }
 
+// sigByteSize tries to calculates size of the given signature in bytes.
+//
+// It returns zero when it can't, for example when it contains non-fixed size
+// types such as strings, maps and arrays that require reading of the transmitted
+// data, for that we would need to implement the unread method for Decoder first.
+func sigByteSize(sig string) int {
+	var total int
+	for offset := 0; offset < len(sig); {
+		switch sig[offset] {
+		case 'y':
+			total += 1
+			offset += 1
+		case 'n', 'q':
+			total += 2
+			offset += 1
+		case 'b', 'i', 'u', 'h':
+			total += 4
+			offset += 1
+		case 'x', 't', 'd':
+			total += 8
+			offset += 1
+		case '(':
+			i := 1
+			depth := 1
+			for i < len(sig[offset:]) && depth != 0 {
+				if sig[offset+i] == '(' {
+					depth++
+				} else if sig[offset+i] == ')' {
+					depth--
+				}
+				i++
+			}
+			s := sigByteSize(sig[offset+1 : offset+i-1])
+			if s == 0 {
+				return 0
+			}
+			total += s
+			offset += i
+		default:
+			return 0
+		}
+	}
+	return total
+}
+
 // A FormatError is an error in the wire format.
 type FormatError string
 
diff --git a/go.mod b/go.mod
index bdcd125..57014e4 100644
--- a/go.mod
+++ b/go.mod
@@ -1,1 +1,3 @@
 module github.com/godbus/dbus
+
+go 1.12


Test patch:
diff --git a/decoder_test.go b/decoder_test.go
index 4650b2e..2170b91 100644
--- a/decoder_test.go
+++ b/decoder_test.go
@@ -70,3 +70,19 @@ func TestDecodeArrayEmptyStruct(t *testing.T) {
 		t.Fatal(err)
 	}
 }
+
+func TestSigByteSize(t *testing.T) {
+	for sig, want := range map[string]int{
+		"b":       4,
+		"t":       8,
+		"(yy)":    2,
+		"(y(uu))": 9,
+		"(y(xs))": 0,
+		"s":       0,
+		"ao":      0,
+	} {
+		if have := sigByteSize(sig); have != want {
+			t.Errorf("sigByteSize(%q) = %d, want %d", sig, have, want)
+		}
+	}
+}
Repository: godbus/dbus
Pull request #174 (2019-08-16 22:25:55)

Support variants of structs
Fix for issue #173.

Enables support for storing variants that contain structs.

jsouthworth: Thanks for the patch. Can you expand the test cases to cover this as well?
andrew-hoff: Done!

Patch:
diff --git a/dbus.go b/dbus.go
index c6d0d3c..428923d 100644
--- a/dbus.go
+++ b/dbus.go
@@ -87,6 +87,7 @@ func setDest(dest, src reflect.Value) error {
 	}
 	if isVariant(src.Type()) && !isVariant(dest.Type()) {
 		src = getVariantValue(src)
+		return store(dest, src)
 	}
 	if !src.Type().ConvertibleTo(dest.Type()) {
 		return fmt.Errorf(


Test patch:
diff --git a/dbus_test.go b/dbus_test.go
new file mode 100644
index 0000000..26fcadf
--- /dev/null
+++ b/dbus_test.go
@@ -0,0 +1,24 @@
+package dbus
+
+import (
+	"testing"
+)
+
+type TestStruct struct {
+	TestInt int
+	TestStr string
+}
+
+func Test_VariantOfStruct(t *testing.T) {
+	tester := TestStruct{TestInt: 123, TestStr: "foobar"}
+	testerDecoded := []interface{}{123, "foobar"}
+	variant := MakeVariant(testerDecoded)
+	input := []interface{}{variant}
+	var output TestStruct
+	if err := Store(input, &output); err != nil {
+		t.Fatal(err)
+	}
+	if tester != output {
+		t.Fatalf("%v != %v\n", tester, output)
+	}
+}