Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into dev/gc-mmtk
Browse files Browse the repository at this point in the history
  • Loading branch information
peterzhu2118 committed Oct 2, 2024
2 parents 6d15b94 + e320da6 commit 5d68564
Show file tree
Hide file tree
Showing 29 changed files with 278 additions and 103 deletions.
7 changes: 7 additions & 0 deletions .github/actions/compilers/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ inputs:
description: >-
Whether to run `make check`
mspecopt:
required: false
default: ''
description: >-
Additional options for mspec.
static_exts:
required: false
description: >-
Expand Down Expand Up @@ -89,6 +95,7 @@ runs:
--env INPUT_CPPFLAGS='${{ inputs.cppflags }}'
--env INPUT_APPEND_CONFIGURE='${{ inputs.append_configure }}'
--env INPUT_CHECK='${{ inputs.check }}'
--env INPUT_MSPECOPT='${{ inputs.mspecopt }}'
--env INPUT_ENABLE_SHARED='${{ inputs.enable_shared }}'
--env INPUT_STATIC_EXTS='${{ inputs.static_exts }}'
'ghcr.io/ruby/ruby-ci-image:${{ inputs.tag }}'
2 changes: 1 addition & 1 deletion .github/actions/compilers/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,4 @@ fi
grouped make install
grouped make test-tool
grouped make test-all TESTS="-- $tests"
grouped env CHECK_LEAKS=true make test-spec
grouped env CHECK_LEAKS=true make test-spec MSPECOPT="$INPUT_MSPECOPT"
8 changes: 4 additions & 4 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,15 @@ jobs:
run: sudo rm /usr/lib/ruby/vendor_ruby/rubygems/defaults/operating_system.rb

- name: Initialize CodeQL
uses: github/codeql-action/init@461ef6c76dfe95d5c364de2f431ddbd31a417628 # v3.26.9
uses: github/codeql-action/init@e2b3eafc8d227b0241d48be5f425d47c2d750a13 # v3.26.10
with:
languages: ${{ matrix.language }}

- name: Autobuild
uses: github/codeql-action/autobuild@461ef6c76dfe95d5c364de2f431ddbd31a417628 # v3.26.9
uses: github/codeql-action/autobuild@e2b3eafc8d227b0241d48be5f425d47c2d750a13 # v3.26.10

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@461ef6c76dfe95d5c364de2f431ddbd31a417628 # v3.26.9
uses: github/codeql-action/analyze@e2b3eafc8d227b0241d48be5f425d47c2d750a13 # v3.26.10
with:
category: '/language:${{ matrix.language }}'
upload: False
Expand Down Expand Up @@ -115,7 +115,7 @@ jobs:
continue-on-error: true

- name: Upload SARIF
uses: github/codeql-action/upload-sarif@461ef6c76dfe95d5c364de2f431ddbd31a417628 # v3.26.9
uses: github/codeql-action/upload-sarif@e2b3eafc8d227b0241d48be5f425d47c2d750a13 # v3.26.10
with:
sarif_file: sarif-results/${{ matrix.language }}.sarif
continue-on-error: true
2 changes: 1 addition & 1 deletion .github/workflows/compilers.yml
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ jobs:
- { uses: './.github/actions/setup/directories', with: { srcdir: 'src', builddir: 'build', makeup: true } }
- { uses: './.github/actions/compilers', name: '-O0', with: { optflags: '-O0 -march=x86-64 -mtune=generic' } }
# - { uses: './.github/actions/compilers', name: '-O3', with: { optflags: '-O3 -march=x86-64 -mtune=generic', check: true } }
- { uses: './.github/actions/compilers', name: 'gmp', with: { append_configure: '--with-gmp', check: 'ruby/test_bignum.rb' } }
- { uses: './.github/actions/compilers', name: 'gmp', with: { append_configure: '--with-gmp', check: 'ruby/test_bignum.rb', mspecopt: "/github/workspace/src/spec/ruby/core/integer" } }
- { uses: './.github/actions/compilers', name: 'jemalloc', with: { append_configure: '--with-jemalloc' } }
- { uses: './.github/actions/compilers', name: 'valgrind', with: { append_configure: '--with-valgrind' } }
- { uses: './.github/actions/compilers', name: 'coroutine=ucontext', with: { append_configure: '--with-coroutine=ucontext' } }
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/scorecards.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,6 @@ jobs:

