4 ms·
Fast Finite State Machine for HTTP Parsing (2014)
- userbinator 11y agoState machines are really the ideal application of goto, where the semantics map directly - go to this state, making the flow of the code very clear. The performance benefits are nice too, so in that sense I think it's beneficial for both the machine and the programmer. It'd be funny to see the results of an implementation with "proper OOP design using the State design pattern", I have a feeling that could be worse than the original switch/loop.
- radicalbyte 11y agoIt depends, I prefer switches with a single line, which call out to some service to be preferable to the State pattern. This is because the State pattern encourages you to design these "everything and the kitchen sink" style classes. It's a bit crazy to implement the State pattern than have the various state object just do single calls (or no-ops) when the switching has been implemented to be readable. Note: if your language doesn't support enumerations and switching over them then the OO pattern may be better. If I didn't had static analysis telling me when I've missed a state then it's a bug waiting to happen. The State pattern makes a missing state hard to miss ;)
- chubot 11y agoI've been using re2c lately and it's really great. It's this gem from the 90's that not a lot of people know about; I encountered it in the Ninja build system, where they were concerned about parsing megabytes of text very quickly. It basically generates a bunch of gotos from readable regexes. http://re2c.org/ http://re2c.org/ One thing I haven't explored is "push vs pull" scanners. It's basically the same thing as threads vs. async, so it could be really useful for async servers. Normal scanners will pull data in when needed, but if you pass -f, it can apparently have data pushed upon it. I guess Ragel can be used easily like this since Mongrel is async (?)
- Puts 11y agoThe thing that really makes GOTOs feel right for state machines is that you are not wasting any CPU cycles for things that can be made pre-compilation. A transition table solution for example would be a great solution if it was built in to the language and the code controlling the flow of the program was generated during compilation and not in real time every time the program is executing. The only reason you would want to generate the states in real time during execution is if you would need a totally unpredictable state machine, and that's not what state machines are good for. :D
- deleted 11y ago[deleted]
- zzzcpan 11y ago> There was successful attempt to generate simple HTTP parsers using Ragel, but the parsers are limited in functionality. They are not limited in any way. You are free to combine as many state machines as you want with as convoluted logic as you want. But it takes some time to learn Ragel.
- MichaelGG 11y agoThis is another reason these text style protocols are poorly designed. In addition to ending up ambiguous in practice, in addition to not being standard because of "liberal implementations", we get this terrible performance to boot. SIP has the same issue. Though most VoIP companies don't have much bandwidth and you can just saturate them. But if they manage to filter that, overloading the actual system is not a difficult task.
- chubot 11y agoDesign has many dimensions; performance is only one of them. Text protocols tend to grow ambiguities, but that's because they're evolvable. That's a feature, not a bug. A binary protocol would likely have already collapsed under its own weight. (Name a binary application level protocol that has achieved adoption as wide as HTTP) HTTP also wasn't designed from scratch. The message format is shared with e-mail (RFC 2822, RFC 822), presumably so people could reuse existing code. HTML wasn't designed from scratch either; it was based on SGML. There are lots of good reasons that e-mail was text. You're basically arguing against nature. Humans ostensibly design protocols, but it's really the laws of evolution and economics (nature) that determine which ones survive. It's like complaining that human knees and backs are fragile... yeah that's true, but there's nothing you could have done about it.
- userbinator 11y agoName a binary application level protocol that has achieved adoption as wide as HTTP DNS?
- andrewflnr 11y agoThat's what I thought first, but it's pretty reasonable to say that DNS is not an application level protocol. It's not a transport protocol, but feels like it belongs at the same "height" as TCP.
- magila 11y ago> That's a feature, not a bug. No, it's definitely a bug. You can have extensiblity without ambiguity, see the examples below... > Name a binary application level protocol that has achieved adoption as wide as HTTP TLS, SSH, DHCP, DNS
- js8 11y agoI think that http://www.ucw.cz/holmes/ http://www.ucw.cz/holmes/ has (or had) an HTTP parser based on perfect hashing of strings for maybe 15 years. It was written by some very smart people and there are lot of tricks like that in these sources.
- amelius 11y agoOf course it is way more elegant to use coroutines for parsing of stream data.
- nickpsecurity 11y agoTwo other benefits to the FSM-based methods: 1. Easier to apply formal verification or at least model-checking to the parsers. 2. Synthesis methods are available to directly generate hardware logic from several types of FSM's. So, using the FSM methods gives an immediate benefit plus potential for future benefits.