Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions .serena/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/cache
/project.local.yml
133 changes: 133 additions & 0 deletions .serena/project.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
# the name by which the project can be referenced within Serena
project_name: "wave-function-collapse-ruby"


# list of languages for which language servers are started; choose from:
# al angular ansible bash clojure
# cpp cpp_ccls crystal csharp csharp_omnisharp
# dart elixir elm erlang fortran
# fsharp go groovy haskell haxe
# hlsl html java json julia
# kotlin lean4 lua luau markdown
# matlab msl nix ocaml pascal
# perl php php_phpactor powershell python
# python_jedi python_ty r rego ruby
# ruby_solargraph rust scala scss solidity
# svelte swift systemverilog terraform toml
# typescript typescript_vts vue yaml zig
# (This list may be outdated. For the current list, see values of Language enum here:
# https://github.com/oraios/serena/blob/main/src/solidlsp/ls_config.py
# For some languages, there are alternative language servers, e.g. csharp_omnisharp, ruby_solargraph.)
# Note:
# - For C, use cpp
# - For JavaScript, use typescript
# - For Angular projects, use angular (subsumes typescript+html; requires `npm install` in the project root)
# - For Svelte projects, use svelte (subsumes typescript/javascript for .svelte projects; requires npm)
# - For SCSS / Sass / plain CSS, use scss (some-sass-language-server handles all three)
# - For Free Pascal/Lazarus, use pascal
# Special requirements:
# Some languages require additional setup/installations.
# See here for details: https://oraios.github.io/serena/01-about/020_programming-languages.html#language-servers
# When using multiple languages, the first language server that supports a given file will be used for that file.
# The first language is the default language and the respective language server will be used as a fallback.
# Note that when using the JetBrains backend, language servers are not used and this list is correspondingly ignored.
languages:
- ruby

# the encoding used by text files in the project
# For a list of possible encodings, see https://docs.python.org/3.11/library/codecs.html#standard-encodings
encoding: "utf-8"

# line ending convention to use when writing source files.
# Possible values: unset (use global setting), "lf", "crlf", or "native" (platform default)
# This does not affect Serena's own files (e.g. memories and configuration files), which always use native line endings.
line_ending:

# The language backend to use for this project.
# If not set, the global setting from serena_config.yml is used.
# Valid values: LSP, JetBrains
# Note: the backend is fixed at startup. If a project with a different backend
# is activated post-init, an error will be returned.
language_backend:

# whether to use project's .gitignore files to ignore files
ignore_all_files_in_gitignore: true

# advanced configuration option allowing to configure language server-specific options.
# Maps the language key to the options.
# Have a look at the docstring of the constructors of the LS implementations within solidlsp (e.g., for C# or PHP) to see which options are available.
# No documentation on options means no options are available.
ls_specific_settings: {}

# list of additional workspace folder paths for cross-package reference support (e.g. in monorepos).
# Paths can be absolute or relative to the project root.
# Each folder is registered as an LSP workspace folder, enabling language servers to discover
# symbols and references across package boundaries.
# Currently supported for: TypeScript.
# Example:
# additional_workspace_folders:
# - ../sibling-package
# - ../shared-lib
additional_workspace_folders: []

# list of additional paths to ignore in this project.
# Same syntax as gitignore, so you can use * and **.
# Note: global ignored_paths from serena_config.yml are also applied additively.
ignored_paths: []

# whether the project is in read-only mode
# If set to true, all editing tools will be disabled and attempts to use them will result in an error
# Added on 2025-04-18
read_only: false

# list of tool names to exclude.
# This extends the existing exclusions (e.g. from the global configuration)
# Find the list of tools here: https://oraios.github.io/serena/01-about/035_tools.html
excluded_tools: []

# list of tools to include that would otherwise be disabled (particularly optional tools that are disabled by default).
# This extends the existing inclusions (e.g. from the global configuration).
# Find the list of tools here: https://oraios.github.io/serena/01-about/035_tools.html
included_optional_tools: []

# fixed set of tools to use as the base tool set (if non-empty), replacing Serena's default set of tools.
# This cannot be combined with non-empty excluded_tools or included_optional_tools.
# Find the list of tools here: https://oraios.github.io/serena/01-about/035_tools.html
fixed_tools: []

# list of mode names that are to be activated by default, overriding the setting in the global configuration.
# The full set of modes to be activated is base_modes (from global config) + default_modes + added_modes.
# If the setting is undefined/empty, the default_modes from the global configuration (serena_config.yml) apply.
# Otherwise, this overrides the setting from the global configuration (serena_config.yml).
# Therefore, you can set this to [] if you do not want the default modes defined in the global config to apply
# for this project.
# This setting can, in turn, be overridden by CLI parameters (--mode).
# See https://oraios.github.io/serena/02-usage/050_configuration.html#modes
default_modes:

# list of mode names to be activated additionally for this project, e.g. ["query-projects"]
# The full set of modes to be activated is base_modes (from global config) + default_modes + added_modes.
# See https://oraios.github.io/serena/02-usage/050_configuration.html#modes
added_modes:

