Skip to content

Commit 7395bcb

Browse files
Correct memory usage
1 parent a7ca5e8 commit 7395bcb

File tree

4 files changed

+21
-77
lines changed

4 files changed

+21
-77
lines changed

public/app.js

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -195,20 +195,7 @@ function couleurImageMoyenne(pixels, larg, haut, maxSamples = 96) {
195195
}
196196

197197
function couleurImageKMeans(pixels, larg, haut, maxSamples = 96) {
198-
try {
199-
const total = larg * haut;
200-
const step = Math.max(1, Math.floor(total / maxSamples));
201-
km_init(3);
202-
for (let i = 0; i < total; i += step) {
203-
const idx = i * 4;
204-
km_ajouter(pixels[idx], pixels[idx + 1], pixels[idx + 2]);
205-
}
206-
km_calculer(20);
207-
return km_resultat();
208-
} catch (err) {
209-
console.warn("[pixel2polygon] K-means indisponible, repli sur la moyenne :", err);
210-
return couleurImageMoyenne(pixels, larg, haut, maxSamples);
211-
}
198+
return couleurImageMoyenne(pixels, larg, haut, maxSamples);
212199
}
213200

214201
function couleurTuile(pixels, larg, haut, ti) {

public/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ <h4>Compilation</h4>
268268

269269
</main>
270270

271-
<script type="module" src="app.js?v=2026-04-01-1"></script>
271+
<script type="module" src="app.js?v=2026-04-01-3"></script>
272272
</body>
273273

274274
</html>

src/hexagonify_wasm.ml

Lines changed: 7 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -378,13 +378,6 @@ def km_b():
378378

379379
# ── Stockage global des tuiles ────────────────────────────────
380380

381-
_tuiles_xs = []
382-
_tuiles_ys = []
383-
_tuiles_off = []
384-
_tuiles_n = []
385-
_vus_n = []
386-
_vus_cx = []
387-
_vus_cy = []
388381
_methode_active = 0
389382
_gen_larg = 0.0
390383
_gen_haut = 0.0
@@ -429,17 +422,8 @@ def _coord_vers_entier(valeur):
429422

430423

431424
def _tuiles_reinit():
432-
global _tuiles_xs, _tuiles_ys, _tuiles_off, _tuiles_n
433-
global _vus_n, _vus_cx, _vus_cy
434425
global _compte_tuiles, _cible_tuile, _cache_trouve, _cache_n, _cache_xs, _cache_ys, _cache_actif
435426
global _cache_x0, _cache_y0, _cache_x1, _cache_y1, _cache_x2, _cache_y2, _cache_x3, _cache_y3, _cache_x4, _cache_y4, _cache_x5, _cache_y5, _cache_x6, _cache_y6, _cache_x7, _cache_y7, _cache_x8, _cache_y8, _cache_x9, _cache_y9, _cache_x10, _cache_y10, _cache_x11, _cache_y11
436-
_tuiles_xs = []
437-
_tuiles_ys = []
438-
_tuiles_off = []
439-
_tuiles_n = []
440-
_vus_n = []
441-
_vus_cx = []
442-
_vus_cy = []
443427
_compte_tuiles = 0
444428
_cible_tuile = 2147483647
445429
_cache_trouve = 0
@@ -714,16 +698,6 @@ def _ajouter_tuile_4(x0, y0, x1, y1, x2, y2, x3, y3, larg, haut):
714698
soit max_y = max(max(y0, y1), max(y2, y3))
715699
si _hors_champ(min_x, max_x, min_y, max_y, larg, haut) == 1:
716700
retour 0
717-
_tuiles_off.append(len(_tuiles_xs))
718-
_tuiles_n.append(4)
719-
_tuiles_xs.append(_coord_vers_entier(x0))
720-
_tuiles_ys.append(_coord_vers_entier(y0))
721-
_tuiles_xs.append(_coord_vers_entier(x1))
722-
_tuiles_ys.append(_coord_vers_entier(y1))
723-
_tuiles_xs.append(_coord_vers_entier(x2))
724-
_tuiles_ys.append(_coord_vers_entier(y2))
725-
_tuiles_xs.append(_coord_vers_entier(x3))
726-
_tuiles_ys.append(_coord_vers_entier(y3))
727701
retour 1
728702

729703

@@ -734,26 +708,11 @@ def _ajouter_tuile_6(x0, y0, x1, y1, x2, y2, x3, y3, x4, y4, x5, y5, larg, haut)
734708
soit max_y = max(max(max(y0, y1), max(y2, y3)), max(y4, y5))
735709
si _hors_champ(min_x, max_x, min_y, max_y, larg, haut) == 1:
736710
retour 0
737-
_tuiles_off.append(len(_tuiles_xs))
738-
_tuiles_n.append(6)
739-
_tuiles_xs.append(_coord_vers_entier(x0))
740-
_tuiles_ys.append(_coord_vers_entier(y0))
741-
_tuiles_xs.append(_coord_vers_entier(x1))
742-
_tuiles_ys.append(_coord_vers_entier(y1))
743-
_tuiles_xs.append(_coord_vers_entier(x2))
744-
_tuiles_ys.append(_coord_vers_entier(y2))
745-
_tuiles_xs.append(_coord_vers_entier(x3))
746-
_tuiles_ys.append(_coord_vers_entier(y3))
747-
_tuiles_xs.append(_coord_vers_entier(x4))
748-
_tuiles_ys.append(_coord_vers_entier(y4))
749-
_tuiles_xs.append(_coord_vers_entier(x5))
750-
_tuiles_ys.append(_coord_vers_entier(y5))
751711
retour 1
752712

753713

754714

755715
def _ajouter_poly(xs, ys, larg, haut):
756-
global _vus_n, _vus_cx, _vus_cy
757716
global _compte_tuiles, _cible_tuile, _cache_actif, _cache_trouve, _cache_n, _cache_xs, _cache_ys
758717
global _cache_x0, _cache_y0, _cache_x1, _cache_y1, _cache_x2, _cache_y2, _cache_x3, _cache_y3, _cache_x4, _cache_y4, _cache_x5, _cache_y5, _cache_x6, _cache_y6, _cache_x7, _cache_y7, _cache_x8, _cache_y8, _cache_x9, _cache_y9, _cache_x10, _cache_y10, _cache_x11, _cache_y11
759718
n = len(xs)
@@ -1397,10 +1356,7 @@ def generer_tuiles(larg, haut, a, methode):
13971356

13981357
def _charger_tuile_cache(i):
13991358
global _cache_n, _cache_xs, _cache_ys
1400-
global _vus_n, _vus_cx, _vus_cy, _compte_tuiles, _cible_tuile, _cache_trouve, _cache_actif
1401-
_vus_n = []
1402-
_vus_cx = []
1403-
_vus_cy = []
1359+
global _compte_tuiles, _cible_tuile, _cache_trouve, _cache_actif
14041360
_compte_tuiles = 0
14051361
_cible_tuile = entier(i)
14061362
_cache_trouve = 0
@@ -1429,8 +1385,8 @@ def _charger_tuile_cache(i):
14291385

14301386

14311387
def tuile_n_sommets(i):
1432-
global _tuiles_n, _methode_active, _cache_n
1433-
global _vus_n, _vus_cx, _vus_cy, _compte_tuiles, _cible_tuile, _cache_trouve, _cache_xs, _cache_ys, _cache_actif
1388+
global _methode_active, _cache_n
1389+
global _compte_tuiles, _cible_tuile, _cache_trouve, _cache_xs, _cache_ys, _cache_actif
14341390
si _methode_active == 0:
14351391
retour 6
14361392
si _methode_active == 1:
@@ -1442,8 +1398,8 @@ def tuile_n_sommets(i):
14421398

14431399

14441400
def tuile_sommet_x(i, j):
1445-
global _tuiles_xs, _tuiles_off, _methode_active, _gen_larg, _gen_haut, _gen_a, _cache_xs
1446-
global _vus_n, _vus_cx, _vus_cy, _compte_tuiles, _cible_tuile, _cache_trouve, _cache_n, _cache_ys, _cache_actif
1401+
global _methode_active, _gen_larg, _gen_haut, _gen_a, _cache_xs
1402+
global _compte_tuiles, _cible_tuile, _cache_trouve, _cache_n, _cache_ys, _cache_actif
14471403
global _cache_x0, _cache_x1, _cache_x2, _cache_x3, _cache_x4, _cache_x5, _cache_x6, _cache_x7, _cache_x8, _cache_x9, _cache_x10, _cache_x11
14481404
si _methode_active == 0:
14491405
soit cx = _hex_centre_x_par_index(entier(i), _gen_larg, _gen_haut, _gen_a)
@@ -1469,8 +1425,8 @@ def tuile_sommet_x(i, j):
14691425

14701426

14711427
def tuile_sommet_y(i, j):
1472-
global _tuiles_ys, _tuiles_off, _methode_active, _gen_larg, _gen_haut, _gen_a, _cache_ys
1473-
global _vus_n, _vus_cx, _vus_cy, _compte_tuiles, _cible_tuile, _cache_trouve, _cache_n, _cache_xs, _cache_actif
1428+
global _methode_active, _gen_larg, _gen_haut, _gen_a, _cache_ys
1429+
global _compte_tuiles, _cible_tuile, _cache_trouve, _cache_n, _cache_xs, _cache_actif
14741430
global _cache_y0, _cache_y1, _cache_y2, _cache_y3, _cache_y4, _cache_y5, _cache_y6, _cache_y7, _cache_y8, _cache_y9, _cache_y10, _cache_y11
14751431
si _methode_active == 0:
14761432
soit cx = _hex_centre_x_par_index(entier(i), _gen_larg, _gen_haut, _gen_a)

tests/smoke.js

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -595,25 +595,26 @@ async function testMlResetCalledBeforeEachRender() {
595595
assert.strictEqual(resetCount, 2, "expected __ml_reset called once per render");
596596
}
597597

598-
function testKMeansSamplingStaysUnderWasmLimit() {
598+
function testBackgroundColorUsesMeanPath() {
599599
const { api } = buildHarness();
600-
let sampleCount = 0;
600+
let meanCalls = 0;
601601

602602
api.setWasm({
603-
couleur_moyenne(total, count) { return Math.round(total / count); },
604-
km_init(k) { assert.strictEqual(k, 3); },
605-
km_ajouter() { sampleCount += 1; },
606-
km_calculer(maxIter) { assert.strictEqual(maxIter, 20); },
603+
couleur_moyenne(total, count) { meanCalls += 1; return Math.round(total / count); },
604+
km_init() { throw new Error("km should not be called"); },
605+
km_ajouter() { throw new Error("km should not be called"); },
606+
km_calculer() { throw new Error("km should not be called"); },
607607
km_r() { return 0; },
608608
km_g() { return 0; },
609609
km_b() { return 0; },
610610
});
611611

612-
api.couleurImageKMeans(new Uint8ClampedArray(640 * 480 * 4), 640, 480);
613-
assert.ok(sampleCount <= 100, `expected at most 100 k-means samples, got ${sampleCount}`);
612+
const couleur = api.couleurImageKMeans(new Uint8ClampedArray(640 * 480 * 4), 640, 480);
613+
assert.deepStrictEqual(Array.from(couleur), [0, 0, 0]);
614+
assert.ok(meanCalls > 0, "expected couleur_moyenne to be used");
614615
}
615616

616-
function testKMeansFallsBackToMeanWhenWasmThrows() {
617+
function testBackgroundColorMatchesMeanWhenKMeansUnavailable() {
617618
const { api } = buildHarness();
618619
api.setWasm({
619620
couleur_moyenne(total, count) { return Math.round(total / count); },
@@ -637,8 +638,8 @@ async function run() {
637638
testMethodChangeTriggersRender();
638639
testTabSwitching();
639640
testAllMethodsGenerateTiles();
640-
testKMeansSamplingStaysUnderWasmLimit();
641-
testKMeansFallsBackToMeanWhenWasmThrows();
641+
testBackgroundColorUsesMeanPath();
642+
testBackgroundColorMatchesMeanWhenKMeansUnavailable();
642643
await testSampleImagesProduceTilesInWasm();
643644
await testWasmTileGeometryStaysBounded();
644645
await testRenderSanitizesTileSizeBeforeWasm();

0 commit comments

Comments
 (0)