wanpeebaw
2020-11-27 13:15:38

Is it possible to code at a lower level (intermediate) language in Racket when user needs to squeeze the most performance out of the machine?


laurent.orseau
2020-11-27 13:18:26

You can write some very-time-sensitive functions in C(++) and then use the FFI. But it’s pretty rare to need that.


laurent.orseau
2020-11-27 13:18:54

You can also learn what makes Racket slow/fast and understand the balance with safety/good error reporting


laurent.orseau
2020-11-27 13:19:15

(like types, contracts, loops, etc.)


laurent.orseau
2020-11-27 13:19:40

But a large fraction of the time, it’s about what data structures and algorithms you’re using rather than how fast the language is.


wanpeebaw
2020-11-27 13:23:04

I mean, is there any or some low-level part of the Racket language or VM (intermediate part) that can be exposed to programmer? It would be nice to write those low-level code directly in Racket and translate into native code via Racket VM.


wanpeebaw
2020-11-27 13:23:15

A Low-level DSL


wanpeebaw
2020-11-27 13:24:24

This avoids using other languages (such as C or C++) and involves FFI stuff.


laurent.orseau
2020-11-27 13:24:47

Apart for the FFI, I don’t think there’s such a thing. Take a look at https://docs.racket-lang.org/reference/performance-hint.html maybe


phanthero
2020-11-27 13:25:23

What does FFI stand for BTW?


wanpeebaw
2020-11-27 13:25:47

Much like C’s inline Assembly. A Racket’s inline C maybe.



laurent.orseau
2020-11-27 13:26:05

That would be very unsafe :slightly_smiling_face:


spdegabrielle
2020-11-27 13:26:11

The Racket Foreign Interface Eli Barzilay (require ffi/unsafe) package: base The ffi/unsafe library enables the direct use of C-based APIs within Racket programs—without writing any new C code. From the Racket perspective, functions and data with a C-based API are foreign, hence the term foreign interface. Furthermore, since most APIs consist mostly of functions, the foreign interface is sometimes called a foreign function interface, abbreviated FFI.


laurent.orseau
2020-11-27 13:27:02

That could be done with a new reader though I guess


wanpeebaw
2020-11-27 13:27:57

C is unsafe already. Using C through FFI doesn’t help either.


laurent.orseau
2020-11-27 13:28:15

true


mflatt
2020-11-27 13:31:26

Besides ffi/unsafe, the main way Racket exposes a machine level is through unsafe functions. If you write code with a lot of unsafe-fx… and unsafe-vector*-… operations, for example, then the machine code generated by Racket CS (via Chez Scheme) is similar to the kind of code that a C compiler would generate for integer arithmetic and array access.


laurent.orseau
2020-11-27 13:42:55

Just saw this too: https://docs.racket-lang.org/reference/linklets.html?q=unsafe#%28tech._unsafe._mode%29 Though I don’t yet know how to write code that plays well with this



phanthero
2020-11-27 13:45:02

Where can I learn more about the Racket compiler? I’ve taken an introductory course that assembles C down to Assembly and then to binary, but curious if I could sort of understand how Racket’s JIT thing works as well


laurent.orseau
2020-11-27 13:46:37

Now that Racket is based on Chez-Scheme, the JIT is gone AFAIU, and compilation is different. I guess this is an intro, but that’s not going to be super helpful I’m afraid: https://docs.racket-lang.org/reference/linklets.html?q=unsafe


phanthero
2020-11-27 13:47:46

