Skip to content

Commit ab68668

Browse files
Default execution context is now parallel (MT:1) (#16136)
The concurrent context doesn't exhibit any performance improvement over the parallel context (see #16135). So we may as well use a parallel context as default context, and allow resizing it for more parallelism (see #15956). The default configuration is without parallelism (MT:1), thus the execution context remains concurrent only, which is backward compatible (nothing changes). Resizing the context to enable parallelism will be at the discretion of the application: it can keep using the `CRYSTAL_WORKERS` environment variable, and/or fallback to the number of CPUs or use a hardcoded value, and/or provide a command line argument, or anything else. Co-authored-by: Johannes Müller <[email protected]>
1 parent f2342c2 commit ab68668

File tree

2 files changed

+15
-14
lines changed

2 files changed

+15
-14
lines changed

src/fiber/execution_context.cr

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,34 +56,31 @@ require "./execution_context/*"
5656
#
5757
# ## The default execution context
5858
#
59-
# The Crystal runtime starts with a single threaded execution context, available
60-
# as `Fiber::ExecutionContext.default`:
59+
# The Crystal runtime starts a default execution context exposed as
60+
# `Fiber::ExecutionContext.default`. This is where the main fiber is running.
61+
#
62+
# Its parallelism is set to 1 for backwards compatibility reasons; Crystal used
63+
# to be single-threaded and concurrent only. You can increase the parallelism at
64+
# any time using `Parallel#resize`, for example:
6165
#
6266
# ```
63-
# Fiber::ExecutionContext.default.class # => Fiber::ExecutionContext::Concurrent
67+
# count = Fiber::ExecutionContext.default_workers_count
68+
# Fiber::ExecutionContext.default.resize(count)
6469
# ```
65-
#
66-
# NOTE: The default context is a `Concurrent` context for backwards
67-
# compatibility reasons. It might change to a `Parallel` context in the
68-
# future.
6970
@[Experimental]
7071
module Fiber::ExecutionContext
7172
@@default : ExecutionContext?
7273

7374
# Returns the default `ExecutionContext` for the process, automatically
7475
# started when the program started.
75-
#
76-
# NOTE: The default context is a `Concurrent` context for backwards
77-
# compatibility reasons. It might change to a `Parallel` context in the
78-
# future.
7976
@[AlwaysInline]
8077
def self.default : ExecutionContext
8178
@@default.not_nil!("expected default execution context to have been setup")
8279
end
8380

8481
# :nodoc:
8582
def self.init_default_context : Nil
86-
@@default = Concurrent.default
83+
@@default = Parallel.default(1)
8784
@@monitor = Monitor.new
8885
end
8986

src/fiber/execution_context/parallel.cr

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ require "./parallel/scheduler"
44
module Fiber::ExecutionContext
55
# Parallel execution context.
66
#
7-
# Fibers running in the same context run both concurrently and in parallel to each
8-
# others, in addition to the other fibers running in other execution contexts.
7+
# Fibers running in the same context run both concurrently and in parallel to
8+
# each others, in addition to the other fibers running in other execution
9+
# contexts.
910
#
1011
# The context internally keeps a number of fiber schedulers, each scheduler
1112
# being able to start running on a system thread, so multiple schedulers can
@@ -18,6 +19,9 @@ module Fiber::ExecutionContext
1819
# example not enough fibers, the schedulers will pause themselves and
1920
# parallelism will decrease.
2021
#
22+
# The parallelism can be as low as 1, in which case the context becomes a
23+
# concurrent context (no parallelism) until resized.
24+
#
2125
# For example: we can start a parallel context to run consumer fibers, while
2226
# the default context produces values. Because the consumer fibers can run in
2327
# parallel, we must protect accesses to the shared *value* variable. Running

0 commit comments

Comments
 (0)