Susan Potter
software: Created / Updated

Software Optimization Through Property-Based Thinking

In my last post on performance optimizations we looked at a variety of mental models to help us optimize software in different ways. Today we will review a valuable yet underutilized mental framework to approach optimization via property-based thinking . This revolves around the idea of defining, understanding, and preserving core properties that govern the behavior of our systems.

In this article, we’ll explore how property-based thinking provides a rigorous mental model for optimization. By linking properties with performance improvements, we’ll see how to build systems that are faster, leaner, and more reliable—all without losing sight of their essential invariants. Let’s dive in.


Properties as Optimization Invariants

At the heart of property-based thinking is a simple but powerful idea: properties act as invariants. When optimizing a system, these properties remain true before and after the transformation. This principle provides a safety net for correctness, enabling optimizations that might otherwise feel risky.

For instance, if a system’s behavior satisfies properties like idempotency, commutativity, or associativity, these can be leveraged to guide performance improvements without fear of breaking the system.

Between Abstraction and Performance

Properties sit comfortably at the intersection of abstraction and concrete implementation. They offer a vocabulary for reasoning about what’s essential to preserve (e.g., correctness) and what can change (e.g., execution order, data structure).

By formalizing properties as invariants, we unlock a treasure chest of optimization opportunities while safeguarding against introducing subtle bugs.


Key Property Types for Optimization

1. Algebraic Laws: The Workhorses of Optimization

Algebraic laws, derived from mathematics, play a starring role in optimization:

These properties are optimization engines. They allow us to safely reshape code to exploit modern hardware or improve resource usage.

2. Metamorphic Relations

Metamorphic relations describe relationships that must hold across different forms of computation, such as:

These relations act as correctness linchpins. They allow us to verify that optimizations preserve essential functionality even when the underlying mechanisms differ.

3. Stateful Migration Properties

In stateful systems, properties guide how to safely transition state while optimizing performance. Consider these use cases:

By focusing on stateful properties, we can optimize even the most complex systems with confidence.


Mental Models for Optimization

Data Structure Properties

As I mentioned in my previous post on performance choosing the right data structure is paramount to optimization. Properties provide a systematic way to evaluate options:

By understanding the properties of candidate structures, we can align our choices with both correctness and performance goals.

Parallel Execution Properties

Parallelism introduces complexity, but properties offer clarity:

  1. Correctness properties guarantee equivalence between sequential and parallel results:

    Property: Parallel Execution Equivalence  
    ∀ input i:  
      sequential_result(i) === parallel_result(i)  
    
  2. Resource properties define acceptable bounds:

    Property: Resource Bounds  
    ∀ execution e:  
      resourceUsage(e) ≤ maxBound ∧ span(e) ≤ targetSpan  
    

These properties provide a roadmap for harnessing concurrency without sacrificing correctness or predictability.

A Practical Optimization Workflow

Using property-based thinking, optimization follows a structured process:

  1. Define Core Properties:

    • Functional correctness (e.g., logical invariants).
    • Performance requirements (e.g., latency thresholds).
    • Resource usage (e.g., memory or CPU bounds).
  2. Establish Measurement Properties:

    • Validate benchmarks and profiles.
    • Ensure statistical significance of results.
  3. Apply Optimization Transformations:

    • Use properties to guide rewrites.
    • Measure the impact of changes.
    • Document trade-offs for maintainability.
  4. Monitor Production Properties:

    • Track performance metrics in real-time.
    • Validate system invariants under load.
    • React to deviations proactively.

Beyond Testing: Properties in Production

Properties shine not just in testing but also in production. For example:

By extending property-based thinking into production, we ensure that our systems remain robust and performant over time.

In the end…

Property-based thinking provides an extraordinary mental model for optimization as well as testing. It enables us to:

By embracing this approach, we combine rigor with creativity, ensuring that every optimization is both of value and safe. Optimization is no longer guesswork—it becomes an elegant dance of properties, measurement, and purpose.

What are you waiting for? Let’s make our systems faster while satisfying relevant properties to guide our process.

If you enjoyed this content, please consider sharing this link with a friend, following my GitHub, Twitter/X or LinkedIn accounts, or subscribing to my RSS feed.