@SuppressWarnings("unchecked")
    public static <T> T deep(final T input) {
        if (input instanceof Map<?, ?>) {
            return (T) deepMap((Map<?, ?>) input);
        } else if (input instanceof List<?>) {
            return (T) deepList((List<?>) input);
        } else if (input instanceof RubyString) {
            // new instance but sharing ByteList (until either String is modified)
            return (T) ((RubyString) input).dup();
        } else if (input instanceof Collection<?>) {
            throw new ClassCastException("unexpected Collection type " + input.getClass());
        }

        return input;
    }
@Test // @Tag("Performance Optimization")
    public void testRubyStringCloningMemoryOptimization() {
        ByteList bytes = ByteList.create("0123456789");
        RubyString original = RubyString.newString(RubyUtil.RUBY, bytes);

        RubyString result = Cloner.deep(original);
        assertNotSame(original, result);

        assertSame(bytes, original.getByteList());
        // NOTE: this is an implementation detail or the underlying sharing :
        assertSame(bytes, result.getByteList()); // bytes-list shared

        // but when string is modified it will stop using the same byte container
        result.concat(RubyUtil.RUBY.getCurrentContext(), RubyUtil.RUBY.newString(" "));
        assertNotSame(bytes, result.getByteList()); // byte-list copied on write
    }