# Speed of UPARSE

**URL:** <https://revolt.metaeducation.com/forum/t/speed-of-uparse/2177>\
**Category:** Optimization\
**Created:** [March 29, 2024, 6:47am UTC](https://revolt.metaeducation.com/forum/t/speed-of-uparse/2177 "2024-03-29T06:47:47Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![hostilefork](https://revolt.metaeducation.com/forum/user_avatar/revolt.metaeducation.com/hostilefork/32/421_2.png) [@hostilefork](https://revolt.metaeducation.com/forum/u/hostilefork)\
**Post date:** [March 29, 2024, 2:38pm UTC](https://revolt.metaeducation.com/forum/t/speed-of-uparse/2177/3 "2024-03-29T14:38:32Z")

</div>

> [@hostilefork](#):
>
> ```plaintext
> 'optional combinator [
> "If applying parser fails, succeed and return NULL; don't advance input"
> return: "PARSER's result if it succeeds, otherwise NULL"
> [any-value? pack?]
> parser [action?]
> <local> ^result
> ][
> [^result remainder]: parser input except e -> [
> remainder: input ; succeed on parser fail but don't advance input
> return null
> ]
> return ^result ; return successful parser result
> ]
> 
> ```

Speaking of optimizations, this doesn't even need the **result** temporary. Nor does it care what the raised error is, so the branch doesn't need to be a lambda:

```
return [{^} remainder]: parser input except [
    remainder: input ; succeed on parser fail but don't advance input
    return null
]

```

_(Truly being equivalent [requires the `{^}` to not decay synthesized pack results](https://revolt.metaeducation.com/forum/t/should-set-word-s-propagate-undecayed-packs/2178), which should possibly be the same behavior you get if you'd named a variable, I need to look into this.)_

Instead of a preemptive RETURN inside a RETURN, you could write that instead so the branch gives back the result and remainder as a pack (antiform block):

```
return [{^} remainder]: parser input except [
    pack [null input]
]

```

But I happen to be certain that would be slower... you pay to reduce **[null input]**, and then you have to pay for SET-BLOCK! to unpack the multi-return.

Neat as all that is (and I think it _is_ neat), worrying over such details isn't really where I want to focus right now.

* * *

_Note that usermode micro-optimization can get fairly crazy, e.g. you know that NULL needs to do a WORD! lookup to fetch the value of NULL... but the quasiform `~null~` has a rote evaluative rule of just setting the quote byte of the quasiform from 2 to 0 to be an antiform... so **`return ~null~`** is faster than **`return null`**. Many historical Rebol programmers were obsessed with this kind of detail..._

---

_[View the full topic](https://revolt.metaeducation.com/forum/t/speed-of-uparse/2177)._