LOL, I guess that Wikipedia page needs to be updated then: > The Racket platform provides an implementation of the Racket language (including a <https://en.wikipedia.org/wiki/Runtime_system|runtime system>,https://en.wikipedia.org/wiki/Racket_(programming_language)#cite_note-mred-12\|[12] libraries, and <https://en.wikipedia.org/wiki/Just-in-time_compilation|JIT compiler>) along with the DrRacket <https://en.wikipedia.org/wiki/Integrated_development_environment|integrated development environment> (IDE) written in Racket.https://en.wikipedia.org/wiki/Racket_(programming_language)#cite_note-drscheme-13\|[13] Racket is used by the https://en.wikipedia.org/wiki/ProgramByDesign\|ProgramByDesign outreach program, which aims to turn <https://en.wikipedia.org/wiki/Computer_science|computer science> into “an indispensable part of the <https://en.wikipedia.org/wiki/Liberal_arts|liberal arts> https://en.wikipedia.org/wiki/Curriculum\|curriculum”.https://en.wikipedia.org/wiki/Racket_(programming_language)#cite_note-teachscheme-14\|[14]https://en.wikipedia.org/wiki/Racket_(programming_language)#cite_note-15\|[15]


phanthero
2020-11-27 13:48:11

But thank you for this as well!


laurent.orseau
2020-11-27 13:48:58

Well, the JIT still exists in the BC version, but it’s not going to be improved anymore I suppose.


phanthero
2020-11-27 13:49:01

Many people at my uni just refuse to learn Emacs and keep using IDEs which take all the fun away


laurent.orseau
2020-11-27 13:49:39

We need to embed Emacs within DrRacket :smile:


phanthero
2020-11-27 13:49:42

Yea, probably the Wikipedia page would need to be updated once Racket 8.0 makes the non-JIT BC version the default


spdegabrielle
2020-11-27 13:50:46

Stephen De Gabrielle and Sam Phillips are organising a Racket Users Video Meetup in Gather (the same awesome platform used for RacketCon). The first one will take place November 28 at 11am PT (or 8pm CET). You’ll need Chrome or Firefox as Safari apparently doesn’t work. Agenda: Lightning talks (< 5mins) - What have you been working on? (optional). Paper for discussion (see featured paper): “Adding Interactive Visual Syntax to Textual Code” by Leif Andersen, (OOPSLA 2020). Agree date for next meet-up (every 6–12 weeks?).

https://gather.town/app/wH1EDG3McffLjrs0/racket-users\|https://gather.town/app/wH1EDG3McffLjrs0/racket-users

https://racket-news.com/2020/11/racket-news-issue-42.html#featuredpaper\|https://racket-news.com/2020/11/racket-news-issue-42.html#featuredpaper



spdegabrielle
2020-11-27 13:54:12

All welcome @phanthero


spdegabrielle
2020-11-27 13:56:22

There is also a Racket Taiwan discord


spdegabrielle
2020-11-27 13:56:26

There is now a Racket Taiwan Discord at https://discord.gg/xpwzAcx\|https://discord.gg/xpwzAcx - it accepts several languages: English, Chinese, and many languages in Taiwan! :100::smiley::+1:


spdegabrielle
2020-11-27 13:57:47

I’d like to trial a Racket Discourse as a alternative to the racket-users google group.


spdegabrielle
2020-11-27 13:58:20

(Discourse has both web and email access)


spdegabrielle
2020-11-27 13:59:09

phanthero
2020-11-27 14:07:14

All of these should be mentioned on http://racket-lang.org\|racket-lang.org ’s Community tab as well!


samth
2020-11-27 15:10:26

I think the short answer is that Racket has a lot of inertia on Slack right now, and so there’s not much impetus to switch.


samth
2020-11-27 15:11:22

As for Discourse, I like it but I think using Github Discussions is a more likely move. However, lots of Racketeers are pretty attached to email.


spdegabrielle
2020-11-27 15:24:11

Discourse is more like GG in that you can interact via email - that’s why I keep coming back to it. (The OSS aspect is also attractive)



samth
2020-11-27 16:00:45

This was an awesome interview


sorawee
2020-11-27 16:04:41

> Matthew has run out of immediate tasks, and he is hoping to spend less time at the level of the compiler and runtime system. Now that Racket CS has generally caught up and stabilized, he hopes to go back more to language design via the Rhombus project.


samth
2020-11-27 16:05:22

Yes, I hope that’s really true (although I’m confident we can come up with more tasks for him :slightly_smiling_face:


dvanhorn
2020-11-27 16:08:31

bug reports will now be fixed in 4 minutes instead of 2.


massung
2020-11-27 17:41:22

Anyone know how I can “back up” a string output port? For example: (let ([p (open-output-string)]) (write-string "Ack!" p) (file-position p 3) (get-output-string p)) ;=&gt; "Ack!" I’d very much like it to return “Ack” instead. But I can’t update the file-position, nor can I file-truncate. I’d very much like to not resort to building my string by constantly appending characters and backing up using substring in order to lop off one character.

Or perhaps there’s a specialized string builder type in Racket I’m not aware of?


soegaard2
2020-11-27 17:58:38

Maybe you can use get-output-bytes instead?

It works in your example, but I suspect there could be problems if non-ascii characters have en been printed.

(let ([p (open-output-string)])
  (write-string "Ack!" p)
  (file-position p 3)
  (get-output-bytes p #f 0 3))

soegaard2
2020-11-27 17:59:37

However - the information must there somewhere - so perhaps get-output-string could be extended?


popa.bogdanp
2020-11-27 18:02:48

Looks like a bug to me:

&gt; (define p (open-output-string)) &gt; (display "hello!" p) &gt; (file-position p) 6 &gt; (file-position p 5) &gt; (display " there" p) &gt; (get-output-string p) "hello there"


popa.bogdanp
2020-11-27 18:03:02

vs: &gt; (define p (open-output-string)) &gt; (display "hello!" p) &gt; (file-position p 5) &gt; (get-output-string p) "hello!" I’d expect get-output-string to not read past the cursor.


soegaard2
2020-11-27 18:03:09

If I am reading the correct code, the implementation of get-output-string is: (define/who (get-output-string o) (check who (lambda (v) (and (output-port? o) (string-port? o))) #:contract "(and/c output-port? string-port?)" o) (bytes-&gt;string/utf-8 (get-output-bytes o) #\uFFFD))


soegaard2
2020-11-27 18:04:18

@popa.bogdanp What does Chez Scheme return for your example?


popa.bogdanp
2020-11-27 18:05:23

The same thing for the 2nd example and a corrupted string for the first. I’m assuming file-position is not supposed to work under Chez, but Racket CS’s ports do support it (and it’s mentioned in the docs).


popa.bogdanp
2020-11-27 18:06:07

~&gt; scheme Chez Scheme Version 9.5.3.41 Copyright 1984-2019 Cisco Systems, Inc. &gt; (define p (open-output-string)) &gt; (display "hello!" p) &gt; (file-position p 5) &gt; (get-output-string p) "hello!" &gt; (file-position p 5) &gt; (display " there" p) &gt; (get-output-string p) "\x0;\x0;\x0;\x0;\x0; there" &gt;


popa.bogdanp
2020-11-27 18:07:04

oh, actually, it emptied out the buffer when I ran get-output-string, so the port wasn’t corrupted. This works:


popa.bogdanp
2020-11-27 18:07:11

&gt; (define p (open-output-string)) &gt; (display "hello!" p) &gt; (file-position p 5) &gt; (display " there" p) &gt; (get-output-string p) "hello there"


popa.bogdanp
2020-11-27 18:11:13

Looks like it’s behaving as intended, after all. From the docs for get-output-bytes:

> Returns the bytes accumulated in the <dfile:///Users/bogdan/Library/Application%20Support/Dash/DocSets/Racket/Racket.docset/Contents/Resources/Documents/docs.racket-lang.org/reference/stringport.html#%28tech._string.port%29|string port> _out so far in a freshly allocated <dfile:///Users/bogdan/Library/Application%20Support/Dash/DocSets/Racket/Racket.docset/Contents/Resources/Documents/docs.racket-lang.org/reference/bytestrings.html#%28tech._byte._string%29|byte string> (including any bytes written after the port’s current position, if any).


popa.bogdanp
2020-11-27 18:13:56

So you can use file-position to go back and overwrite data in the port, but you have to be careful and supply the position as the end position to get-output-bytes (or get-output-string if it’s changed to take a start and end pos) when you’re done.


massung
2020-11-27 18:18:07

well, good to know


massung
2020-11-27 18:22:17

Yep, using (get-output-bytes port #f 0 (file-position port)) works for me


massung
2020-11-27 18:22:19

thanks!


wanpeebaw
2020-11-28 01:54:19

I know that algorithms are important. However, when you must tweak performance when the algorithm is given, this is just not the answer.


wanpeebaw
2020-11-28 02:07:30

@mflatt Why are there no unsafe-bytes*-?


mflatt
2020-11-28 02:09:29

The * in vector* means “not impersonated”. Byte strings can’t be impersonated, so no * is needed.