# initial prompt for the project. It will always be given to the LLM upon activating the project
# (contrary to the memories, which are loaded on demand).
initial_prompt: ""

# time budget (seconds) per tool call for the retrieval of additional symbol information
# such as docstrings or parameter information.
# This overrides the corresponding setting in the global configuration; see the documentation there.
# If null or missing, use the setting from the global configuration.
symbol_info_budget:

# list of regex patterns which, when matched, mark a memory entry as read‑only.
# Extends the list from the global configuration, merging the two lists.
read_only_memory_patterns: []

# list of regex patterns for memories to completely ignore.
# Matching memories will not appear in list_memories or activate_project output
# and cannot be accessed via read_memory or write_memory.
# To access ignored memory files, use the read_file tool on the raw file path.
# Extends the list from the global configuration, merging the two lists.
# Example: ["_archive/.*", "_episodes/.*"]
ignored_memory_patterns: []
158 changes: 130 additions & 28 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,139 @@ tiles =
probability: prob
)
end
times = []
end

GC_STAT_KEYS = %i[total_allocated_objects malloc_increase_bytes minor_gc_count major_gc_count].freeze

def gc_delta(before, after)
GC_STAT_KEYS.each_with_object({}) { |k, h| h[k] = after[k] - before[k] }
end

def run_once(klass, tiles, w, h)
GC.start
gc_before = GC.stat
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
elapsed = Process.clock_gettime(Process::CLOCK_MONOTONIC) - t0
gc_after = GC.stat
[elapsed, iters, gc_delta(gc_before, gc_after)]
end

def median(xs)
s = xs.sort
n = s.length
n.odd? ? s[n / 2] : (s[n / 2 - 1] + s[n / 2]) / 2.0
end

puts RUBY_DESCRIPTION unless ENV["CI"]
tiles = build_tiles

times = 10.times.map { |i|
time = Benchmark.realtime {
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]}
])
exit
end

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

# Accepts either "N" (treated as NxN) or "WxH" entries.
def parse_sizes(str)
str.split(",").map do |tok|
tok = tok.strip
if tok.include?("x")
w, h = tok.split("x", 2).map(&:to_i)
[w, h]
else
n = tok.to_i
[n, n]
end
end
end

# SIZES overrides both lists. Otherwise each model uses its own *_SIZES var if
# set, or falls back to the other model's setting, or finally its own default.
shared = ENV["SIZES"]
legacy_env = ENV["LEGACY_SIZES"]
new_env = ENV["NEW_SIZES"]
legacy_sizes = parse_sizes(shared || legacy_env || new_env || "10,15,20,25,30")
new_sizes = parse_sizes(shared || new_env || legacy_env || "10,15,20,30,50,64x36,75,100")
runs = Integer(ENV["RUNS"] || "3")
skip_legacy = ARGV.include?("--no-legacy")
show_gc = ENV["GC_STATS"]

# Header widths match the data printf below so columns align.
# Data: "%-8s %-9s %-7d %9.3fs %9.3fs %12.0f %10d" (+ GC: %12d %14d %8d %8d)
# %9.3fs renders as 10 chars (9 numeric + literal 's').
if show_gc
printf("%-8s %-9s %-7s %10s %10s %12s %10s %12s %14s %8s %8s\n",
"Model", "Grid", "Cells", "Median", "Best", "Obs/sec", "Iters",
"Alloc/run", "Malloc B/run", "MinorGC", "MajorGC")
printf("%-8s %-9s %-7s %10s %10s %12s %10s %12s %14s %8s %8s\n",
"-----", "----", "-----", "------", "----", "-------", "-----",
"---------", "------------", "-------", "-------")
else
puts "Average time: #{average}"
puts "Slowest time: #{times.max}"
puts "Fastest time: #{times.min}"
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",
"-----", "----", "-----", "------", "----", "-------", "-----")
end

[
[WaveFunctionCollapse::LegacyModel, skip_legacy ? [] : legacy_sizes, "Legacy"],
[WaveFunctionCollapse::Model, new_sizes, "New"]
].each do |klass, sizes, label|
sizes.each do |(w, h)|
cells = w * h
grid = "#{w}x#{h}"
printf("%-8s %-9s %-7d ", label, grid, cells)
$stdout.flush
times = []
iters_seen = 0
gc_deltas = []
runs.times do |r|
srand(cells * 1000 + r + 1)
elapsed, iters, gc = run_once(klass, tiles, w, h)
times << elapsed
iters_seen = iters
gc_deltas << gc
end
med = median(times)
best = times.min
if show_gc
avg = ->(k) { gc_deltas.sum { |g| g[k] } / gc_deltas.size }
printf("%9.3fs %9.3fs %12.0f %10d %12d %14d %8d %8d\n",
med, best, iters_seen / med, iters_seen,
avg.call(:total_allocated_objects), avg.call(:malloc_increase_bytes),
avg.call(:minor_gc_count), avg.call(:major_gc_count))
else
printf("%9.3fs %9.3fs %12.0f %10d\n",
med, best, iters_seen / med, iters_seen)
end
end
puts
end
Loading
Loading