Skip to content

Runtime-level performance: GC and string representation, thread creation, exception dispatch #213

Open

Description

@Largo

Successor to #89. That issue listed performance gaps found while optimizing; everything in it that was a missed optimization in IronRuby's own code has been fixed and merged (#95, #111, #114, #171, #175, #182, #184, #186–#192, #194, #195, #202, #206–#208, #210). What is left is at the level of the .NET runtime or needs a design change. This issue holds those ideas, with the numbers from #89, so they are not lost. None is known to be worth doing yet; each needs measuring first.

Numbers are from #89 (Release, Linux, one core, net10.0 / net11.0 against CRuby 4.0.9; the box was loaded for several of them, so compare ratios).

1. GC cost and string representation

s + "x" on a 100 KB string is ~43–48 µs against CRuby's 9–11. A Ruby string is UTF-16 here, so a 100 KB string is a 200 KB char[] on the large-object heap, and large-object allocation triggers gen-2 collections. Real gen-0 pauses are ~3× a bare .NET app's (0.3–0.45 ms against 0.15 ms), about 70 % of it scanning thread stacks: a Ruby-level call is ~10 CLR frames. See #89 (comment).

Ideas, none applied:

  • Hold string contents as bytes where possible (halves large-object churn). Strings: long ASCII String#+ held as bytes; a slice that is appended to is copied once #207 does this for a long ASCII sum only.
  • Fewer CLR frames per Ruby call.
  • A smaller startup heap (~15 MB after startup): expression trees kept by not-yet-compiled interpreted lambdas are ~25 % of its objects, RubyMethodBody._ast keeps all prelude ASTs, and ruby4.rb's source is held twice.
  • Process-wide GC settings: DOTNET_GCLOHThreshold=0x200000 gave ~4× on s + "x", a bigger gen-0 budget 7–10 % in a hash loop. These are policy for every workload and need data across workloads before becoming a default.

Also left here: hash of a non-ASCII string held as bytes is ~3× CRuby (.NET's UTF-8 decode, 0.5–1.9 ns/byte). A fused decode-and-hash loop was tried and was slower.

2. Thread creation

Thread.new { }.join is 41–58 µs main-thread CPU against CRuby's 27–34, and 124–171 µs process CPU against 51–60. What is left is the CLR thread itself: a plain C# new Thread().Start(); Join() loop costs the same.

Idea, evaluated in #208 and not done: pool CLR threads (~8–15 µs per hand-off). A Ruby Thread is its System.Threading.Thread today, so pooling needs a separate identity object and changes to every Thread.CurrentThread use and to the async-exception and fiber code.

3. Exception dispatch

A raise rescued in place is ~9.3 µs against CRuby's 1.1; through 20 ensure frames 34 µs against 2.8; catch { throw } ~5 µs against 0.19. About 90 % of what is left is the CLR's own dispatch.

Left in IronRuby's code after #192, #194 and #210: an ensure clause that calls a block, yields, calls super or jumps out still catches and rethrows (one full CLR throw per such frame), because the DLR rethrows there. A rescue whose class list has to be evaluated (anything but constant core classes) cannot move into a first-pass filter, since Ruby evaluates it after inner ensures have run. See #89 (comment).

4. Smaller items, runtime-bound

  • Small numeric calls (x[3], Integer(12), 3 ** 5, 3.to_c, Range#size) are within ~2× CRuby with instruction counts near CRuby's; the gap is the general cost of a dynamic call and an allocation.
  • Integer.sqrt above ~10k bits and Integer#to_s at ~1M bits (~13×) are .NET BigInteger against GMP.
  • CPU spent in a Fiber is not charged to the resuming thread by Process.clock_gettime(CLOCK_THREAD_CPUTIME_ID) (a Fiber runs on a worker thread here).

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions