Skip to content

Commit

Permalink
Use more concrete types
Browse files Browse the repository at this point in the history
  • Loading branch information
charleskawczynski committed Jul 15, 2023
1 parent cbdb0b9 commit 7d408b7
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 31 deletions.
23 changes: 5 additions & 18 deletions perf/benchmark.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,12 @@ TH.player_option!(game::Game, player::Player{BotCheckCall}, ::CallFold) = call!(

players() = ntuple(i->(Player(BotCheckCall(), i)), 4)

# use a counter to avoid benchmarking
# the creation of players and games:
const cntr = Int[1]
function do_work!(games)
with_logger(NullLogger()) do
play!(games[cntr[1]])
cntr[]+=1
end
return nothing
end

games = map(x->Game(players()), 1:100_000);
trial = @benchmark do_work!($games)
show(stdout, MIME("text/plain"), trial)

# Also benchmark including the
# creation of players and games:
# It's not easy to benchmark games without
# also benchmarking the creation/allocation
# of games. We previously did, and they're
# very close in benchmark times.
function do_work!()
with_logger(NullLogger()) do
Logging.with_logger(Logging.NullLogger()) do
play!(Game(players()))
end
return nothing
Expand Down
1 change: 0 additions & 1 deletion perf/jet.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ end

# Make sure it runs without errors
game = Game(players();logger=TH.ByPassLogger())
println("------------ about to do work")
do_work!(game)

import JET
Expand Down
12 changes: 5 additions & 7 deletions src/table.jl
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ buttons(b::Buttons) = (
b.first_to_act,
)

mutable struct Table{P,L}
mutable struct Table{P, L, TM}
deck::PlayingCards.Deck
players::P
cards::Union{Nothing,Tuple{<:Card,<:Card,<:Card,<:Card,<:Card}}
Expand All @@ -60,7 +60,7 @@ mutable struct Table{P,L}
buttons::Buttons
current_raise_amt::Float64
initial_round_raise_amt::Float64
transactions::TransactionManager
transactions::TM
winners::Winners
play_out_game::Bool
n_max_actions::Int
Expand Down Expand Up @@ -106,20 +106,18 @@ function Table(;
dealer_id = default_dealer_id(),
current_raise_amt = Float64(0),
initial_round_raise_amt = blinds.small,
transactions = nothing,
transactions = TransactionManager(players),
winners = Winners(),
play_out_game = false,
logger = StandardLogger(),
)
P = typeof(players)
if transactions == nothing
transactions = TransactionManager(players)
end
buttons = Buttons(dealer_id, players)
n_max_actions = compute_n_max_actions(players, blinds.big)
@cdebug logger "n_max_actions = $n_max_actions"
L = typeof(logger)
return Table{P,L}(deck,
TM = typeof(transactions)
return Table{P, L, TM}(deck,
players,
cards,
blinds,
Expand Down
11 changes: 6 additions & 5 deletions src/transactions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ cap(sp::SidePot) = sp.cap
Handle pots and side pots
among multiple players.
"""
struct TransactionManager
sorted_players::Vector{Player}
struct TransactionManager{SP}
sorted_players::SP
initial_brs::Vector{Float64}
pot_id::Union{Nothing,Vector{Int}}
pot_id::Vector{Int}
side_pots::Vector{SidePot}
unsorted_to_sorted_map::Vector{Int}
end
Expand All @@ -51,9 +51,10 @@ function TransactionManager(players)
unsorted_to_sorted_map = collect(map(players) do player
findfirst(seat_number.(sorted_players) .== seat_number(player))
end)
side_pots = [SidePot(sn, 0, cap_i) for (cap_i, sn, amt) in zip(cap, seat_number.(sorted_players), bank_roll.(sorted_players))]
side_pots = [SidePot(seat_number(sp), 0, cap_i) for (cap_i, sp) in zip(cap, sorted_players)]

TransactionManager(
SP = typeof(sorted_players)
TransactionManager{SP}(
sorted_players,
deepcopy(collect(bank_roll.(players))),
Int[1],
Expand Down

0 comments on commit 7d408b7

Please sign in to comment.