Skip to content

Commit 2fd8534

Browse files
authored
Build compiler with -Dexecution_context (#16447)
We just enabled MT in #16380, but we can go further and enable execution context as suggested in the PR, by merely resizing the default context to the default workers count. Also changes the default `--threads` value to also be the default workers count, which is `CRYSTAL_WORKERS` when specified and valid, and defaults to the number of available logical CPUs instead of being hardcoded to 4 threads (default for `preview_mt`) or 8 forked processes (arbitrary number). It might be faster, on my computer recompiling Crystal went from ~17s to ~15s but your mileage may vary.
1 parent 412ec28 commit 2fd8534

File tree

3 files changed

+9
-3
lines changed

3 files changed

+9
-3
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ check ?= ## Enable only check when running format
3737
order ?=random ## Enable order for spec execution (values: "default" | "random" | seed number)
3838
deref_symlinks ?= ## Dereference symbolic links for `make install`
3939
docs_sanitizer ?= ## Enable sanitization for documentation generation
40-
sequential_codegen ?=$(if $(filter 0,$(supports_preview_mt)),true,)## Enforce sequential codegen in compiler builds. Base compiler before Crystal 1.8 cannot build with `-Dpreview_mt`
40+
sequential_codegen ?=$(if $(filter 0,$(supports_preview_mt)),true,)## Enforce sequential codegen in compiler builds. Base compiler before Crystal 1.8 cannot build with `-Dpreview_mt -Dexecution_context`
4141

4242
O := .build
4343
SOURCES := $(shell find src -name '*.cr')
@@ -46,7 +46,7 @@ MAN1PAGES := $(patsubst doc/man/%.adoc,man/%.1,$(wildcard doc/man/*.adoc))
4646
override FLAGS += -D strict_multi_assign -D preview_overload_order $(if $(release),--release )$(if $(stats),--stats )$(if $(progress),--progress )$(if $(threads),--threads $(threads) )$(if $(debug),-d )$(if $(static),--static )$(if $(LDFLAGS),--link-flags="$(LDFLAGS)" )$(if $(target),--cross-compile --target $(target) )
4747
# NOTE: USE_PCRE1 is only used for testing compatibility with legacy environments that don't provide libpcre2.
4848
# Newly built compilers should never be distributed with libpcre to ensure syntax consistency.
49-
override COMPILER_FLAGS += $(if $(interpreter),,-Dwithout_interpreter )$(if $(docs_sanitizer),,-Dwithout_libxml2 ) -Dwithout_openssl -Dwithout_zlib$(if $(sequential_codegen),, -Dpreview_mt) $(if $(USE_PCRE1),-Duse_pcre,-Duse_pcre2)
49+
override COMPILER_FLAGS += $(if $(interpreter),,-Dwithout_interpreter )$(if $(docs_sanitizer),,-Dwithout_libxml2 ) -Dwithout_openssl -Dwithout_zlib$(if $(sequential_codegen),, -Dpreview_mt -Dexecution_context) $(if $(USE_PCRE1),-Duse_pcre,-Duse_pcre2)
5050
SPEC_WARNINGS_OFF := --exclude-warnings spec/std --exclude-warnings spec/compiler --exclude-warnings spec/primitives --exclude-warnings src/float/printer --exclude-warnings src/random.cr
5151
override SPEC_FLAGS += $(if $(verbose),-v )$(if $(junit_output),--junit_output $(junit_output) )$(if $(order),--order=$(order) )
5252
CRYSTAL_CONFIG_LIBRARY_PATH := '$$ORIGIN/../lib/crystal'

src/compiler/crystal.cr

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,8 @@ require "./requires"
88

99
Log.setup_from_env(default_level: :warn, default_sources: "crystal.*")
1010

11+
{% if flag?(:execution_context) %}
12+
Fiber::ExecutionContext.default.resize(Fiber::ExecutionContext.default_workers_count)
13+
{% end %}
14+
1115
Crystal::Command.run

src/compiler/crystal/compiler.cr

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,9 @@ module Crystal
8383
property? no_codegen = false
8484

8585
# Maximum number of LLVM modules that are compiled in parallel
86-
property n_threads : Int32 = {% if flag?(:preview_mt) %}
86+
property n_threads : Int32 = {% if flag?(:execution_context) %}
87+
Fiber::ExecutionContext.default_workers_count
88+
{% elsif flag?(:preview_mt) %}
8789
ENV["CRYSTAL_WORKERS"]?.try(&.to_i?) || 4
8890
{% elsif flag?(:win32) %}
8991
1

0 commit comments

Comments
 (0)