# ArgIteratorWindows: Reduce allocated memory by parsing the WTF-16 string directly · gitcafe/zig

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

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

Visibility: public

Requested revision: 959d227d1370c9cf9198ed111e3a8bca15a7e47b

Requested commit: 959d227d1370c9cf9198ed111e3a8bca15a7e47b

Commit: 959d227d1370c9cf9198ed111e3a8bca15a7e47b

Tree: 4141025bd114fa54fedf1e76adf19202561bc018

Author: Ryan Liptak

Committer: Andrew Kelley

## Message

```
ArgIteratorWindows: Reduce allocated memory by parsing the WTF-16 string directly

Before this commit, the WTF-16 command line string would be converted to WTF-8 in `init`, and then a second buffer of the WTF-8 size + 1 would be allocated to store the parsed arguments. The converted WTF-8 command line would then be parsed and the relevant bytes would be copied into the argument buffer before being returned.

After this commit, only the WTF-8 size of the WTF-16 string is calculated (without conversion) which is then used to allocate the buffer for the parsed arguments. Parsing is then done on the WTF-16 slice directly, with the arguments being converted to WTF-8 on-the-fly.

This has a few (minor) benefits:

- Cuts the amount of memory allocated by ArgIteratorWindows in half (or better)
- Makes the total amount of memory allocated by ArgIteratorWindows predictable, since, before, the upfront `wtf16LeToWtf8Alloc` call could end up allocating more-memory-than-necessary temporarily due to its internal use of an ArrayList. Now, the amount of memory allocated is always exactly `calcWtf8Len(cmd_line) + 1`.

```

## Parents

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

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