# Upload the results to GitHub's code scanning dashboard.
- name: 'Upload to code-scanning'
uses: github/codeql-action/upload-sarif@461ef6c76dfe95d5c364de2f431ddbd31a417628 # v3.26.9
uses: github/codeql-action/upload-sarif@e2b3eafc8d227b0241d48be5f425d47c2d750a13 # v3.26.10
with:
sarif_file: results.sarif
2 changes: 0 additions & 2 deletions gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1143,8 +1143,6 @@ rb_gc_obj_free(void *objspace, VALUE obj)
{
RB_DEBUG_COUNTER_INC(obj_free);

rb_gc_event_hook(obj, RUBY_INTERNAL_EVENT_FREEOBJ);

switch (BUILTIN_TYPE(obj)) {
case T_NIL:
case T_FIXNUM:
Expand Down
22 changes: 7 additions & 15 deletions gc/default.c
Original file line number Diff line number Diff line change
Expand Up @@ -939,16 +939,6 @@ heap_eden_total_pages(rb_objspace_t *objspace)
return count;
}

static inline size_t
heap_eden_total_slots(rb_objspace_t *objspace)
{
size_t count = 0;
for (int i = 0; i < SIZE_POOL_COUNT; i++) {
count += SIZE_POOL_EDEN_HEAP(&size_pools[i])->total_slots;
}
return count;
}

static inline size_t
total_allocated_objects(rb_objspace_t *objspace)
{
Expand Down Expand Up @@ -3565,6 +3555,8 @@ gc_sweep_plane(rb_objspace_t *objspace, rb_heap_t *heap, uintptr_t p, bits_t bit
#undef CHECK
#endif

rb_gc_event_hook(vp, RUBY_INTERNAL_EVENT_FREEOBJ);

bool has_object_id = FL_TEST(vp, FL_SEEN_OBJ_ID);
if (rb_gc_obj_free(objspace, vp)) {
if (has_object_id) {
Expand Down Expand Up @@ -5460,7 +5452,7 @@ gc_marks_finish(rb_objspace_t *objspace)
{
const unsigned long r_mul = objspace->live_ractor_cache_count > 8 ? 8 : objspace->live_ractor_cache_count; // upto 8

size_t total_slots = heap_eden_total_slots(objspace);
size_t total_slots = objspace_available_slots(objspace);
size_t sweep_slots = total_slots - objspace->marked_slots; /* will be swept slots */
size_t max_free_slots = (size_t)(total_slots * gc_params.heap_free_slots_max_ratio);
size_t min_free_slots = (size_t)(total_slots * gc_params.heap_free_slots_min_ratio);
Expand All @@ -5470,7 +5462,7 @@ gc_marks_finish(rb_objspace_t *objspace)

int full_marking = is_full_marking(objspace);

GC_ASSERT(heap_eden_total_slots(objspace) >= objspace->marked_slots);
GC_ASSERT(objspace_available_slots(objspace) >= objspace->marked_slots);

/* Setup freeable slots. */
size_t total_init_slots = 0;
Expand Down Expand Up @@ -5524,7 +5516,7 @@ gc_marks_finish(rb_objspace_t *objspace)
gc_report(1, objspace, "gc_marks_finish (marks %"PRIdSIZE" objects, "
"old %"PRIdSIZE" objects, total %"PRIdSIZE" slots, "
"sweep %"PRIdSIZE" slots, allocatable %"PRIdSIZE" slots, next GC: %s)\n",
objspace->marked_slots, objspace->rgengc.old_objects, heap_eden_total_slots(objspace), sweep_slots, objspace->heap_pages.allocatable_slots,
objspace->marked_slots, objspace->rgengc.old_objects, objspace_available_slots(objspace), sweep_slots, objspace->heap_pages.allocatable_slots,
gc_needs_major_flags ? "major" : "minor");
}

Expand Down Expand Up @@ -6860,7 +6852,7 @@ rb_gc_impl_prepare_heap(void *objspace_ptr)
{
rb_objspace_t *objspace = objspace_ptr;

size_t orig_total_slots = heap_eden_total_slots(objspace);
size_t orig_total_slots = objspace_available_slots(objspace);
size_t orig_allocatable_slots = objspace->heap_pages.allocatable_slots;

rb_gc_impl_each_objects(objspace, gc_set_candidate_object_i, objspace_ptr);
Expand All @@ -6876,7 +6868,7 @@ rb_gc_impl_prepare_heap(void *objspace_ptr)
GC_ASSERT(objspace->empty_pages_count == 0);
objspace->heap_pages.allocatable_slots = orig_allocatable_slots;

size_t total_slots = heap_eden_total_slots(objspace);
size_t total_slots = objspace_available_slots(objspace);
if (orig_total_slots > total_slots) {
objspace->heap_pages.allocatable_slots += orig_total_slots - total_slots;
}
Expand Down
3 changes: 2 additions & 1 deletion io_buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -843,7 +843,8 @@ rb_io_buffer_get_bytes(VALUE self, void **base, size_t *size)
static inline void
io_buffer_get_bytes_for_writing(struct rb_io_buffer *buffer, void **base, size_t *size)
{
if (buffer->flags & RB_IO_BUFFER_READONLY) {
if (buffer->flags & RB_IO_BUFFER_READONLY ||
(!NIL_P(buffer->source) && OBJ_FROZEN(buffer->source))) {
rb_raise(rb_eIOBufferAccessError, "Buffer is not writable!");
}

Expand Down
6 changes: 3 additions & 3 deletions lib/reline/history.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def [](index)

def []=(index, val)
index = check_index(index)
super(index, String.new(val, encoding: Reline.encoding_system_needs))
super(index, Reline::Unicode.safe_encode(val, Reline.encoding_system_needs))
end

def concat(*val)
Expand All @@ -45,7 +45,7 @@ def push(*val)
end
end
super(*(val.map{ |v|
String.new(v, encoding: Reline.encoding_system_needs)
Reline::Unicode.safe_encode(v, Reline.encoding_system_needs)
}))
end

Expand All @@ -56,7 +56,7 @@ def <<(val)
if @config.history_size.positive?
shift if size + 1 > @config.history_size
end
super(String.new(val, encoding: Reline.encoding_system_needs))
super(Reline::Unicode.safe_encode(val, Reline.encoding_system_needs))
end

private def check_index(index)
Expand Down
2 changes: 1 addition & 1 deletion lib/reline/line_editor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1325,7 +1325,7 @@ def insert_multiline_text(text)
save_old_buffer
pre = @buffer_of_lines[@line_index].byteslice(0, @byte_pointer)
post = @buffer_of_lines[@line_index].byteslice(@byte_pointer..)
lines = (pre + text.gsub(/\r\n?/, "\n") + post).split("\n", -1)
lines = (pre + Reline::Unicode.safe_encode(text, @encoding).gsub(/\r\n?/, "\n") + post).split("\n", -1)
lines << '' if lines.empty?
@buffer_of_lines[@line_index, 1] = lines
@line_index += lines.size - 1
Expand Down
16 changes: 16 additions & 0 deletions lib/reline/unicode.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,22 @@ def self.escape_for_print(str)
}.join
end

def self.safe_encode(str, encoding)
# Reline only supports utf-8 convertible string.
converted = str.encode(encoding, invalid: :replace, undef: :replace)
return converted if str.encoding == Encoding::UTF_8 || converted.encoding == Encoding::UTF_8 || converted.ascii_only?

# This code is essentially doing the same thing as
# `str.encode(utf8, **replace_options).encode(encoding, **replace_options)`
# but also avoids unneccesary irreversible encoding conversion.
converted.gsub(/\X/) do |c|
c.encode(Encoding::UTF_8)
c
rescue Encoding::UndefinedConversionError
'?'
end
end

require 'reline/unicode/east_asian_width'

def self.get_mbchar_width(mbchar)
Expand Down
107 changes: 87 additions & 20 deletions lib/singleton.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# frozen_string_literal: false
# frozen_string_literal: true

# The Singleton module implements the Singleton pattern.
#
Expand Down Expand Up @@ -94,20 +94,23 @@
module Singleton
VERSION = "0.2.0"

# Raises a TypeError to prevent cloning.
def clone
raise TypeError, "can't clone instance of singleton #{self.class}"
end
module SingletonInstanceMethods
# Raises a TypeError to prevent cloning.
def clone
raise TypeError, "can't clone instance of singleton #{self.class}"
end

# Raises a TypeError to prevent duping.
def dup
raise TypeError, "can't dup instance of singleton #{self.class}"
end
# Raises a TypeError to prevent duping.
def dup
raise TypeError, "can't dup instance of singleton #{self.class}"
end

# By default, do not retain any state when marshalling.
def _dump(depth = -1)
''
# By default, do not retain any state when marshalling.
def _dump(depth = -1)
''
end
end
include SingletonInstanceMethods

module SingletonClassMethods # :nodoc:

Expand All @@ -121,7 +124,7 @@ def _load(str)
end

def instance # :nodoc:
@singleton__instance__ || @singleton__mutex__.synchronize { @singleton__instance__ ||= new }
@singleton__instance__ || @singleton__mutex__.synchronize { @singleton__instance__ ||= set_instance(new) }
end

private
Expand All @@ -130,22 +133,42 @@ def inherited(sub_klass)
super
Singleton.__init__(sub_klass)
end

def set_instance(val)
@singleton__instance__ = val
end

def set_mutex(val)
@singleton__mutex__ = val
end
end

class << Singleton # :nodoc:
def self.module_with_class_methods
SingletonClassMethods
end

module SingletonClassProperties

def self.included(c)
# extending an object with Singleton is a bad idea
c.undef_method :extend_object
end

def self.extended(c)
# extending an object with Singleton is a bad idea
c.singleton_class.send(:undef_method, :extend_object)
end

def __init__(klass) # :nodoc:
klass.instance_eval {
@singleton__instance__ = nil
@singleton__mutex__ = Thread::Mutex.new
set_instance(nil)
set_mutex(Thread::Mutex.new)
}
klass
end

private

# extending an object with Singleton is a bad idea
undef_method :extend_object

def append_features(mod)
# help out people counting on transitive mixins
unless mod.instance_of?(Class)
Expand All @@ -157,10 +180,11 @@ def append_features(mod)
def included(klass)
super
klass.private_class_method :new, :allocate
klass.extend SingletonClassMethods
klass.extend module_with_class_methods
Singleton.__init__(klass)
end
end
extend SingletonClassProperties

##
# :singleton-method: _load
Expand All @@ -170,3 +194,46 @@ def included(klass)
# :singleton-method: instance
# Returns the singleton instance.
end

if defined?(Ractor)
module RactorLocalSingleton
include Singleton::SingletonInstanceMethods

module RactorLocalSingletonClassMethods
include Singleton::SingletonClassMethods
def instance
set_mutex(Thread::Mutex.new) if Ractor.current[mutex_key].nil?
return Ractor.current[instance_key] if Ractor.current[instance_key]
Ractor.current[mutex_key].synchronize {
return Ractor.current[instance_key] if Ractor.current[instance_key]
set_instance(new())
}
Ractor.current[instance_key]
end

private

def instance_key
:"__RactorLocalSingleton_instance_with_class_id_#{object_id}__"
end

def mutex_key
:"__RactorLocalSingleton_mutex_with_class_id_#{object_id}__"
end

def set_instance(val)
Ractor.current[instance_key] = val
end

def set_mutex(val)
Ractor.current[mutex_key] = val
end
end

def self.module_with_class_methods
RactorLocalSingletonClassMethods
end

extend Singleton::SingletonClassProperties
end
end
Loading

0 comments on commit 5d68564

Please sign in to comment.