Skip to content
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
27de2ab
Rewrite Model with bitmask wave and AC-4 propagator
pusewicz May 26, 2026
4738796
Skip supporter decrement for already-banned tiles
pusewicz May 26, 2026
5cf00c0
Drop unused log_weights array in propagator setup
pusewicz May 26, 2026
10ee256
Assert percent is exactly zero before solving
pusewicz May 26, 2026
f216999
Terminate when the tileset has only one tile
pusewicz May 26, 2026
8a0434b
Precompute per-tile bitmasks
pusewicz May 26, 2026
c951f6c
Precompute per-cell neighbour table
pusewicz May 26, 2026
444d2b6
Resolve chosen tile via Integer#bit_length
pusewicz May 26, 2026
aebf07e
Cache the initial supporter-count buffer for restarts
pusewicz May 26, 2026
4d4fafd
Use setbyte for interior_block construction
pusewicz May 26, 2026
6ea4d9f
Intern tile edge signatures via class-level cache
pusewicz May 26, 2026
ddf3b50
Reuse per-cell state arrays across restarts
pusewicz May 26, 2026
a1c1dc9
Reuse compatible buffer via String#replace
pusewicz May 26, 2026
2dd31e5
Shift prepend_empty_row state in place
pusewicz May 26, 2026
c0c7c4f
Compact heap after one-time model setup
pusewicz May 26, 2026
a52b8af
Emit optional GC stats from bin/benchmark
pusewicz May 26, 2026
d149ff3
Add bin/memory_benchmark for allocation profiling
pusewicz May 26, 2026
22a0a0c
Inline ban into propagate's inner loop
pusewicz May 26, 2026
9f803ad
Add serena
pusewicz May 26, 2026
6cc5bd3
Handle zero-probability tiles without producing NaN
pusewicz May 26, 2026
25b831e
Reject tilesets whose supporter counts would overflow a byte
pusewicz May 26, 2026
28a120a
Recover instead of silently resetting when prepend_empty_row contradicts
pusewicz May 26, 2026
57f465c
Materialise chosen_tile for the new row of single-tile tilesets
pusewicz May 26, 2026
c27ee0c
Key intern_edge by the triple itself instead of a packed integer
pusewicz May 26, 2026
1b804ce
Cap consecutive restarts in observe_and_propagate
pusewicz May 27, 2026
1813bb0
Cache map into a Gosu macro and batch iterates per frame
pusewicz May 27, 2026
b033fb1
Support WxH sizes and stream progress in bin/benchmark
pusewicz May 27, 2026
35cee4d
Split wave into chunked Fixnums for allocation-free propagation
pusewicz May 27, 2026
08d8513
Merge entropy and noise into a single array for the lowest-entropy scan
pusewicz May 27, 2026
1d93616
Re-enable GC in bin/run
pusewicz May 27, 2026
dd5350b
Split compatible per direction and tighten propagate inner loop
pusewicz May 27, 2026
9ea443c
Add ZJIT status
pusewicz May 27, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 74 additions & 29 deletions bin/benchmark
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,13 @@

$LOAD_PATH.unshift File.expand_path("../lib", __dir__)

require "benchmark"
require "json"
require "wave_function_collapse"

WIDTH = 20
HEIGHT = 20
TILE_PATH = File.expand_path("../assets/map.tsj", __dir__)

srand(WIDTH * HEIGHT)

json = JSON.load_file!("assets/map.tsj")
tiles =
def build_tiles
json = JSON.load_file(TILE_PATH)
json["wangsets"].last["wangtiles"].map do |tile|
prob = json["tiles"]&.find { |t| t["id"] == tile["tileid"] }&.fetch("probability")
WaveFunctionCollapse::Tile.new(
Expand All @@ -22,33 +18,82 @@ tiles =
probability: prob
)
end
times = []
end

puts RUBY_DESCRIPTION unless ENV["CI"]
def run_once(klass, tiles, w, h)
t0 = Process.clock_gettime(Process::CLOCK_MONOTONIC)
model = klass.new(tiles, w, h)
model.solve
iters = 1
until model.complete?
model.iterate
iters += 1
end
[Process.clock_gettime(Process::CLOCK_MONOTONIC) - t0, iters]
end

