Skip to content

Commit

Permalink
evict_to_size should only evict if it needs to
Browse files Browse the repository at this point in the history
(Previously, it would *always* evict at least one record even if
size was <= new_size)
  • Loading branch information
mtrudel committed Dec 19, 2024
1 parent f59c2d8 commit 6d034a1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
2 changes: 2 additions & 0 deletions lib/hpax/table.ex
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,8 @@ defmodule HPAX.Table do

# Removes records as necessary to have the total size of entries within the table be less than
# or equal to the specified value. Does not change the table's max size.
defp evict_to_size(%__MODULE__{size: size} = table, new_size) when size <= new_size, do: table

defp evict_to_size(%__MODULE__{entries: entries, size: size} = table, new_size) do
{new_entries_reversed, new_size} =
evict_towards_size(Enum.reverse(entries), size, new_size)
Expand Down
37 changes: 28 additions & 9 deletions test/hpax/table_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ defmodule HPAX.TableTest do
end
end

describe "resizing" do
describe "resize/2" do
test "increasing the protocol max table size" do
table = Table.new(4096, :never)
table = Table.add(table, "aaaa", "AAAA")
Expand All @@ -80,25 +80,44 @@ defmodule HPAX.TableTest do
assert table.protocol_max_table_size == 8192
end

test "decreasing the protocol max table size not below the max table size" do
test "decreasing the protocol max table size but above table size" do
table = Table.new(4096, :never)
table = Table.add(table, "aaaa", "AAAA")
table = Table.resize(table, 2048)
assert table.size == 40
assert table.max_table_size == 2048
assert table.protocol_max_table_size == 2048
end

test "decreasing the protocol max table size below current size should evict" do
table = Table.new(4096, :never)
table = Table.add(table, "aaaa", "AAAA")
table = Table.add(table, "bbbb", "BBBB")
table = Table.resize(table, 60)
assert table.size == 40
assert table.max_table_size == 60
assert table.protocol_max_table_size == 60
end
end

describe "dynamic_resize/2" do
test "decreasing the max table size but above table size" do
table = Table.new(4096, :never)
table = Table.add(table, "aaaa", "AAAA")
table = Table.dynamic_resize(table, 2048)
table = Table.resize(table, 6000)
assert table.size == 40
assert table.max_table_size == 6000
assert table.protocol_max_table_size == 6000
assert table.max_table_size == 2048
assert table.protocol_max_table_size == 4096
end

test "decreasing the protocol max table size below the max table size" do
test "decreasing the protocol max table size below current size should evict" do
table = Table.new(4096, :never)
table = Table.add(table, "aaaa", "AAAA")
table = Table.add(table, "bbbb", "BBBB")
table = Table.resize(table, 40)
table = Table.dynamic_resize(table, 60)
assert table.size == 40
assert table.max_table_size == 40
assert table.protocol_max_table_size == 40
assert table.max_table_size == 60
assert table.protocol_max_table_size == 4096
end
end
end

0 comments on commit 6d034a1

Please sign in to comment.