Merge commit '81396a5322a7a48764fcf254d5d933ba1e57bdc5' as 'kiwi'

This commit is contained in:
2024-02-11 15:32:50 -06:00
76 changed files with 13184 additions and 0 deletions

View File

@@ -0,0 +1,17 @@
Benchmarks for Kiwi
-------------------
Those benchmarks are mostly used to check the performance of kiwi depending on
different c++ data structure.
# C++
GCC must be installed first on your system (`build-essential` package with apt)
>>> ./build_and_run_bench.sh
# Python
Running these benchmarks require to install the perf module::
>>> python enaml_like_benchmarks.py

View File

@@ -0,0 +1,11 @@
#!/bin/bash
set -o errexit -o nounset # fail on error or on unset variables
# set default values if variables are unset
: "${CXX_COMPILER:=g++}"
: "${CXX_FLAGS:=-std=c++11}"
"$CXX_COMPILER" ${CXX_FLAGS} -O2 -Wall -pedantic -I.. enaml_like_benchmark.cpp -o run_bench
./run_bench

View File

@@ -0,0 +1,224 @@
/*-----------------------------------------------------------------------------
| Copyright (c) 2020, Nucleic Development Team.
|
| Distributed under the terms of the Modified BSD License.
|
| The full license is in the file LICENSE, distributed with this software.
|----------------------------------------------------------------------------*/
// Time updating an EditVariable in a set of constraints typical of enaml use.
#include <kiwi/kiwi.h>
#define ANKERL_NANOBENCH_IMPLEMENT
#include "nanobench.h"
using namespace kiwi;
void build_solver(Solver& solver, Variable& width, Variable& height)
{
// Create custom strength
double mmedium = strength::create(0.0, 1.0, 0.0, 1.25);
double smedium = strength::create(0.0, 100, 0.0);
// Create the variable
Variable left("left");
Variable top("top");
Variable contents_top("contents_top");
Variable contents_bottom("contents_bottom");
Variable contents_left("contents_left");
Variable contents_right("contents_right");
Variable midline("midline");
Variable ctleft("ctleft");
Variable ctheight("ctheight");
Variable cttop("cttop");
Variable ctwidth("ctwidth");
Variable lb1left("lb1left");
Variable lb1height("lb1height");
Variable lb1top("lb1top");
Variable lb1width("lb1width");
Variable lb2left("lb2left");
Variable lb2height("lb2height");
Variable lb2top("lb2top");
Variable lb2width("lb2width");
Variable lb3left("lb3left");
Variable lb3height("lb3height");
Variable lb3top("lb3top");
Variable lb3width("lb3width");
Variable fl1left("fl1left");
Variable fl1height("fl1height");
Variable fl1top("fl1top");
Variable fl1width("fl1width");
Variable fl2left("fl2left");
Variable fl2height("fl2height");
Variable fl2top("fl2top");
Variable fl2width("fl2width");
Variable fl3left("fl3left");
Variable fl3height("fl3height");
Variable fl3top("fl3top");
Variable fl3width("fl3width");
// Add the edit variables
solver.addEditVariable(width, strength::strong);
solver.addEditVariable(height, strength::strong);
// Add the constraints
Constraint constraints[] = {
(left + -0 >= 0) | strength::required,
(height + 0 == 0) | strength::medium,
(top + -0 >= 0) | strength::required,
(width + -0 >= 0) | strength::required,
(height + -0 >= 0) | strength::required,
(-top + contents_top + -10 == 0) | strength::required,
(lb3height + -16 == 0) | strength::strong,
(lb3height + -16 >= 0) | strength::strong,
(ctleft + -0 >= 0) | strength::required,
(cttop + -0 >= 0) | strength::required,
(ctwidth + -0 >= 0) | strength::required,
(ctheight + -0 >= 0) | strength::required,
(fl3left + -0 >= 0) | strength::required,
(ctheight + -24 >= 0) | smedium,
(ctwidth + -1.67772e+07 <= 0) | smedium,
(ctheight + -24 <= 0) | smedium,
(fl3top + -0 >= 0) | strength::required,
(fl3width + -0 >= 0) | strength::required,
(fl3height + -0 >= 0) | strength::required,
(lb1width + -67 == 0) | strength::weak,
(lb2width + -0 >= 0) | strength::required,
(lb2height + -0 >= 0) | strength::required,
(fl2height + -0 >= 0) | strength::required,
(lb3left + -0 >= 0) | strength::required,
(fl2width + -125 >= 0) | strength::strong,
(fl2height + -21 == 0) | strength::strong,
(fl2height + -21 >= 0) | strength::strong,
(lb3top + -0 >= 0) | strength::required,
(lb3width + -0 >= 0) | strength::required,
(fl1left + -0 >= 0) | strength::required,
(fl1width + -0 >= 0) | strength::required,
(lb1width + -67 >= 0) | strength::strong,
(fl2left + -0 >= 0) | strength::required,
(lb2width + -66 == 0) | strength::weak,
(lb2width + -66 >= 0) | strength::strong,
(lb2height + -16 == 0) | strength::strong,
(fl1height + -0 >= 0) | strength::required,
(fl1top + -0 >= 0) | strength::required,
(lb2top + -0 >= 0) | strength::required,
(-lb2top + lb3top + -lb2height + -10 == 0) | mmedium,
(-lb3top + -lb3height + fl3top + -10 >= 0) | strength::required,
(-lb3top + -lb3height + fl3top + -10 == 0) | mmedium,
(contents_bottom + -fl3height + -fl3top + -0 == 0) | mmedium,
(fl1top + -contents_top + 0 >= 0) | strength::required,
(fl1top + -contents_top + 0 == 0) | mmedium,
(contents_bottom + -fl3height + -fl3top + -0 >= 0) | strength::required,
(-left + -width + contents_right + 10 == 0) | strength::required,
(-top + -height + contents_bottom + 10 == 0) | strength::required,
(-left + contents_left + -10 == 0) | strength::required,
(lb3left + -contents_left + 0 == 0) | mmedium,
(fl1left + -midline + 0 == 0) | strength::strong,
(fl2left + -midline + 0 == 0) | strength::strong,
(ctleft + -midline + 0 == 0) | strength::strong,
(fl1top + 0.5 * fl1height + -lb1top + -0.5 * lb1height + 0 == 0) | strength::strong,
(lb1left + -contents_left + 0 >= 0) | strength::required,
(lb1left + -contents_left + 0 == 0) | mmedium,
(-lb1left + fl1left + -lb1width + -10 >= 0) | strength::required,
(-lb1left + fl1left + -lb1width + -10 == 0) | mmedium,
(-fl1left + contents_right + -fl1width + -0 >= 0) | strength::required,
(width + 0 == 0) | strength::medium,
(-fl1top + fl2top + -fl1height + -10 >= 0) | strength::required,
(-fl1top + fl2top + -fl1height + -10 == 0) | mmedium,
(cttop + -fl2top + -fl2height + -10 >= 0) | strength::required,
(-ctheight + -cttop + fl3top + -10 >= 0) | strength::required,
(contents_bottom + -fl3height + -fl3top + -0 >= 0) | strength::required,
(cttop + -fl2top + -fl2height + -10 == 0) | mmedium,
(-fl1left + contents_right + -fl1width + -0 == 0) | mmedium,
(-lb2top + -0.5 * lb2height + fl2top + 0.5 * fl2height + 0 == 0) | strength::strong,
(-contents_left + lb2left + 0 >= 0) | strength::required,
(-contents_left + lb2left + 0 == 0) | mmedium,
(fl2left + -lb2width + -lb2left + -10 >= 0) | strength::required,
(-ctheight + -cttop + fl3top + -10 == 0) | mmedium,
(contents_bottom + -fl3height + -fl3top + -0 == 0) | mmedium,
(lb1top + -0 >= 0) | strength::required,
(lb1width + -0 >= 0) | strength::required,
(lb1height + -0 >= 0) | strength::required,
(fl2left + -lb2width + -lb2left + -10 == 0) | mmedium,
(-fl2left + -fl2width + contents_right + -0 == 0) | mmedium,
(-fl2left + -fl2width + contents_right + -0 >= 0) | strength::required,
(lb3left + -contents_left + 0 >= 0) | strength::required,
(lb1left + -0 >= 0) | strength::required,
(0.5 * ctheight + cttop + -lb3top + -0.5 * lb3height + 0 == 0) | strength::strong,
(ctleft + -lb3left + -lb3width + -10 >= 0) | strength::required,
(-ctwidth + -ctleft + contents_right + -0 >= 0) | strength::required,
(ctleft + -lb3left + -lb3width + -10 == 0) | mmedium,
(fl3left + -contents_left + 0 >= 0) | strength::required,
(fl3left + -contents_left + 0 == 0) | mmedium,
(-ctwidth + -ctleft + contents_right + -0 == 0) | mmedium,
(-fl3left + contents_right + -fl3width + -0 == 0) | mmedium,
(-contents_top + lb1top + 0 >= 0) | strength::required,
(-contents_top + lb1top + 0 == 0) | mmedium,
(-fl3left + contents_right + -fl3width + -0 >= 0) | strength::required,
(lb2top + -lb1top + -lb1height + -10 >= 0) | strength::required,
(-lb2top + lb3top + -lb2height + -10 >= 0) | strength::required,
(lb2top + -lb1top + -lb1height + -10 == 0) | mmedium,
(fl1height + -21 == 0) | strength::strong,
(fl1height + -21 >= 0) | strength::strong,
(lb2left + -0 >= 0) | strength::required,
(lb2height + -16 >= 0) | strength::strong,
(fl2top + -0 >= 0) | strength::required,
(fl2width + -0 >= 0) | strength::required,
(lb1height + -16 >= 0) | strength::strong,
(lb1height + -16 == 0) | strength::strong,
(fl3width + -125 >= 0) | strength::strong,
(fl3height + -21 == 0) | strength::strong,
(fl3height + -21 >= 0) | strength::strong,
(lb3height + -0 >= 0) | strength::required,
(ctwidth + -119 >= 0) | smedium,
(lb3width + -24 == 0) | strength::weak,
(lb3width + -24 >= 0) | strength::strong,
(fl1width + -125 >= 0) | strength::strong,
};
for (const auto& constraint : constraints)
solver.addConstraint(constraint);
}
int main()
{
ankerl::nanobench::Bench().run("building solver", [&] {
Solver solver;
Variable width("width");
Variable height("height");
build_solver(solver, width, height);
ankerl::nanobench::doNotOptimizeAway(solver); //< prevent the compiler to optimize away the solver
});
struct Size
{
int width;
int height;
};
Size sizes[] = {
{ 400, 600 },
{ 600, 400 },
{ 800, 1200 },
{ 1200, 800 },
{ 400, 800 },
{ 800, 400 }
};
Solver solver;
Variable widthVar("width");
Variable heightVar("height");
build_solver(solver, widthVar, heightVar);
for (const Size& size : sizes)
{
double width = size.width;
double height = size.height;
ankerl::nanobench::Bench().minEpochIterations(10).run("suggest value " + std::to_string(size.width) + "x" + std::to_string(size.height), [&] {
solver.suggestValue(widthVar, width);
solver.suggestValue(heightVar, height);
solver.updateVariables();
});
}
}