times = 10.times.map { |i|
time = Benchmark.realtime {
def median(xs)
s = xs.sort
n = s.length
n.odd? ? s[n / 2] : (s[n / 2 - 1] + s[n / 2]) / 2.0
end

tiles = build_tiles

if ENV["CI"]
# CI contract: 10 runs of 20x20 on the new Model, JSON output for the
# github-action-benchmark workflow. Do not change without updating the workflow.
WIDTH = 20
HEIGHT = 20
srand(WIDTH * HEIGHT)
times = 10.times.map {
t0 = Process.clock_gettime(Process::CLOCK_MONOTONIC)
model = WaveFunctionCollapse::Model.new(tiles, WIDTH, HEIGHT)
print "Run ##{i + 1}: Benchmark for Model(grid=#{model.width}x#{model.height} entropy=#{model.max_entropy})… " unless ENV["CI"]
model.solve
until model.complete?
model.iterate
end
model.iterate until model.complete?
Process.clock_gettime(Process::CLOCK_MONOTONIC) - t0
}
puts "Finished in #{time.round(2)}s" unless ENV["CI"]
time
}

average = times.sum / times.size
if ENV["CI"]
puts JSON.dump([
{ name: "Average time", unit: "Seconds", value: average },
{ name: "Slowest time", unit: "Seconds", value: times.max },
{ name: "Fastest time", unit: "Seconds", value: times.min },
{ name: "P90", unit: "Seconds", value: times.sort[8] }
{name: "Average time", unit: "Seconds", value: times.sum / times.size},
{name: "Slowest time", unit: "Seconds", value: times.max},
{name: "Fastest time", unit: "Seconds", value: times.min},
{name: "P90", unit: "Seconds", value: times.sort[8]}
])
else
puts "Average time: #{average}"
puts "Slowest time: #{times.max}"
puts "Fastest time: #{times.min}"
exit
end

yjit_status = defined?(RubyVM::YJIT) && RubyVM::YJIT.enabled? ? "on" : "off"
puts RUBY_DESCRIPTION
puts "Tiles loaded: #{tiles.size}, YJIT=#{yjit_status}"
puts

legacy_sizes = (ENV["LEGACY_SIZES"] || "10,15,20,25,30").split(",").map(&:to_i)
new_sizes = (ENV["NEW_SIZES"] || "10,15,20,30,50,75,100").split(",").map(&:to_i)
runs = Integer(ENV["RUNS"] || "3")
skip_legacy = ARGV.include?("--no-legacy")

printf("%-8s %-9s %-7s %-10s %-10s %-12s %-10s\n",
"Model", "Grid", "Cells", "Median", "Best", "Obs/sec", "Iters")
printf("%-8s %-9s %-7s %-10s %-10s %-12s %-10s\n",
"-----", "----", "-----", "------", "----", "-------", "-----")

[
[WaveFunctionCollapse::LegacyModel, skip_legacy ? [] : legacy_sizes, "Legacy"],
[WaveFunctionCollapse::Model, new_sizes, "New"]
].each do |klass, sizes, label|
sizes.each do |s|
times = []
iters_seen = 0
runs.times do |r|
srand(s * 1000 + r + 1)
elapsed, iters = run_once(klass, tiles, s, s)
times << elapsed
iters_seen = iters
end
med = median(times)
best = times.min
printf("%-8s %-9s %-7d %9.3fs %9.3fs %12.0f %10d\n",
label, "#{s}x#{s}", s * s, med, best, iters_seen / med, iters_seen)
end
puts
end
40 changes: 40 additions & 0 deletions bin/profile
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

$LOAD_PATH.unshift File.expand_path("../lib", __dir__)

require "json"
require "wave_function_collapse"
require "ruby-prof"

WIDTH = 20
HEIGHT = 20

srand(WIDTH * HEIGHT)

json = JSON.load_file!("assets/map.tsj")
tiles =
json["wangsets"].last["wangtiles"].map do |tile|
prob = json["tiles"]&.find { |t| t["id"] == tile["tileid"] }&.fetch("probability")
WaveFunctionCollapse::Tile.new(
tileid: tile["tileid"],
wangid: tile["wangid"],
probability: prob
)
end

# Profile
profile = RubyProf::Profile.new
profile.start

model = WaveFunctionCollapse::Model.new(tiles, WIDTH, HEIGHT)
model.solve
until model.complete?
model.iterate
end

result = profile.stop

# Print a flat profile to text
printer = RubyProf::FlatPrinter.new(result)
printer.print(STDOUT, min_percent: 1)
2 changes: 1 addition & 1 deletion lib/wave_function_collapse.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module WaveFunctionCollapse
class Error < StandardError; end

autoload :Cell, "wave_function_collapse/cell"
autoload :LegacyModel, "wave_function_collapse/legacy_model"
autoload :Model, "wave_function_collapse/model"
autoload :Tile, "wave_function_collapse/tile"
autoload :Window, "wave_function_collapse/window"
Expand Down
52 changes: 0 additions & 52 deletions lib/wave_function_collapse/cell.rb

This file was deleted.

Loading
Loading