Skip to content

WASM: Disable Gini parallelization on Emscripten - #935

Closed
russlan23 wants to merge 1 commit into
QuantEcon:mainfrom
russlan23:agent/gate-gini-parallel-emscripten
Closed

russlan23 wants to merge 1 commit into
QuantEcon:mainfrom
russlan23:agent/gate-gini-parallel-emscripten

Conversation

@russlan23

Copy link
Copy Markdown
Contributor

Closes #926

Summary

  • disable Numba's parallel accelerator for gini_coefficient on Emscripten;
  • keep the existing parallel path on native platforms.

The browser Numba runtime is single-threaded and cannot compile this library's only @njit(parallel=True) function. Selecting the decorator option from sys.platform lets prange use its serial semantics in JupyterLite without changing the public API or native behavior.

Validation

  • python -m pytest -q quantecon/tests/test_inequality.py — 4 passed
  • python -m flake8 --select=F401,F405,E231 quantecon — passed
  • Native Python 3.13: compiled target reports parallel=True
  • Simulated Emscripten import: compiled target reports parallel=False; gini_coefficient([1, 2, 3]) == 2/9
  • git diff --check — passed

Automated assistance was used to prepare and validate this change.

@russlan23
russlan23 marked this pull request as ready for review August 13, 2026 03:38
@mmcky

mmcky commented Aug 14, 2026

Copy link
Copy Markdown
Contributor

Thanks for this @russlan23, and welcome — this is a clean, well-validated first contribution. To be clear up front: the change does exactly what #926 asked for at the time you wrote it, right down to the diff the issue prescribed, and the validation you did (native vs simulated-Emscripten compiled target, plus the 2/9 check) is more thorough than most. The reason I'm closing it is that we changed the approach in the issue yesterday, after you'd already opened this. That's on our side, not yours.

The background is #702, an old PR to optimise gini_coefficient that I benchmarked while deciding whether to close it. That work showed the O(n²) pairwise double loop can be replaced with an O(n log n) closed form on sorted data, which removes parallel=True altogether rather than gating it. #926 has been retitled and rewritten to make that the primary fix, so the platform gate here would become dead code as soon as the rewrite lands.

The reason I don't want to merge this as an interim step is that the gate leaves browser users on a serial O(n²) path, and that's the environment where it hurts most. Measured natively (numba 0.62.1, numpy 2.3.5, Pareto(3) inputs), comparing what the gate compiles to on Emscripten against the rewrite:

n gated serial O(n²) rewrite O(n log n) ratio
1,000 0.8 ms 0.011 ms 77x
10,000 92.9 ms 0.521 ms 178x
50,000 2346 ms 3.13 ms 750x
100,000 9364 ms 6.91 ms 1354x

Those serial figures are native timings; WASM typically runs a further 2–3x slower, so a 100,000-element Gini in JupyterLite would be a 20–30 second call. It wouldn't crash, which is what this PR set out to fix, but it wouldn't really be usable either. The rewrite gets it to single-digit milliseconds and needs no platform branch at all.

If you'd like to take the rewrite, it would be very welcome — it's the same file and the same issue, #926 now carries the formula and acceptance criteria, and you clearly have the setup for validating it. No obligation at all if you'd rather pick up something else; there are other open items under the #925 umbrella. Either way, thanks for jumping on this one so quickly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

WASM: remove @njit(parallel=True) from gini_coefficient via an O(n log n) rewrite

2 participants