View File

@@ -0,0 +1,206 @@
# --------------------------------------------------------------------------------------
# Copyright (c) 2019, Nucleic Development Team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file LICENSE, distributed with this software.
# --------------------------------------------------------------------------------------
"""Time updating an EditVariable in a set of constraints typical of enaml use.
"""
import perf
from kiwisolver import Solver, Variable, strength
solver = Solver()
# Create custom strength
mmedium = strength.create(0, 1, 0, 1.25)
smedium = strength.create(0, 100, 0)
# Create the variable
left = Variable("left")
height = Variable("height")
top = Variable("top")
width = Variable("width")
contents_top = Variable("contents_top")
contents_bottom = Variable("contents_bottom")
contents_left = Variable("contents_left")
contents_right = Variable("contents_right")
midline = Variable("midline")
ctleft = Variable("ctleft")
ctheight = Variable("ctheight")
cttop = Variable("cttop")
ctwidth = Variable("ctwidth")
lb1left = Variable("lb1left")
lb1height = Variable("lb1height")
lb1top = Variable("lb1top")
lb1width = Variable("lb1width")
lb2left = Variable("lb2left")
lb2height = Variable("lb2height")
lb2top = Variable("lb2top")
lb2width = Variable("lb2width")
lb3left = Variable("lb3left")
lb3height = Variable("lb3height")
lb3top = Variable("lb3top")
lb3width = Variable("lb3width")
fl1left = Variable("fl1left")
fl1height = Variable("fl1height")
fl1top = Variable("fl1top")
fl1width = Variable("fl1width")
fl2left = Variable("fl2left")
fl2height = Variable("fl2height")
fl2top = Variable("fl2top")
fl2width = Variable("fl2width")
fl3left = Variable("fl3left")
fl3height = Variable("fl3height")
fl3top = Variable("fl3top")
fl3width = Variable("fl3width")
# Add the edit variables
solver.addEditVariable(width, "strong")
solver.addEditVariable(height, "strong")
# Add the constraints
for c in [
(left + -0 >= 0) | "required",
(height + 0 == 0) | "medium",
(top + -0 >= 0) | "required",
(width + -0 >= 0) | "required",
(height + -0 >= 0) | "required",
(-top + contents_top + -10 == 0) | "required",
(lb3height + -16 == 0) | "strong",
(lb3height + -16 >= 0) | "strong",
(ctleft + -0 >= 0) | "required",
(cttop + -0 >= 0) | "required",
(ctwidth + -0 >= 0) | "required",
(ctheight + -0 >= 0) | "required",
(fl3left + -0 >= 0) | "required",
(ctheight + -24 >= 0) | smedium,
(ctwidth + -1.67772e07 <= 0) | smedium,
(ctheight + -24 <= 0) | smedium,
(fl3top + -0 >= 0) | "required",
(fl3width + -0 >= 0) | "required",
(fl3height + -0 >= 0) | "required",
(lb1width + -67 == 0) | "weak",
(lb2width + -0 >= 0) | "required",
(lb2height + -0 >= 0) | "required",
(fl2height + -0 >= 0) | "required",
(lb3left + -0 >= 0) | "required",
(fl2width + -125 >= 0) | "strong",
(fl2height + -21 == 0) | "strong",
(fl2height + -21 >= 0) | "strong",
(lb3top + -0 >= 0) | "required",
(lb3width + -0 >= 0) | "required",
(fl1left + -0 >= 0) | "required",
(fl1width + -0 >= 0) | "required",
(lb1width + -67 >= 0) | "strong",
(fl2left + -0 >= 0) | "required",
(lb2width + -66 == 0) | "weak",
(lb2width + -66 >= 0) | "strong",
(lb2height + -16 == 0) | "strong",
(fl1height + -0 >= 0) | "required",
(fl1top + -0 >= 0) | "required",
(lb2top + -0 >= 0) | "required",
(-lb2top + lb3top + -lb2height + -10 == 0) | mmedium,
(-lb3top + -lb3height + fl3top + -10 >= 0) | "required",
(-lb3top + -lb3height + fl3top + -10 == 0) | mmedium,
(contents_bottom + -fl3height + -fl3top + -0 == 0) | mmedium,
(fl1top + -contents_top + 0 >= 0) | "required",
(fl1top + -contents_top + 0 == 0) | mmedium,
(contents_bottom + -fl3height + -fl3top + -0 >= 0) | "required",
(-left + -width + contents_right + 10 == 0) | "required",
(-top + -height + contents_bottom + 10 == 0) | "required",
(-left + contents_left + -10 == 0) | "required",
(lb3left + -contents_left + 0 == 0) | mmedium,
(fl1left + -midline + 0 == 0) | "strong",
(fl2left + -midline + 0 == 0) | "strong",
(ctleft + -midline + 0 == 0) | "strong",
(fl1top + 0.5 * fl1height + -lb1top + -0.5 * lb1height + 0 == 0) | "strong",
(lb1left + -contents_left + 0 >= 0) | "required",
(lb1left + -contents_left + 0 == 0) | mmedium,
(-lb1left + fl1left + -lb1width + -10 >= 0) | "required",
(-lb1left + fl1left + -lb1width + -10 == 0) | mmedium,
(-fl1left + contents_right + -fl1width + -0 >= 0) | "required",
(width + 0 == 0) | "medium",
(-fl1top + fl2top + -fl1height + -10 >= 0) | "required",
(-fl1top + fl2top + -fl1height + -10 == 0) | mmedium,
(cttop + -fl2top + -fl2height + -10 >= 0) | "required",
(-ctheight + -cttop + fl3top + -10 >= 0) | "required",
(contents_bottom + -fl3height + -fl3top + -0 >= 0) | "required",
(cttop + -fl2top + -fl2height + -10 == 0) | mmedium,
(-fl1left + contents_right + -fl1width + -0 == 0) | mmedium,
(-lb2top + -0.5 * lb2height + fl2top + 0.5 * fl2height + 0 == 0) | "strong",
(-contents_left + lb2left + 0 >= 0) | "required",
(-contents_left + lb2left + 0 == 0) | mmedium,
(fl2left + -lb2width + -lb2left + -10 >= 0) | "required",
(-ctheight + -cttop + fl3top + -10 == 0) | mmedium,
(contents_bottom + -fl3height + -fl3top + -0 == 0) | mmedium,
(lb1top + -0 >= 0) | "required",
(lb1width + -0 >= 0) | "required",
(lb1height + -0 >= 0) | "required",
(fl2left + -lb2width + -lb2left + -10 == 0) | mmedium,
(-fl2left + -fl2width + contents_right + -0 == 0) | mmedium,
(-fl2left + -fl2width + contents_right + -0 >= 0) | "required",
(lb3left + -contents_left + 0 >= 0) | "required",
(lb1left + -0 >= 0) | "required",
(0.5 * ctheight + cttop + -lb3top + -0.5 * lb3height + 0 == 0) | "strong",
(ctleft + -lb3left + -lb3width + -10 >= 0) | "required",
(-ctwidth + -ctleft + contents_right + -0 >= 0) | "required",
(ctleft + -lb3left + -lb3width + -10 == 0) | mmedium,
(fl3left + -contents_left + 0 >= 0) | "required",
(fl3left + -contents_left + 0 == 0) | mmedium,
(-ctwidth + -ctleft + contents_right + -0 == 0) | mmedium,
(-fl3left + contents_right + -fl3width + -0 == 0) | mmedium,
(-contents_top + lb1top + 0 >= 0) | "required",
(-contents_top + lb1top + 0 == 0) | mmedium,
(-fl3left + contents_right + -fl3width + -0 >= 0) | "required",
(lb2top + -lb1top + -lb1height + -10 >= 0) | "required",
(-lb2top + lb3top + -lb2height + -10 >= 0) | "required",
(lb2top + -lb1top + -lb1height + -10 == 0) | mmedium,
(fl1height + -21 == 0) | "strong",
(fl1height + -21 >= 0) | "strong",
(lb2left + -0 >= 0) | "required",
(lb2height + -16 >= 0) | "strong",
(fl2top + -0 >= 0) | "required",
(fl2width + -0 >= 0) | "required",
(lb1height + -16 >= 0) | "strong",
(lb1height + -16 == 0) | "strong",
(fl3width + -125 >= 0) | "strong",
(fl3height + -21 == 0) | "strong",
(fl3height + -21 >= 0) | "strong",
(lb3height + -0 >= 0) | "required",
(ctwidth + -119 >= 0) | smedium,
(lb3width + -24 == 0) | "weak",
(lb3width + -24 >= 0) | "strong",
(fl1width + -125 >= 0) | "strong",
]:
solver.addConstraint(c)
def bench_update_variables(loops, solver):
"""Suggest new values and update variables.
This mimic the use of kiwi in enaml in the case of a resizing.
"""
t0 = perf.perf_counter()
for w, h in [
(400, 600),
(600, 400),
(800, 1200),
(1200, 800),
(400, 800),
(800, 400),
] * loops:
solver.suggestValue(width, w)
solver.suggestValue(height, h)
solver.updateVariables()
return perf.perf_counter() - t0
runner = perf.Runner()
runner.bench_time_func(
"kiwi.suggestValue", bench_update_variables, solver, inner_loops=1
)

3359
kiwi/benchmarks/nanobench.h Normal file

File diff suppressed because it is too large Load Diff