Skip to content

Commit

Permalink
performance optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
mruoss committed Nov 23, 2023
1 parent cfdc2ee commit c44a016
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 48 deletions.
84 changes: 42 additions & 42 deletions BENCHMARK.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Benchmark

Benchmark run from 2023-11-21 21:51:23.068588Z UTC
Benchmark run from 2023-11-23 17:53:11.749718Z UTC

## System

Expand Down Expand Up @@ -65,20 +65,20 @@ Run Time

<tr>
<td style="white-space: nowrap">Jason</td>
<td style="white-space: nowrap; text-align: right">273.46</td>
<td style="white-space: nowrap; text-align: right">3.66 ms</td>
<td style="white-space: nowrap; text-align: right">&plusmn;15.86%</td>
<td style="white-space: nowrap; text-align: right">3.37 ms</td>
<td style="white-space: nowrap; text-align: right">5.53 ms</td>
<td style="white-space: nowrap; text-align: right">248.88</td>
<td style="white-space: nowrap; text-align: right">4.02 ms</td>
<td style="white-space: nowrap; text-align: right">&plusmn;14.75%</td>
<td style="white-space: nowrap; text-align: right">3.81 ms</td>
<td style="white-space: nowrap; text-align: right">5.72 ms</td>
</tr>

<tr>
<td style="white-space: nowrap">Ymlr</td>
<td style="white-space: nowrap; text-align: right">3.50</td>
<td style="white-space: nowrap; text-align: right">286.06 ms</td>
<td style="white-space: nowrap; text-align: right">&plusmn;0.60%</td>
<td style="white-space: nowrap; text-align: right">286.03 ms</td>
<td style="white-space: nowrap; text-align: right">289.08 ms</td>
<td style="white-space: nowrap; text-align: right">3.27</td>
<td style="white-space: nowrap; text-align: right">305.72 ms</td>
<td style="white-space: nowrap; text-align: right">&plusmn;1.14%</td>
<td style="white-space: nowrap; text-align: right">305.02 ms</td>
<td style="white-space: nowrap; text-align: right">317.72 ms</td>
</tr>

</table>
Expand All @@ -93,14 +93,14 @@ Run Time Comparison
<th style="text-align: right">Slower</th>
<tr>
<td style="white-space: nowrap">Jason</td>
<td style="white-space: nowrap;text-align: right">273.46</td>
<td style="white-space: nowrap;text-align: right">248.88</td>
<td>&nbsp;</td>
</tr>

<tr>
<td style="white-space: nowrap">Ymlr</td>
<td style="white-space: nowrap; text-align: right">3.50</td>
<td style="white-space: nowrap; text-align: right">78.23x</td>
<td style="white-space: nowrap; text-align: right">3.27</td>
<td style="white-space: nowrap; text-align: right">76.09x</td>
</tr>

</table>
Expand All @@ -122,8 +122,8 @@ Memory Usage
</tr>
<tr>
<td style="white-space: nowrap">Ymlr</td>
<td style="white-space: nowrap">67.38 MB</td>
<td>14.02x</td>
<td style="white-space: nowrap">68.18 MB</td>
<td>14.19x</td>
</tr>
</table>

Expand All @@ -145,20 +145,20 @@ Run Time

<tr>
<td style="white-space: nowrap">Jason</td>
<td style="white-space: nowrap; text-align: right">127.15</td>
<td style="white-space: nowrap; text-align: right">7.86 ms</td>
<td style="white-space: nowrap; text-align: right">&plusmn;14.06%</td>
<td style="white-space: nowrap; text-align: right">7.44 ms</td>
<td style="white-space: nowrap; text-align: right">121.34</td>
<td style="white-space: nowrap; text-align: right">8.24 ms</td>
<td style="white-space: nowrap; text-align: right">&plusmn;142.36%</td>
<td style="white-space: nowrap; text-align: right">7.35 ms</td>
<td style="white-space: nowrap; text-align: right">13.61 ms</td>
</tr>

<tr>
<td style="white-space: nowrap">Ymlr</td>
<td style="white-space: nowrap; text-align: right">22.26</td>
<td style="white-space: nowrap; text-align: right">44.91 ms</td>
<td style="white-space: nowrap; text-align: right">&plusmn;19.91%</td>
<td style="white-space: nowrap; text-align: right">44.52 ms</td>
<td style="white-space: nowrap; text-align: right">58.06 ms</td>
<td style="white-space: nowrap; text-align: right">21.10</td>
<td style="white-space: nowrap; text-align: right">47.39 ms</td>
<td style="white-space: nowrap; text-align: right">&plusmn;62.18%</td>
<td style="white-space: nowrap; text-align: right">43.84 ms</td>
<td style="white-space: nowrap; text-align: right">319.16 ms</td>
</tr>

</table>
Expand All @@ -173,14 +173,14 @@ Run Time Comparison
<th style="text-align: right">Slower</th>
<tr>
<td style="white-space: nowrap">Jason</td>
<td style="white-space: nowrap;text-align: right">127.15</td>
<td style="white-space: nowrap;text-align: right">121.34</td>
<td>&nbsp;</td>
</tr>

<tr>
<td style="white-space: nowrap">Ymlr</td>
<td style="white-space: nowrap; text-align: right">22.26</td>
<td style="white-space: nowrap; text-align: right">5.71x</td>
<td style="white-space: nowrap; text-align: right">21.10</td>
<td style="white-space: nowrap; text-align: right">5.75x</td>
</tr>

