Live data from Hacker News

Strings and the CLR – A Special Relationship

mattwarren.org

11–20 of 30 posts

Re: Strings and the CLR – A Special Relationship

#11
post #2

Another point for this, besides optimization, is easier interop with existing technologies. The memory layout is identical to Windows' BSTR, as far as I know, and also includes a terminating U+0000. All of that makes it possible to marshal a CLR string as is in many circumstances without the need to copy or convert the characters around.

There is an amusing comment in the CLR about it's support for odd length BSTRs[0]. There can be an extra wchar after the null terminator to store the extra byte. It looks like if you use the automatic BSTR marshaler it creates a copy[1], though the CLR is so big it's hard for me to say that with confidence. Taking the address of a string with the fixed statement in C# of course won't allocate and that pointer can be…

I love comments like that, it's almost a history lesson on VB and the CLR!

Re: Strings and the CLR – A Special Relationship

#12
Nice post.

There are many things that aren't possible in C# but can be expressed in pure IL. Sigil [0] is an IL generator used in the high-performance JSON serializer Jil [1], which takes advantage of this.

https://github.com/kevin-montrose/Sigil

https://github.com/kevin-montrose/Jil

Re: Strings and the CLR – A Special Relationship

#13

Nice post. There are many things that aren't possible in C# but can be expressed in pure IL. Sigil [0] is an IL generator used in the high-performance JSON serializer Jil [1], which takes advantage of this. https://github.com/kevin-montrose/Sigil https://github.com/kevin-montrose/Jil

I've seen Jil/Sigil before, but not thought about it in this way.

Are you saying that you could exactly replicate a C# System.String using IL, or am I mis-understanding?

Re: Strings and the CLR – A Special Relationship

#14
post #6

So, are there high-level languages that make it easy to write classes that include length-varies-by-instance member(s) in their memory layout? That could be a huge efficiency gain because of cache misses.

I imagine Eiffel, Modula-3 and Ada are possible candidates, but would need to research it.

Re: Strings and the CLR – A Special Relationship

#15

Nice post. There are many things that aren't possible in C# but can be expressed in pure IL. Sigil [0] is an IL generator used in the high-performance JSON serializer Jil [1], which takes advantage of this. https://github.com/kevin-montrose/Sigil https://github.com/kevin-montrose/Jil

I've seen Jil/Sigil before, but not thought about it in this way. Are you saying that you could exactly replicate a C# System.String using IL, or am I mis-understanding?

Given that IL needs to be expressive enough to represent C++/CLI, I would guess it is possible.

Re: Strings and the CLR – A Special Relationship

#16
post #15

Earlier quoted context omitted.

I've seen Jil/Sigil before, but not thought about it in this way. Are you saying that you could exactly replicate a C# System.String using IL, or am I mis-understanding?

Given that IL needs to be expressive enough to represent C++/CLI, I would guess it is possible.

But C++/CLI results in a mixed-mode half-native, half-managed assembly. It doesn't compile down to only IL.

Re: Strings and the CLR – A Special Relationship

#17
post #6

So, are there high-level languages that make it easy to write classes that include length-varies-by-instance member(s) in their memory layout? That could be a huge efficiency gain because of cache misses.

Rust has unsized (aka dynamically-sized) types, trait objects and slices are probably the most commonly encountered DSTs.

There are two main limitations to them:

* by default most generic code can not operate over them, generics require the special `?Sized` bound to allow DSTs (essentially generics have a default Sized generic bound)

* Rust doesn't currently support dynamic stack allocation so DSTs can't be stack-allocated (https://github.com/rust-lang/rfcs/issues/618)

Re: Strings and the CLR – A Special Relationship

#18

Nice post. There are many things that aren't possible in C# but can be expressed in pure IL. Sigil [0] is an IL generator used in the high-performance JSON serializer Jil [1], which takes advantage of this. https://github.com/kevin-montrose/Sigil https://github.com/kevin-montrose/Jil

I've seen Jil/Sigil before, but not thought about it in this way. Are you saying that you could exactly replicate a C# System.String using IL, or am I mis-understanding?

I'm not sure if you could replicate System.String, because the oddity is happening at the metadata level - apart from special cases like String, .Net types must be compile-time constant length (if I remember correctly).

However, at the instruction level you can do some pretty awesome stuff. This slice[1] library is able to verifiably and safely grab a slice of data out of any arbitrary structure by using a MSIL feature that was invented for C++/CLR. The call site[2] is not unsafe.

[1]: https://github.com/joeduffy/slice.net/blob/master/src/PtrUti... [2]: https://github.com/joeduffy/slice.net/blob/master/src/Slice....

Re: Strings and the CLR – A Special Relationship

#19
post #18

Earlier quoted context omitted.

I've seen Jil/Sigil before, but not thought about it in this way. Are you saying that you could exactly replicate a C# System.String using IL, or am I mis-understanding?

I'm not sure if you could replicate System.String, because the oddity is happening at the metadata level - apart from special cases like String, .Net types must be compile-time constant length (if I remember correctly). However, at the instruction level you can do some pretty awesome stuff. This slice[1] library is able to verifiably and safely grab a slice of data out of any arbitrary structure by using a MSIL featu…

Yeah the slice stuff is pretty nice, it's actually now being developed on the CoreFX Labs[1], so it might eventually make it into the Core CLR.

[1]: https://github.com/dotnet/corefxlab/tree/master/src/System.S...

Re: Strings and the CLR – A Special Relationship

#20
post #16
post #15

Earlier quoted context omitted.

Given that IL needs to be expressive enough to represent C++/CLI, I would guess it is possible.

But C++/CLI results in a mixed-mode half-native, half-managed assembly. It doesn't compile down to only IL.

Not if you use the /clr:pure flag
Post reply on HN