Fast string management

hello, I’m new to Elixir and I found it exceptional (I’m spreading the words among collegues)

I need to build a websocket based server, and I want to do it using Phoenix.

My question, I need to build a parser of commands over strings passed in and out, however I know that ElixirErlangBeam is not efficient as C to manage strings (copy, strip, delimited lists, search…). Guess I have to build a multi-party game server.
Avoiding to use NIFS, can you suggest me the most optimized way to do strings management eg. build a protocol?
Thank you
kind regards

Elixir may not be the absolute fastest, but it is very expressive when it comes to parsing and building protocols due the power of binary pattern matching. One good place to start is this blog post on using binary pattern matching to parse a PNG file.

2 Likes

What’s your exact use case? It might just turn out Elixir could be fast enough for you.

thanks for your answers

binary pattern matching can be used also for “set” or only to “get” values?

A simple case of string management I need, get and set, guess a very basilar pattern:

##CMD##one:two;three:four;five:six##CMD##
how to build a similar string from pieces and put it to socket send (concat() or +?)
how to retrieve and split a socket recv string into list from delimiters ( : ; )
so compare, pos, split to list giving delimiters, concat, uppercase, lowercase, utf8 encode decode etc.

thanks
btw. perhaps I shoud take a look into phoenix source in URI, querystring, contentfields management

:wave:

how to retrieve and split a socket recv string into list from delimiters ( : ; )

Check out GitHub - dashbitco/nimble_parsec: A simple and fast library for text-based parser combinators

This discussion might be interesting for you as well: How to properly parse a list of lines, with 'look-ahead' functionality?

how to build a similar string from pieces and put it to socket send (concat() or +?)

Read up on iolists in elixir and erlang:

https://www.evanmiller.org/elixir-ram-and-the-template-of-doom.html

5 Likes

thanks
btw. can I ask why do you use a offensive nickname (idiot)?

I couldn’t come up with anything else.

3 Likes

@idi527 I see what you were doing there. Subtly clever :smirk:

Hi,
While I think @idi527 ’ s answer was really good (especially anything pointing to io lists), and would add http://erlang.org/doc/efficiency_guide/binaryhandling.html .

I’m a bit worried that it might lead you to a less than optimal path :

First you’re talking about setting values, and, well, generally speaking in erlang or elixir you can’t set values. Values are immutable. That doesn’t mean that there are no ways to optimise here (as per @idi527 's comment) but, it is generally not the same framework.

Second, and mainly (granted I might be wrong, I don’t know where your project stands right now), you might be optimising for the wrong thing. Don’t get me wrong, I believe in local optimisation, I even love to do it, but the way you design your application will probably have much more impact.

Now, I don’t know much about mutliplayer game server (if it’s your actual use case), but I would assume that you would have to handle state for each player, and a global state for the world. So I would assume something like genserver (or channel) for each player, and a global state (backed by maybe a genserver, maybe a set of ets tables, that would communicate with pg ?). I don’t know exactly, but I would focus on this general design first, way before I tried to optimise locally.

Again, you may have done all that, in which case I’m sorry to point to that, but keep in mind that elixir/erlang is somewhat high level, is mostly immutable, and while local optimisation is very much possible and desirable, it should probably come last (while keeping it in mind while designing).

Hope it makes sense.

3 Likes

thanks for answers

do you know about some repositories with similar projects made open in Elixir?
As telegram clone, or multiplayer rpg clone, or also webrtc conference signaling…

anyway I need to study deeply elixir/erlang