Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve efficiency of Natural <-> ByteString #3386

Merged
merged 5 commits into from
Apr 8, 2025
Merged

Conversation

janmasrovira
Copy link
Collaborator

@janmasrovira janmasrovira commented Apr 5, 2025

I've improved efficiency of encoding/decoding Natural to Bytestring.

With very big numbers, the gains are dramatic. With smaller numbers, the gains are more modest but still relevant.

Benchmarks

1,000,000 digits

Natural -> ByteString

      Shift based:    OK
        1.697 s ±  13 ms
      Word64 based:   OK
        327  μs ±  21 μs
      Word8 based:    OK
        21.7 ms ± 1.2 ms
      Cereal library: OK
        1.874 s ± 3.6 ms

ByteString -> Natural

    ByteString -> Natural
      Shift based:    OK
        13.7 ms ± 889 μs
      Word based:     OK
        671  μs ±  30 μs

500 digits

Natural -> ByteString

    Natural -> ByteString
      Shift based:    OK
        4.99 μs ± 456 ns
      Word64 based:   OK
        267  ns ±  16 ns
      Word8 based:    OK
        1.86 μs ± 170 ns
      Cereal library: OK
        9.72 μs ± 771 ns

ByteString -> Natural

    ByteString -> Natural
      Old:            OK
        4.31 μs ± 411 ns
      New:            OK
        219  ns ±  21 ns

Unfortunately, I haven't measured any gains by measuring the execution time of the nock pipeline.

@janmasrovira janmasrovira self-assigned this Apr 5, 2025
@janmasrovira janmasrovira changed the title Improve efficiency of Natural -> ByteString Improve efficiency of Natural <-> ByteString Apr 8, 2025
@janmasrovira janmasrovira force-pushed the natural-bytestring branch 3 times, most recently from 12d0d9c to 9a6c70b Compare April 8, 2025 10:58
@janmasrovira janmasrovira requested a review from lukaszcz April 8, 2025 14:47
@janmasrovira janmasrovira marked this pull request as ready for review April 8, 2025 14:47
@lukaszcz lukaszcz force-pushed the natural-bytestring branch from 6eb11b7 to 98d71a7 Compare April 8, 2025 15:21
@lukaszcz
Copy link
Collaborator

lukaszcz commented Apr 8, 2025

Unfortunately, I haven't measured any gains by measuring the execution time of the nock pipeline.

Me neither. But after precomputing the hashes, the time difference with compilation to cairo dropped to below 30%. So it's acceptable.

heliax@imac positive % hyperfine -w 1 "juvix compile anoma test001.juvix" "juvix compile cairo test001.juvix"
Benchmark 1: juvix compile anoma test001.juvix
  Time (mean ± σ):      1.448 s ±  0.046 s    [User: 2.232 s, System: 0.252 s]
  Range (min … max):    1.375 s …  1.535 s    10 runs
 
Benchmark 2: juvix compile cairo test001.juvix
  Time (mean ± σ):      1.247 s ±  0.024 s    [User: 1.971 s, System: 0.223 s]
  Range (min … max):    1.200 s …  1.278 s    10 runs
 
Summary
  juvix compile cairo test001.juvix ran
    1.16 ± 0.04 times faster than juvix compile anoma test001.juvix

heliax@imac positive % hyperfine -w 1 "juvix compile anoma test001.juvix --modular" "juvix compile cairo test001.juvix"
Benchmark 1: juvix compile anoma test001.juvix --modular
  Time (mean ± σ):      1.605 s ±  0.043 s    [User: 2.382 s, System: 0.268 s]
  Range (min … max):    1.553 s …  1.677 s    10 runs
 
Benchmark 2: juvix compile cairo test001.juvix
  Time (mean ± σ):      1.260 s ±  0.045 s    [User: 1.975 s, System: 0.222 s]
  Range (min … max):    1.200 s …  1.339 s    10 runs
 
Summary
  juvix compile cairo test001.juvix ran
    1.27 ± 0.06 times faster than juvix compile anoma test001.juvix --modular

Modular compilation slows things down, because we don't filter out unused definitions early in Core anymore. But not so much (and we can't do much about that):

heliax@imac positive % hyperfine -w 1 "juvix compile anoma test001.juvix --modular" "juvix compile anoma test001.juvix" 
Benchmark 1: juvix compile anoma test001.juvix --modular
  Time (mean ± σ):      1.474 s ±  0.037 s    [User: 2.268 s, System: 0.186 s]
  Range (min … max):    1.418 s …  1.543 s    10 runs
 
Benchmark 2: juvix compile anoma test001.juvix
  Time (mean ± σ):      1.310 s ±  0.038 s    [User: 2.024 s, System: 0.161 s]
  Range (min … max):    1.265 s …  1.381 s    10 runs
 
Summary
  juvix compile anoma test001.juvix ran
    1.13 ± 0.04 times faster than juvix compile anoma test001.juvix --modular

heliax@imac positive % hyperfine -w 1 -p "rm -rf .juvix-build" "juvix compile anoma test001.juvix --modular" "juvix compile anoma test001.juvix"
Benchmark 1: juvix compile anoma test001.juvix --modular
  Time (mean ± σ):      5.247 s ±  0.216 s    [User: 8.392 s, System: 0.352 s]
  Range (min … max):    5.046 s …  5.694 s    10 runs
 
Benchmark 2: juvix compile anoma test001.juvix
  Time (mean ± σ):      5.029 s ±  0.235 s    [User: 8.302 s, System: 0.374 s]
  Range (min … max):    4.820 s …  5.468 s    10 runs
 
Summary
  juvix compile anoma test001.juvix ran
    1.04 ± 0.06 times faster than juvix compile anoma test001.juvix --modular

@lukaszcz lukaszcz merged commit 58af0e1 into main Apr 8, 2025
4 checks passed
@lukaszcz lukaszcz deleted the natural-bytestring branch April 8, 2025 17:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants