Skip to content

Commit 0fcfcdc

Browse files
committed
Revert "optimize zipper performance"
This reverts commit e2d4e11.
1 parent e2d4e11 commit 0fcfcdc

File tree

4 files changed

+36
-30
lines changed

4 files changed

+36
-30
lines changed

lib/style/configs.ex

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,8 @@ defmodule Styler.Style.Configs do
4848
end
4949

5050
def run({{:config, cfm, [_, _ | _]} = config, zm}, %{mix_config?: true, comments: comments} = ctx) do
51-
{l, p, r} = zm
5251
# all of these list are reversed due to the reduce
53-
{configs, assignments, rest} = accumulate(r, [], [])
52+
{configs, assignments, rest} = accumulate(zm.r, [], [])
5453
# @TODO
5554
# okay so comments between nodes that we moved.......
5655
# lets just push them out of the way (???). so
@@ -93,9 +92,9 @@ defmodule Styler.Style.Configs do
9392
{nodes, comments}
9493
end
9594

96-
[config | left_siblings] = Enum.reverse(nodes, l)
95+
[config | left_siblings] = Enum.reverse(nodes, zm.l)
9796

98-
{:skip, {config, {left_siblings, p, rest}}, %{ctx | comments: comments}}
97+
{:skip, {config, %{zm | l: left_siblings, r: rest}}, %{ctx | comments: comments}}
9998
end
10099

101100
def run(zipper, %{config?: true} = ctx) do