</table>
Expand Down Expand Up @@ -225,20 +225,20 @@ Run Time

<tr>
<td style="white-space: nowrap">Jason</td>
<td style="white-space: nowrap; text-align: right">391.80</td>
<td style="white-space: nowrap; text-align: right">2.55 ms</td>
<td style="white-space: nowrap; text-align: right">&plusmn;4.91%</td>
<td style="white-space: nowrap; text-align: right">368.32</td>
<td style="white-space: nowrap; text-align: right">2.72 ms</td>
<td style="white-space: nowrap; text-align: right">&plusmn;251.44%</td>
<td style="white-space: nowrap; text-align: right">2.57 ms</td>
<td style="white-space: nowrap; text-align: right">2.94 ms</td>
<td style="white-space: nowrap; text-align: right">3.29 ms</td>
</tr>

<tr>
<td style="white-space: nowrap">Ymlr</td>
<td style="white-space: nowrap; text-align: right">4.93</td>
<td style="white-space: nowrap; text-align: right">202.80 ms</td>
<td style="white-space: nowrap; text-align: right">&plusmn;1.12%</td>
<td style="white-space: nowrap; text-align: right">202.63 ms</td>
<td style="white-space: nowrap; text-align: right">208.55 ms</td>
<td style="white-space: nowrap; text-align: right">4.76</td>
<td style="white-space: nowrap; text-align: right">210.14 ms</td>
<td style="white-space: nowrap; text-align: right">&plusmn;0.45%</td>
<td style="white-space: nowrap; text-align: right">209.83 ms</td>
<td style="white-space: nowrap; text-align: right">211.96 ms</td>
</tr>

</table>
Expand All @@ -253,14 +253,14 @@ Run Time Comparison
<th style="text-align: right">Slower</th>
<tr>
<td style="white-space: nowrap">Jason</td>
<td style="white-space: nowrap;text-align: right">391.80</td>
<td style="white-space: nowrap;text-align: right">368.32</td>
<td>&nbsp;</td>
</tr>

<tr>
<td style="white-space: nowrap">Ymlr</td>
<td style="white-space: nowrap; text-align: right">4.93</td>
<td style="white-space: nowrap; text-align: right">79.46x</td>
<td style="white-space: nowrap; text-align: right">4.76</td>
<td style="white-space: nowrap; text-align: right">77.4x</td>
</tr>

</table>
Expand All @@ -282,7 +282,7 @@ Memory Usage
</tr>
<tr>
<td style="white-space: nowrap">Ymlr</td>
<td style="white-space: nowrap">65.35 MB</td>
<td>25.69x</td>
<td style="white-space: nowrap">65.86 MB</td>
<td>25.89x</td>
</tr>
</table>
20 changes: 16 additions & 4 deletions lib/ymlr/encode.ex
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,18 @@ defmodule Ymlr.Encode do
# Printable ASCII
Enum.to_list(0x20..0x7E),
# Basic Multilingual Plane (BMP)
Enum.to_list(0xA0..0xFF)
Enum.to_list(0xA0..0xD7FF),
Enum.to_list(0xE000..0xFFFD),
# 32 bit
Enum.to_list(0x010000..0x10FFFF)
])

@not_supported_by_elixir Enum.to_list(0xD800..0xDFFF)

# Non-Printable Characters (8-bit only for now) - all chars minus union of printable and escape chars:
@non_printable_chars Enum.to_list(0x00..0xFF) --
(@printable_chars ++ @escape_if_within_double_quotes)
@non_printable_chars Enum.to_list(0..0x10FFFF) --
(@printable_chars ++
@escape_if_within_double_quotes ++ @not_supported_by_elixir)

# Chars that, if contained within, force the string to be double-quoted:
@chars_forcing_double_quotes_strings Enum.map(
Expand Down Expand Up @@ -312,7 +318,13 @@ defmodule Ymlr.Encode do
end

for uchar <- @non_printable_chars do
unicode_sequence = List.to_string(:io_lib.format("\\x~2.16.0B", [uchar]))
unicode_sequence =
case uchar do
uchar when uchar <= 0xFF -> List.to_string(:io_lib.format("\\x~2.16.0B", [uchar]))
uchar when uchar <= 0xFFFF -> List.to_string(:io_lib.format("\\u~4.16.0B", [uchar]))
uchar -> List.to_string(:io_lib.format("\\U~6.16.0B", [uchar]))
end

defp escape_char(unquote(uchar)), do: unquote(unicode_sequence)
end

Expand Down
7 changes: 5 additions & 2 deletions test/ymlr/encode_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,11 @@ defmodule Ymlr.EncodeTest do
~S(" \_ \N \L \P")
)

# Possible formats: \x13 \u0013 \U00000013. We use \x13
assert_identity_and_output("\u0013", "\"\\x13\"")
# Possible formats: \x13 \u0013 \U00000013.
assert_identity_and_output(
"\u0013\uFFFD\uFFFE\u{10FFFF}",
"\"\\x13\uFFFD\\uFFFE\u{10FFFF}\""
)
end

test "quoted strings - in map key (requires escape char)" do
Expand Down

0 comments on commit c44a016

Please sign in to comment.