1
1
mirror of https://github.com/anoma/juvix.git synced 2024-12-12 04:43:18 +03:00
Juvix empowers developers to write code in a high-level, functional language, compile it to gas-efficient output VM instructions, and formally verify the safety of their contracts prior to deployment and execution.
Go to file
Paul Cadman 87c5f0af44
Improve performance of anomaEncode / anomaDecode in the Core evaluator (#2975)
This PR:

* Adds a new implementation of {decode, encode}ByteString functions,
used by anomaEncode and anomaDecode in the Core evaluator
* Adds property tests for roundtripping and benchmarks for the new
functions.

The old implementation used
[bitvec](https://hackage.haskell.org/package/bitvec) to manipulate the
ByteString. This was far too slow. The new implementation uses bit
operations directly on the input integer and ByteArray.

It's now possible to run
[anoma-app-patterns:`Tests/Swap.juvix`](https://github.com/anoma/anoma-app-patterns/blob/feature/tests/Tests/Swap.juvix)
to completion.

For encoding, if the size of the output integer exceeds 64 bits (and
therefore a BigInt must be used) then the new implementation has
quadratic time complexity in the number of input bytes if an
implementation of `ByteString -> Integer` is used as follows:

```
byteStringToIntegerLE :: ByteString -> Integer
byteStringToIntegerLE = BS.foldr (\b acc -> acc `shiftL` 8 .|. fromIntegral b) 0
```

```
byteStringToInteger' :: ByteString -> Integer
byteStringToInteger' = BS.foldl' (\acc b -> acc `shiftL` 8 .|. fromIntegral b) 0

```

I think this is because `shiftL` is expensive for large Integers. To
mitigate this I'm splitting the input ByteString into 1024 byte chunks
and processing each separately. Using this we get 100x speed up at
~0.25Mb input over the non-chunked approach and linear time-complexity
thereafter.

## Benchmarks

The benchmarks for encoding and decoding 250000 bytes:

```
 ByteString Encoding to/from integer
      encode bytes to integer:   OK
        59.1 ms ± 5.3 ms
      decode bytes from integer: OK
        338  ms ±  16 ms
```

The previous implementation would never complete for this input.

Benchmarks for encoding and decoding 2 * 250000 bytes:

```
    ByteString Encoding to/from integer
      encode bytes to integer:   OK
        121  ms ± 8.3 ms
      decode bytes from integer: OK
        651  ms ±  27 ms
```

Benchmarks for encoding and decoding 4 * 250000 bytes:

```
    ByteString Encoding to/from integer
      encode bytes to integer:   OK
        249  ms ±  17 ms
      decode bytes from integer: OK
        1.317 s ±  16 ms
```

---------

Co-authored-by: Lukasz Czajka <lukasz@heliax.dev>
2024-08-30 18:20:18 +01:00
.devcontainer Upgrade to ghc-9.8.2 (#2794) 2024-06-08 14:43:33 +02:00
.github Use ormolu from stackage in the CI (#2900) 2024-07-19 09:38:39 +01:00
app Improve compilation progress log (#2969) 2024-08-30 00:10:13 +02:00
assets Improve css of html documentation and allow different themes (#2931) 2024-08-02 16:16:33 +02:00
bench Fix benchmark runtime variant (#2740) 2024-04-22 11:09:27 +01:00
bench2 Improve performance of anomaEncode / anomaDecode in the Core evaluator (#2975) 2024-08-30 18:20:18 +01:00
examples Migrate old named application syntax (#2876) 2024-07-12 18:31:09 +02:00
gnuplot Automatized benchmarks (#1673) 2023-01-05 17:48:26 +01:00
include Export Juvix source code to latex (#2917) 2024-08-05 11:28:19 +02:00
juvix-stdlib@615a02c810 Monad and Applicative traits in juvix stdlib (#2979) 2024-08-30 15:07:29 +02:00
licenses Remove unused files (#2398) 2023-09-28 14:20:19 +02:00
runtime Add support for unsigned 8-bit integer type Byte (#2918) 2024-08-02 07:43:24 +01:00
scripts Support for Cairo builtins (#2718) 2024-04-16 19:01:30 +02:00
src Improve performance of anomaEncode / anomaDecode in the Core evaluator (#2975) 2024-08-30 18:20:18 +01:00
test Improve performance of anomaEncode / anomaDecode in the Core evaluator (#2975) 2024-08-30 18:20:18 +01:00
tests Monad and Applicative traits in juvix stdlib (#2979) 2024-08-30 15:07:29 +02:00
.clang-format Juvix C runtime (#1580) 2022-11-03 09:38:09 +01:00
.github_changelog_generator Update Changelog v0.2.3 🎉 2022-08-15 12:24:42 +02:00
.gitignore Rust runtime (#2782) 2024-05-22 12:26:51 +02:00
.gitmodules Rename MiniJuvix to Juvix (#259) 2022-07-08 13:59:45 +02:00
.hlint.yaml Add default arguments (#2408) 2023-10-10 23:28:06 +02:00
.pre-commit-config.yaml Add an if instruction to JuvixReg (#2855) 2024-06-26 19:08:33 +02:00
cabal.hie.yaml Automatized benchmarks (#1673) 2023-01-05 17:48:26 +01:00
cabal.project Upgrade to ghc-9.8.2 (#2794) 2024-06-08 14:43:33 +02:00
cabal.project.freeze Upgrade to ghc-9.8.2 (#2794) 2024-06-08 14:43:33 +02:00
CHANGELOG.md Release 0.6.5 (#2956) 2024-08-14 17:48:36 +01:00
cntlines.sh Remove Geb backend (#2886) 2024-07-11 15:45:52 +01:00
CONTRIBUTING.md Fix baseUrl for juvix docs in Doctor command (#2122) 2023-05-24 12:14:30 +02:00
justfile Use ormolu from stackage in the CI (#2900) 2024-07-19 09:38:39 +01:00
LICENSE.md Add new README and md files (#1904) 2023-03-21 20:01:48 +01:00
Makefile Logger (#2908) 2024-07-22 17:14:37 +02:00
package.yaml Improve performance of anomaEncode / anomaDecode in the Core evaluator (#2975) 2024-08-30 18:20:18 +01:00
README.md Fix tara url svg (#2406) 2023-09-28 19:05:05 +02:00
stack.hie.yaml Refactor hie.yaml and add entry in the readme (#1672) 2022-12-20 16:45:48 +01:00
stack.yaml Upgrade to ghc-9.8.2 (#2794) 2024-06-08 14:43:33 +02:00

Juvix

Tara the Juvix mascot

CI Status

The Juvix compiler CI

Nightly build, release and benchmark

Codebase
Open the Juvix Standard Lib in Github Codespace

This repository is specifically dedicated to the compiler of the Juvix programming language.

For any Juvix-related inquiries, we strongly advise visiting the following resources.

Our documentation offers an in-depth understanding of the Juvix programming language. It encompasses a language reference, examples, blog posts, and numerous other resources to facilitate effective use of Juvix.