lib/style/module_directives.ex

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,9 @@ defmodule Styler.Style.ModuleDirectives do
107107
# puts `@derive` before `defstruct` etc, fixing compiler warnings
108108
def run({{:@, _, [{:derive, _, _}]}, _} = zipper, ctx) do
109109
case Style.ensure_block_parent(zipper) do
110-
{:ok, {derive, {l, p, r}}} ->
110+
{:ok, {derive, %{l: left_siblings} = z_meta}} ->
111111
previous_defstruct =
112-
l
112+
left_siblings
113113
|> Stream.with_index()
114114
|> Enum.find_value(fn
115115
{{struct_def, meta, _}, index} when struct_def in @defstruct -> {meta[:line], index}
@@ -119,8 +119,8 @@ defmodule Styler.Style.ModuleDirectives do
119119
if previous_defstruct do
120120
{defstruct_line, defstruct_index} = previous_defstruct
121121
derive = Style.set_line(derive, defstruct_line - 1)
122-
left_siblings = List.insert_at(l, defstruct_index + 1, derive)
123-
{:skip, Zipper.remove({derive, {left_siblings, p, r}}), ctx}
122+
left_siblings = List.insert_at(left_siblings, defstruct_index + 1, derive)
123+
{:skip, Zipper.remove({derive, %{z_meta | l: left_siblings}}), ctx}
124124
else
125125
{:cont, zipper, ctx}
126126
end

lib/zipper.ex

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,14 @@ defmodule Styler.Zipper do
2626
import Kernel, except: [node: 1]
2727

2828
@type tree :: Macro.t()
29+
30+
@opaque path :: %{
31+
l: [tree],
32+
ptree: zipper,
33+
r: [tree]
34+
}
35+
2936
@type zipper :: {tree, path | nil}
30-
@type path :: {left :: [tree], parent :: zipper, right :: [tree]}
3137
@type t :: zipper
3238
@type command :: :cont | :skip | :halt
3339

@@ -85,7 +91,7 @@ defmodule Styler.Zipper do
8591
def down(zipper) do
8692
case children(zipper) do
8793
[] -> nil
88-
[first | rest] -> {first, {[], zipper, rest}}
94+
[first | rest] -> {first, %{ptree: zipper, l: [], r: rest}}
8995
end
9096
end
9197

@@ -96,25 +102,26 @@ defmodule Styler.Zipper do
96102
@spec up(zipper) :: zipper | nil
97103
def up({_, nil}), do: nil
98104

99-
def up({tree, {l, {parent, parent_meta}, r}}) do
100-
children = Enum.reverse(l, [tree | r])
105+
def up({tree, meta}) do
106+
children = Enum.reverse(meta.l, [tree | meta.r])
107+
{parent, parent_meta} = meta.ptree
101108
{do_replace_children(parent, children), parent_meta}
102109
end
103110

104111
@doc """
105112
Returns the zipper of the left sibling of the node at this zipper, or nil.
106113
"""
107114
@spec left(zipper) :: zipper | nil
108-
def left({tree, {[ltree | l], p, r}}), do: {ltree, {l, p, [tree | r]}}
115+
def left({tree, %{l: [ltree | l], r: r} = meta}), do: {ltree, %{meta | l: l, r: [tree | r]}}
109116
def left(_), do: nil
110117

111118
@doc """
112119
Returns the leftmost sibling of the node at this zipper, or itself.
113120
"""
114121
@spec leftmost(zipper) :: zipper
115-
def leftmost({tree, {[_ | _] = l, p, r}}) do
116-
[leftmost | r] = Enum.reverse(l, [tree | r])
117-
{leftmost, {[], p, r}}
122+
def leftmost({tree, %{l: [_ | _] = l} = meta}) do
123+
[leftmost | r] = Enum.reverse(l, [tree | meta.r])
124+
{leftmost, %{meta | l: [], r: r}}
118125
end
119126

120127
def leftmost({_, _} = zipper), do: zipper
@@ -123,16 +130,16 @@ defmodule Styler.Zipper do
123130
Returns the zipper of the right sibling of the node at this zipper, or nil.
124131
"""
125132
@spec right(zipper) :: zipper | nil
126-
def right({tree, {l, p, [rtree | r]}}), do: {rtree, {[tree | l], p, r}}
133+
def right({tree, %{r: [rtree | r]} = meta}), do: {rtree, %{meta | r: r, l: [tree | meta.l]}}
127134
def right(_), do: nil
128135

129136
@doc """
130137
Returns the rightmost sibling of the node at this zipper, or itself.
131138
"""
132139
@spec rightmost(zipper) :: zipper
133-
def rightmost({tree, {l, p, [_ | _] = r}}) do
134-
[rightmost | l] = Enum.reverse(r, [tree | l])
135-
{rightmost, {l, p, []}}
140+
def rightmost({tree, %{r: [_ | _] = r} = meta}) do
141+
[rightmost | l] = Enum.reverse(r, [tree | meta.l])
142+
{rightmost, %{meta | l: l, r: []}}
136143
end
137144

138145
def rightmost({_, _} = zipper), do: zipper
@@ -156,8 +163,8 @@ defmodule Styler.Zipper do
156163
"""
157164
@spec remove(zipper) :: zipper
158165
def remove({_, nil}), do: raise(ArgumentError, message: "Cannot remove the top level node.")
159-
def remove({_, {[left | rest], p, r}}), do: prev_down({left, {rest, p, r}})
160-
def remove({_, {_, {parent, parent_meta}, children}}), do: {do_replace_children(parent, children), parent_meta}
166+
def remove({_, %{l: [left | rest]} = meta}), do: prev_down({left, %{meta | l: rest}})
167+
def remove({_, %{ptree: {parent, parent_meta}, r: children}}), do: {do_replace_children(parent, children), parent_meta}
161168

162169
@doc """
163170
Inserts the item as the left sibling of the node at this zipper, without
@@ -177,7 +184,7 @@ defmodule Styler.Zipper do
177184
"""
178185
@spec prepend_siblings(zipper, [tree]) :: zipper
179186
def prepend_siblings({node, nil}, siblings), do: {:__block__, [], siblings ++ [node]} |> zip() |> down() |> rightmost()
180-
def prepend_siblings({tree, {l, p, r}}, siblings), do: {tree, {Enum.reverse(siblings, l), p , r}}
187+
def prepend_siblings({tree, meta}, siblings), do: {tree, %{meta | l: Enum.reverse(siblings, meta.l)}}
181188

182189
@doc """
183190
Inserts the item as the right sibling of the node at this zipper, without
@@ -197,7 +204,7 @@ defmodule Styler.Zipper do
197204
"""
198205
@spec insert_siblings(zipper, [tree]) :: zipper
199206
def insert_siblings({node, nil}, siblings), do: {:__block__, [], [node | siblings]} |> zip() |> down()
200-
def insert_siblings({tree, {l, p, r}}, siblings), do: {tree, {l, p, siblings ++ r}}
207+
def insert_siblings({tree, meta}, siblings), do: {tree, %{meta | r: siblings ++ meta.r}}
201208

202209
@doc """
203210
Inserts the item as the leftmost child of the node at this zipper,

test/zipper_test.exs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,14 @@ defmodule StylerTest.ZipperTest do
6363

6464
describe "down/1" do
6565
test "rips and tears the parent node" do
66-
assert [1, 2] |> Zipper.zip() |> Zipper.down() == {1, {[], {[1, 2], nil}, [2]}}
67-
assert {1, 2} |> Zipper.zip() |> Zipper.down() == {1, {[], {{1, 2}, nil}, [2]}}
66+
assert [1, 2] |> Zipper.zip() |> Zipper.down() == {1, %{l: [], r: [2], ptree: {[1, 2], nil}}}
67+
assert {1, 2} |> Zipper.zip() |> Zipper.down() == {1, %{l: [], r: [2], ptree: {{1, 2}, nil}}}
6868

6969
assert {:foo, [], [1, 2]} |> Zipper.zip() |> Zipper.down() ==
70-
{1, {[], {{:foo, [], [1, 2]}, nil}, [2]}}
70+
{1, %{l: [], r: [2], ptree: {{:foo, [], [1, 2]}, nil}}}
7171

7272
assert {{:., [], [:a, :b]}, [], [1, 2]} |> Zipper.zip() |> Zipper.down() ==
73-
{{:., [], [:a, :b]}, {[],{{{:., [], [:a, :b]}, [], [1, 2]}, nil}, [1, 2]}}
73+
{{:., [], [:a, :b]}, %{l: [], r: [1, 2], ptree: {{{:., [], [:a, :b]}, [], [1, 2]}, nil}}}
7474
end
7575
end
7676

@@ -471,8 +471,8 @@ defmodule StylerTest.ZipperTest do
471471
end
472472

473473
test "builds a new root node made of a block" do
474-
assert {42, {[:nope], {{:__block__, _, _}, nil}, []}} = 42 |> Zipper.zip() |> Zipper.insert_left(:nope)
475-
assert {42, {[], {{:__block__, _, _}, nil}, [:nope]}} = 42 |> Zipper.zip() |> Zipper.insert_right(:nope)
474+
assert {42, %{l: [:nope], ptree: {{:__block__, _, _}, nil}}} = 42 |> Zipper.zip() |> Zipper.insert_left(:nope)
475+
assert {42, %{r: [:nope], ptree: {{:__block__, _, _}, nil}}} = 42 |> Zipper.zip() |> Zipper.insert_right(:nope)
476476
end
477477
end
478478

0 commit comments

Comments
 (0)