Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
08e65c6
fix(uint256): Change type definition of `uint256.Uint` type.
notJoon Nov 25, 2025
a44207a
test(uint256): Add runtime-based unit tests to measure function metrics
notJoon Nov 25, 2025
1445a7c
fix(uint256): Remove temporary values in fromDecimal loop
notJoon Nov 25, 2025
5a7284b
fix(uint256): Remove temporary values in Dec loop
notJoon Nov 25, 2025
75b69f0
fix(uint256): Define constants as values to remove pointer dereference
notJoon Nov 25, 2025
7f226e0
chore: Update global constant's name to floow convention
notJoon Nov 25, 2025
312748b
fix: zero, one function should return copied value's address
notJoon Nov 25, 2025
f8fcc11
fix: remove unecessary function calls
notJoon Nov 25, 2025
7e6fd37
perf(p/uint256): simplify `umulStep` and `umulHop`
notJoon Nov 25, 2025
5ee5e10
perf(uint256): optimize umul with length-aware multiplication
notJoon Nov 25, 2025
bd31537
test(p/int256): Add runtume metric test
notJoon Nov 26, 2025
ff4abf2
fix: remove usage of global pointers
notJoon Nov 26, 2025
6788b48
Merge branch 'main' into optimize-uint256
notJoon Nov 26, 2025
49020b2
chore: remove unused code
notJoon Nov 27, 2025
0c63075
chore: remove remaining dead codes
notJoon Nov 27, 2025
f9770dc
fix
notJoon Nov 27, 2025
c516982
fix: optimize constructors
notJoon Nov 27, 2025
512a04a
Merge branch 'main' into optimize-uint256
notJoon Nov 27, 2025
15ec6ef
fix: Caching repeated values
notJoon Nov 27, 2025
5dc0f90
Merge branch 'main' into optimize-uint256
notJoon Nov 27, 2025
eca6a24
fix: extract local variable from `Dec` function
notJoon Nov 27, 2025
c3f48b0
revert Dec
notJoon Nov 27, 2025
f830a6c
refactor: calculation value
jinoosss Nov 27, 2025
121958d
test: remove unused functions
jinoosss Nov 27, 2025
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
165 changes: 165 additions & 0 deletions contract/p/gnoswap/int256/runtime_metrics_test.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
package int256

import (
"runtime"
"strconv"
"strings"
"testing"
"time"
)

// readAllocatorBytes parses runtime.MemStats() into the current allocated bytes.
func readAllocatorBytes(t *testing.T) int64 {
t.Helper()
stats := runtime.MemStats()
if stats == "nil allocator" {
return 0
}
if !strings.HasPrefix(stats, "Allocator{") || !strings.HasSuffix(stats, "}") {
t.Fatalf("unexpected runtime.MemStats output: %q", stats)
}
body := strings.TrimSuffix(strings.TrimPrefix(stats, "Allocator{"), "}")
parts := strings.Split(body, ", ")
if len(parts) != 2 {
t.Fatalf("unexpected runtime.MemStats content: %q", stats)
}
var (
bytes int64
found bool
)
for _, part := range parts {
fields := strings.Split(part, ":")
if len(fields) != 2 {
t.Fatalf("unexpected runtime.MemStats pair %q", part)
}
if fields[0] == "bytes" {
val, err := strconv.ParseInt(fields[1], 10, 64)
if err != nil {
t.Fatalf("failed to parse bytes from %q: %v", part, err)
}
bytes = val
found = true
break
}
}
if !found {
t.Fatalf("bytes key not found in runtime.MemStats output: %q", stats)
}
return bytes
}

type MetricResult struct {
Name string
Iterations int
DurationNs int64
AllocDelta int64
}

// runMetric executes fn the given number of iterations while logging elapsed time and allocation deltas.
func runMetric(t *testing.T, name string, iterations int, fn func()) MetricResult {
t.Helper()

// Skip GC if allocator is nil to avoid panic
stats := runtime.MemStats()
if stats != "nil allocator" {
runtime.GC()
}
beforeBytes := readAllocatorBytes(t)
start := time.Now()
for i := 0; i < iterations; i++ {
fn()
}
elapsed := time.Since(start)
afterBytes := readAllocatorBytes(t)

return MetricResult{
Name: name,
Iterations: iterations,
DurationNs: elapsed.Nanoseconds(),
AllocDelta: afterBytes - beforeBytes,
}
}

func TestPublicFunctionRuntimeMetrics(t *testing.T) {
const iterations = 200
var results []MetricResult

// Test data
commonDecimal := "1234567890123456789012345678901234567890"

tests := []struct {
name string
run func(t *testing.T) MetricResult
}{
{
name: "FromDecimal",
run: func(t *testing.T) MetricResult {
return runMetric(t, "FromDecimal", iterations, func() {
if _, err := FromDecimal(commonDecimal); err != nil {
t.Fatalf("FromDecimal error: %v", err)
}
})
},
},
{
name: "SetString",
run: func(t *testing.T) MetricResult {
return runMetric(t, "SetString", iterations, func() {
var target Int
if _, err := target.SetString(commonDecimal); err != nil {
t.Fatalf("SetString error: %v", err)
}
})
},
},
{
name: "MustFromDecimal",
run: func(t *testing.T) MetricResult {
return runMetric(t, "MustFromDecimal", iterations, func() {
MustFromDecimal(commonDecimal)
})
},
},
{
name: "MaxInt256",
run: func(t *testing.T) MetricResult {
return runMetric(t, "MaxInt256", iterations, func() {
MaxInt256()
})
},
},
{
name: "MinInt256",
run: func(t *testing.T) MetricResult {
return runMetric(t, "MinInt256", iterations, func() {
MinInt256()
})
},
},
{
name: "ToString",
run: func(t *testing.T) MetricResult {
largeInt := MustFromDecimal("57896044618658097711785492504343953926634992332820282019728792003956564819967") // max int256
return runMetric(t, "ToString", iterations, func() {
_ = largeInt.ToString()
})
},
},
}

for i := range tests {
test := tests[i]
t.Run(test.name, func(t *testing.T) {
result := test.run(t)
results = append(results, result)
})
}

// Print results as markdown table
t.Log("\n## Runtime Metrics Results\n")
t.Log("| Function | Iterations | Duration (ns) | Alloc Delta (bytes) |")
t.Log("|----------|------------|---------------|---------------------|")
for _, result := range results {
t.Logf("| %s | %d | %d | %d |", result.Name, result.Iterations, result.DurationNs, result.AllocDelta)
}
}
3 changes: 0 additions & 3 deletions contract/p/gnoswap/uint256/_helper_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@ func shouldPanicWithMsg(t *testing.T, f func(), msg string) {

// for original tests
func parseUint(s string) *Uint {
if len(s) >= 2 && s[:2] == "0x" {
return MustFromHex(s)
}
return MustFromDecimal(s)
}

Expand Down
Loading
Loading