nandi/vflutter_ffipublic Fork 0
728acaab2594667f4998fc18812e7c1e2e4cc549
Commits
Clone
git clone https://git.rickub.com/nandi/vflutter_ffi.git
git clone ssh://git@rickub.com/nandi/vflutter_ffi.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

README.md · 121 lines · 4.3 KBmarkdown Blame HistoryRaw
Add a Mandelbrot example app, and a V kernel worth demonstrating 81540bb nandithebull 9h ago1# vflutter_ffi example
2
Add drag-to-pan 0ed90f3 nandithebull 9h ago3A Mandelbrot explorer whose pixels are computed in V. Drag to pan, scroll to
4zoom smoothly, tap to zoom in, right-click to zoom out — zoom is anchored at
5the cursor, and a drag covers less ground the deeper you are. Sliders
Add a Mandelbrot example app, and a V kernel worth demonstrating 81540bb nandithebull 9h ago6control the iteration cap and how many isolates the frame is split across;
7"Reset view" restores the framing without disturbing either.
8
9It exists to exercise the parts of the bridge the string demo cannot:
10
11* **Bulk data across the boundary.** Each frame is a full RGBA buffer, not a
12 string. Dart allocates it and `vf_mandelbrot` fills it in place, so no V
13 memory crosses the boundary and there is nothing to `vf_free`.
14* **Band parallelism.** The image splits into horizontal strips rendered on
15 separate isolates. Bands are independent by construction, and `-gc none`
16 means no stop-the-world phase to serialise them.
17* **An honest benchmark.** The same algorithm is written twice — in V
18 (`src/vflutter.v`) and in Dart (`lib/mandelbrot_dart.dart`) — and the
19 Benchmark button times both. The tests assert the two agree pixel-for-pixel,
20 which is what makes the comparison meaningful.
21
22## What the numbers actually say
23
24Measured here (800x600, maxIter 500, release build, 8-core Linux):
25
26| | time |
27|---|---|
28| V, one isolate | ~152 ms |
29| V, 4 isolates | ~65 ms |
30| Dart AOT, one isolate | ~165 ms |
31
32**V is not meaningfully faster than Dart at this.** For a scalar
33floating-point loop, V-through-C and Dart AOT land within ~10% of each other.
34The 2.5x comes from the parallel split, which Dart could do on its own without
35any FFI. Treat this app as a demonstration of the *bridge* — bulk buffers,
36caller-owned memory, safe multi-isolate calls — not as a speed claim for V.
37
38Reach for V here because you want to write the logic in V, or already have it
39in V, not because C-via-V is expected to outrun Dart AOT on arithmetic.
40
Raise the isolate ceiling to 32, and add the sweep that justifies it 59a731a nandithebull 9h ago41## How many isolates?
42
43The slider goes to 32, and past the core count is the right place to be. Bands
44cost very different amounts — rows crossing the set's interior run the full
45iteration cap, rows in open space escape almost immediately — and a frame is
46not done until its slowest band is. Over-decomposing lets a free core start the
47next small band instead of idling.
48
49Measured here (800x600 / 500 iterations, 8 logical cores):
50
51| tiles | best ms | speedup |
52|---|---|---|
53| serial | 153 | 1.00x |
54| 4 | 64 | 2.39x |
55| 8 | 43 | 3.56x |
56| 16 | 34 | 4.50x |
57| 32 | 29 | 5.28x |
58| 64 | 27 | 5.67x |
59
60Even at 32 bands the slowest is still 2.7x the mean, which is the remaining
61inefficiency — not isolate overhead. Spawning 24 isolates costs ~0.8 ms and
62moving the whole 1.83 MB frame back across them ~1.4 ms, against a ~29 ms
63frame. Run `just sweep` to get these numbers for your own machine.
64
Add a Mandelbrot example app, and a V kernel worth demonstrating 81540bb nandithebull 9h ago65## Run it
66
Add a justfile f7c3825 nandithebull 9h ago67```bash
68just mandelbrot
69```
70
71or, driving Flutter yourself:
72
Add a Mandelbrot example app, and a V kernel worth demonstrating 81540bb nandithebull 9h ago73```bash
74cd example
75flutter run -d linux
76```
77
78Only the Linux runner is generated. For the other platforms, generate their
79runner directories once first:
80
81```bash
82flutter create --platforms=android,ios,macos,windows .
83```
84
85`flutter create` leaves `lib/`, `test/` and `pubspec.yaml` alone. The native
86library is built by the plugin itself: CMake invokes `v` on Linux, Windows and
87Android; iOS and macOS compile the pre-generated C in `ios/Classes/`.
88
89## Tests
90
91`flutter test` runs on the host VM rather than in the app bundle, so it needs
92the native library on the loader path:
93
94```bash
95./tool/build.sh
96cd example && LD_LIBRARY_PATH=../build flutter test
97```
98
99The suite covers the zoom maths as a property (the point under the cursor stays
100put), the buffer contract, the band split matching a single-shot render, and
101the V and Dart kernels agreeing exactly.
102
103## Headless scripts
104
105No Flutter SDK needed — only `dart` and the host library. Run from the
106repository root, which resolves without Flutter:
107
108```bash
109./tool/build.sh
110LD_LIBRARY_PATH=build dart example/lib/fractal_bench.dart # V vs Dart timings
111LD_LIBRARY_PATH=build dart example/lib/main_test.dart # string round-trips
112LD_LIBRARY_PATH=build dart example/lib/memory_check.dart # RSS regression guard
113```
114
115For a like-for-like comparison with a release app, compile the benchmark AOT
116first — `dart run` uses the JIT:
117
118```bash
119dart compile exe example/lib/fractal_bench.dart -o /tmp/bench
120LD_LIBRARY_PATH=build /tmp/bench
121```