Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 101 additions & 0 deletions ci/performance-results/native-go-20260915.md

Large diffs are not rendered by default.

Binary file added ci/performance-results/native-go-20260915.zip
Binary file not shown.
1 change: 1 addition & 0 deletions d2cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -909,6 +909,7 @@ func _renderWithPNGEncoder(ctx context.Context, ms *xmain.State, opts d2svg.Rend
scale = go2.Pointer(1.)
}
renderOpts := &d2svg.RenderOpts{
Ruler: ruler,
Pad: opts.Pad,
Sketch: opts.Sketch,
Center: opts.Center,
Expand Down
60 changes: 60 additions & 0 deletions d2cli/svg_ruler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package d2cli

import (
"bytes"
"context"
"testing"
"time"

"github.com/d2lang/d2/d2graph"
"github.com/d2lang/d2/d2layouts/d2dagrelayout"
"github.com/d2lang/d2/d2lib"
"github.com/d2lang/d2/d2renderers/d2svg"
"github.com/d2lang/d2/lib/log"
"github.com/d2lang/d2/lib/textmeasure"
"github.com/d2lang/util-go/xmain"
)

func TestSVGOutputReusesCompilerRuler(t *testing.T) {
ruler, err := textmeasure.NewRuler()
if err != nil {
t.Fatal(err)
}
opts := d2svg.RenderOpts{}
diagram, _, err := d2lib.Compile(log.WithTB(context.Background(), t), "a: |md\n **Native** `SVG`\n|\na -> b\n", &d2lib.CompileOptions{
Ruler: ruler,
LayoutResolver: func(string) (d2graph.LayoutGraph, error) { return d2dagrelayout.DefaultLayout, nil },
}, &opts)
if err != nil {
t.Fatal(err)
}
want, err := d2svg.Render(diagram, &opts)
if err != nil {
t.Fatal(err)
}
dot := ruler.Dot
stdout := &controlledWriteCloser{limit: -1}
state := &xmain.TestState{
Run: func(ctx context.Context, ms *xmain.State) error {
_, _, err := _renderWithPNGEncoder(ctx, ms, opts, "input.d2", "-", false, false, ruler, diagram, SVG, "", false, nil)
return err
},
Args: []string{"d2"}, PWD: t.TempDir(), Stdout: stdout,
}
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
state.Start(t, ctx)
defer state.Cleanup(t)
if err := state.Wait(ctx); err != nil {
t.Fatal(err)
}
if !bytes.Equal(stdout.Bytes(), append(want, '\n')) {
t.Fatal("native CLI SVG differs from standalone rendering")
}
if ruler.Dot == dot {
t.Fatal("native CLI SVG did not use the compiler ruler")
}
if opts.Ruler != nil {
t.Fatal("native CLI mutated caller options")
}
}
5 changes: 3 additions & 2 deletions d2layouts/d2talalayout/internal/layoutgraph/node.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package layoutgraph

import (
"cmp"
"fmt"
"math"
"slices"
Expand Down Expand Up @@ -1060,8 +1061,8 @@ func sortNodesByID(nodes []*Node) {
if len(nodes) < 2 {
return
}
sort.Slice(nodes, func(i, j int) bool {
return nodes[i].entityID() < nodes[j].entityID()
slices.SortFunc(nodes, func(a, b *Node) int {
return cmp.Compare(a.entityID(), b.entityID())
})
}

Expand Down
85 changes: 85 additions & 0 deletions d2layouts/d2talalayout/internal/layoutgraph/node_sort_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package layoutgraph

import (
"fmt"
"math"
"math/rand"
"slices"
"sort"
"testing"
)

// Keep the previous comparator and reflection-based sort as an independent
// ordering oracle. Equal IDs need exact pointer-order parity, not just sorted IDs.
func legacySortNodesByID(nodes []*Node) {
if len(nodes) < 2 {
return
}
sort.Slice(nodes, func(i, j int) bool {
return nodes[i].entityID() < nodes[j].entityID()
})
}

func TestSortNodesByIDMatchesLegacyPointerOrder(t *testing.T) {
// Cover both sides of pdqsort's insertion-sort and pivot-selection thresholds,
// plus larger partitions with repeated IDs and pointers.
for _, size := range []int{0, 1, 2, 11, 12, 13, 48, 49, 50, 51, 127, 128, 257, 1024} {
for _, pattern := range []string{"sorted", "reverse", "shuffled", "all equal", "few IDs", "nil zero extrema repeated"} {
t.Run(fmt.Sprintf("%s/%d", pattern, size), func(t *testing.T) {
nodes := make([]*Node, size)
for i := range nodes {
nodes[i] = &Node{ID: int64(i) - int64(size/2)}
}
switch pattern {
case "reverse":
slices.Reverse(nodes)
case "shuffled":
rng := rand.New(rand.NewSource(int64(size)))
rng.Shuffle(len(nodes), func(i, j int) { nodes[i], nodes[j] = nodes[j], nodes[i] })
case "all equal":
for _, node := range nodes {
node.ID = 7
}
case "few IDs":
for i, node := range nodes {
node.ID = int64(i%5) - 2
}
case "nil zero extrema repeated":
zero := &Node{ID: 0}
minimum, maximum := &Node{ID: math.MinInt64}, &Node{ID: math.MaxInt64}
values := []*Node{nil, zero, maximum, minimum, {ID: 0}, {ID: -1}, zero, {ID: 1}, minimum, nil, maximum}
for i := range nodes {
nodes[i] = values[i%len(values)]
}
}
checkNodeSortPointerOrder(t, nodes)
})
}
}
}

func checkNodeSortPointerOrder(t *testing.T, input []*Node) {
t.Helper()
// Sort a subslice with writable capacity beyond its length. Neither sorting
// implementation may move the surrounding sentinels or change its slice header.
left, right := &Node{ID: math.MaxInt64}, &Node{ID: math.MinInt64}
gotBacking := make([]*Node, len(input)+4)
gotBacking[0], gotBacking[1] = left, nil
copy(gotBacking[2:], input)
gotBacking[len(gotBacking)-2], gotBacking[len(gotBacking)-1] = right, left
wantBacking := slices.Clone(gotBacking)
got := gotBacking[2 : len(gotBacking)-2]
want := wantBacking[2 : len(wantBacking)-2]
beforeLen, beforeCap := len(got), cap(got)
legacySortNodesByID(want)
sortNodesByID(got)
if len(got) != beforeLen || cap(got) != beforeCap {
t.Fatal("node slice header changed")
}
for i := range gotBacking {
if gotBacking[i] != wantBacking[i] {
t.Fatalf("pointer at backing index %d: got %p (ID %d), want %p (ID %d)",
i, gotBacking[i], gotBacking[i].entityID(), wantBacking[i], wantBacking[i].entityID())
}
}
}
Loading
Loading