# ArgIteratorWindows: Store last emitted code unit instead of checking the last 6 emitted bytes · gitcafe/zig

[View on GitCafe](https://git.cafe/gitcafe/zig/commit/1418c8a5d49ecc38a20df07151a7fe942992aea4)

Repository: [gitcafe/zig](https://git.cafe/gitcafe/zig)

Visibility: public

Requested revision: 1418c8a5d49ecc38a20df07151a7fe942992aea4

Requested commit: 1418c8a5d49ecc38a20df07151a7fe942992aea4

Commit: 1418c8a5d49ecc38a20df07151a7fe942992aea4

Tree: 4386161d9e439aac1b609f8902f0e07e12c44565

Author: Ryan Liptak

Committer: Ryan Liptak

## Message

```
ArgIteratorWindows: Store last emitted code unit instead of checking the last 6 emitted bytes

Previously, to ensure args were encoded as well-formed WTF-8 (i.e. no encoded surrogate pairs), the code unit would be encoded and then the last 6 emitted bytes would be checked to see if they were a surrogate pair, and this was done for any emitted code unit (although this was not necessary, it should have only been done when emitting a low surrogate).

After this commit, we still want to ensure well-formed WTF-8, but, to do so, the last emitted code point is stored, meaning we can just directly check that the last code unit is a high surrogate and the current code unit is a low surrogate to determine if we have a surrogate pair.

This provides some performance benefit over and above a "use the same strategy as before but only check when we're emitting a low surrogate" implementation:

    Benchmark 1 (111 runs): benchargv-master.exe
      measurement          mean ± σ            min … max           outliers         delta
      wall_time          45.2ms ±  532us    44.5ms … 49.4ms          2 ( 2%)        0%
      peak_rss           6.49MB ± 3.94KB    6.46MB … 6.49MB         10 ( 9%)        0%
    Benchmark 2 (154 runs): benchargv-storelast.exe
      measurement          mean ± σ            min … max           outliers         delta
      wall_time          32.6ms ±  293us    32.2ms … 34.2ms          8 ( 5%)        ⚡- 27.8% ±  0.2%
      peak_rss           6.49MB ± 5.15KB    6.46MB … 6.49MB         15 (10%)          -  0.0% ±  0.0%
    Benchmark 3 (131 runs): benchargv-onlylow.exe
      measurement          mean ± σ            min … max           outliers         delta
      wall_time          38.4ms ±  257us    37.9ms … 39.6ms          5 ( 4%)        ⚡- 15.1% ±  0.2%
      peak_rss           6.49MB ± 5.70KB    6.46MB … 6.49MB          9 ( 7%)          -  0.0% ±  0.0%

```

## Parents

- [10914dc31000bc5c3b7164a4101808719d3da07f](https://git.cafe/gitcafe/zig/commit/10914dc31000bc5c3b7164a4101808719d3da07f?format=markdown)

[Source at this commit](https://git.cafe/gitcafe/zig/tree/1418c8a5d49ecc38a20df07151a7fe942992aea4?format=markdown)
