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

[Clarity] concat should allow to concatenate more than 2 sequences at once. #3056

Open
LNow opened this issue Feb 19, 2022 · 6 comments
Open
Assignees

Comments

@LNow
Copy link

LNow commented Feb 19, 2022

Is your feature request related to a problem? Please describe.
I want to concatenate more than 2 sequences at once without using fold because it is to expensive (execution costs wise).

Describe the solution you'd like
concat function should take 2 or more sequences of the same type and returns a concatenated sequence of the same type, with the resulting sequence_len = sequence1_len + sequence2_len ...+ ... sequenceN_len.

Instead of this:

(concat
  (concat
    (concat 0x00 0x01)
    (concat 0x02 0x03
  )
  (concat
    (concat 0x04 0x05)
    (concat 0x05 0x07)
  )
)
;; or
(fold my-concat (list 0x00 0x01 0x02 0x03 0x04 0x05 0x06 0x07) 0x)
(define-private (my-concat (input (buff 1)) (output (buff 8)))
  (unwrap-panic (as-max-len? (concat output input) u8))
)

I'd like to be able to do this:

(concat  0x00 0x01 0x02 0x03 0x04 0x05 0x06 0x07)

Describe alternatives you've considered
It is quite easy to concatenate multiple sequences using fold, but this approach is quite expensive.

@jcnelson
Copy link
Member

Hey @LNow,

It is quite easy to concatenate multiple sequences using fold, but this approach is quite expensive.

What leads you to believe that (concat 0x00 0x01 0x02 0x03 0x04 0x05 0x06 0x07) will be significantly cheaper than the fold approach?

@LNow
Copy link
Author

LNow commented Feb 21, 2022

@jcnelson the answer to your question is quite simple. Current execution costs.

Using example I posted above.

Multiple nested concat:

+----------------------+----------+------------+
|                      | Consumed | Limit      |
+----------------------+----------+------------+
| Runtime              | 8337     | 5000000000 |
+----------------------+----------+------------+
| Read count           | 3        | 7750       |
+----------------------+----------+------------+
| Read length (bytes)  | 221      | 100000000  |
+----------------------+----------+------------+
| Write count          | 0        | 7750       |
+----------------------+----------+------------+
| Write length (bytes) | 0        | 15000000   |
+----------------------+----------+------------+

Fold:

+----------------------+----------+------------+
|                      | Consumed | Limit      |
+----------------------+----------+------------+
| Runtime              | 20130    | 5000000000 |
+----------------------+----------+------------+
| Read count           | 3        | 7750       |
+----------------------+----------+------------+
| Read length (bytes)  | 221      | 100000000  |
+----------------------+----------+------------+
| Write count          | 0        | 7750       |
+----------------------+----------+------------+
| Write length (bytes) | 0        | 15000000   |
+----------------------+----------+------------+

Of course the more elements we have to concat, multiple nested concat's consumes more Read length (bytes), but that is only because code becomes longer and longer. Runtime wise it is consistently 2x cheaper.

@jcnelson
Copy link
Member

One thing we could do, regardless of how we address this, is make it so the Clarity VM does not re-run the type checks (and associated runtime costs) on each application of the function in fold.

@njordhov
Copy link
Contributor

njordhov commented Apr 25, 2022

See also the proposal for variadic concat at clarity-lang/reference#17

@jcnelson
Copy link
Member

PR #3294 is still in-flight and will be included in the next VM upgrade

@jcnelson
Copy link
Member

Temporarily assigning this to @obycode. Please feel free to re-assign.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Status: 🆕 New
Development

No branches or pull requests

4 participants