learning erlang (from a prolog dropout's perspective)

21
26-APR-2008 1000speakers:4 by JJ1BDX: Creative Commons CC-BY 3.0 licensed 1 Learning Erlang (from a Prolog dropout's perspective) Kenji Rikitake, JJ1BDX 26-APR-2008 For 1000speakers:4 conference

Upload: elliando-dias

Post on 18-Dec-2014

1.692 views

Category:

Technology


5 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Learning Erlang (from a Prolog dropout's perspective)

26-APR-2008 1000speakers:4 by JJ1BDX: Creative Commons CC-BY 3.0 licensed

1

Learning Erlang(from a Prolog dropout's perspective)

Kenji Rikitake, JJ1BDX26-APR-2008

For 1000speakers:4 conference

Page 2: Learning Erlang (from a Prolog dropout's perspective)

26-APR-2008 1000speakers:4 by JJ1BDX: Creative Commons CC-BY 3.0 licensed

2

Disclaimers

*Strictly NO WARRANTY(Ab)use this presentation at your own risk

*JJ1BDX is the author and the only responsible person for this presentation

My (former) employers or anyone else have nothing to do with this work and the contents

*This is a work-in-progressYou may find a bunch of errors and glitches

Page 3: Learning Erlang (from a Prolog dropout's perspective)

26-APR-2008 1000speakers:4 by JJ1BDX: Creative Commons CC-BY 3.0 licensed

3

Who am I?

*... I've been the JJ1BDX since 1976JJ1BDX is my amateur radio callsign in Japan

*I’ve been an Internet activist since 1986*In 1986, I was partying around with fellow radio and computer hackers, like what you guys are doing now in the 1000speakers conference and other events. So I come here to enjoy. :-)

Page 4: Learning Erlang (from a Prolog dropout's perspective)

26-APR-2008 1000speakers:4 by JJ1BDX: Creative Commons CC-BY 3.0 licensed

4

Why I didn't like prolog (in 1980s)

*programming only by matching? *programming without assignment? *you can't really compute numbers? *parallelism on the desktop?*runtime (slow) virtual machines?

...NO WAY!(and I spent too much time on NetNews)

Page 5: Learning Erlang (from a Prolog dropout's perspective)

26-APR-2008 1000speakers:4 by JJ1BDX: Creative Commons CC-BY 3.0 licensed

5

...then why Erlang NOW??

*...works fast enough on modern PCsit works OK even on Windoze! (UNIX rules)

*...has a bunch of practical applicationsyaws, ejabberd, ATM packet exchanges

*...can get the most out of parallelismthreads and shared memory are headaches

*...is new to me and I’m sure I can learn something new. That’s for sure.

Page 6: Learning Erlang (from a Prolog dropout's perspective)

26-APR-2008 1000speakers:4 by JJ1BDX: Creative Commons CC-BY 3.0 licensed

6

Why Erlang is hard to learn?

*extraordinary syntaxembedded Prolog-ism

Horn clauses and pattern-matching branchesmessage-driven parallelism

*I’m preoccupied by C and FORTRANleaning tail recursion is not a trivial tasklocal variable declaration is not explicit

*lots of new thingsmodules, data structures, Mnesia, OTP...

Page 7: Learning Erlang (from a Prolog dropout's perspective)

26-APR-2008 1000speakers:4 by JJ1BDX: Creative Commons CC-BY 3.0 licensed

7

Page 8: Learning Erlang (from a Prolog dropout's perspective)

26-APR-2008 1000speakers:4 by JJ1BDX: Creative Commons CC-BY 3.0 licensed

8

What I'm going to show you

*IPv6-related string manipulationgeneration of 10,000 random IPv6 addressesparsing IPv6 colon-notation addresses into Erlang-native tuple formsgenerating reverse-lookup names from the Erlang tuples

colon-form addresses -> ip6.arpa name

*some crude profiling resultswith parallelism: using SMP Erlang VM

Page 9: Learning Erlang (from a Prolog dropout's perspective)

26-APR-2008 1000speakers:4 by JJ1BDX: Creative Commons CC-BY 3.0 licensed

9

Page 10: Learning Erlang (from a Prolog dropout's perspective)

26-APR-2008 1000speakers:4 by JJ1BDX: Creative Commons CC-BY 3.0 licensed

10

Erlang IP address tuples

*IPv4{127,0,0,1} -> localhost(4 x 8-bit elements)

*IPv6{0,0,0,0,0,0,0,1} -> ::1 (... well, localhost)(8 16-bit big-endian elements)

*Address conversion function readyinet_parse:address() for both IPv4 and IPv6

Page 11: Learning Erlang (from a Prolog dropout's perspective)

26-APR-2008 1000speakers:4 by JJ1BDX: Creative Commons CC-BY 3.0 licensed

11

Page 12: Learning Erlang (from a Prolog dropout's perspective)

26-APR-2008 1000speakers:4 by JJ1BDX: Creative Commons CC-BY 3.0 licensed

12

Reverse lookup is getting harder in IPv6 than IPv4

*IPv4:just reversing the tuple elements is enough

127.0.0.1 -> 1.0.0.127.in-addr.arpalists:reverse() for the tuple elements

*IPv6:parsing and string manipulation needed

2001:1:3fff:dbde::1 -> 1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.e.d.b.d.f.f.f.3.1.0.0.0.1.0.0.2.ip6.arpa... binary bit pack/unpack operation is effective

Page 13: Learning Erlang (from a Prolog dropout's perspective)

26-APR-2008 1000speakers:4 by JJ1BDX: Creative Commons CC-BY 3.0 licensed

13

Page 14: Learning Erlang (from a Prolog dropout's perspective)

26-APR-2008 1000speakers:4 by JJ1BDX: Creative Commons CC-BY 3.0 licensed

14

Page 15: Learning Erlang (from a Prolog dropout's perspective)

26-APR-2008 1000speakers:4 by JJ1BDX: Creative Commons CC-BY 3.0 licensed

15

Concurrency and map()ping

*simple list iteration: map()pingApplying a function to all the list memberslists:map(fun(X) -> function(X) end, Arglist)

*parallelism: parallel map()Joe Armstrong’s pmap()

(in Programming Erlang: Software for a Concurrent World)spawn()ing light-weight process per element preserving result list sequence

caution: execution sequence implementation dependent – no side effect allowed in the function

Page 16: Learning Erlang (from a Prolog dropout's perspective)

26-APR-2008 1000speakers:4 by JJ1BDX: Creative Commons CC-BY 3.0 licensed

16

map() .vs. pmap() results* 10,000 addresses / Core2Duo 2.33GHz

(mean values of 5-time measurements)

1.202 seconds(-10.5%)

1.343 secondsSMP (2 cores)(2 schedulers)

2.029 seconds(+55.0%)

1.309 secondsnon-SMP(single scheduler)

Armstrong’spmap()

lists:map()completion timefor test functions

Page 17: Learning Erlang (from a Prolog dropout's perspective)

26-APR-2008 1000speakers:4 by JJ1BDX: Creative Commons CC-BY 3.0 licensed

17

Spawning overhead of lightweight processes

*cost of creating lightweight processes3 to 12 microseconds/process

*messaging overhead between processes*more efficient utilization of CPUs needed

granularity: per-function computationnumber of simultaneous processesefficient process spawning

e.g., result list sequence need not be preserved

Page 18: Learning Erlang (from a Prolog dropout's perspective)

26-APR-2008 1000speakers:4 by JJ1BDX: Creative Commons CC-BY 3.0 licensed

18

Conclusions and lessons

*Erlang is weird, but worth learninggetting out of the modules is essential

*parallelism works (even for 2 CPUs!)write parallelism-aware functions:

Erlang’s idioms help parallel programming

*programmers need to learn parallelismErlang’s idea applicable to other languageseffective serialization is also critical; some algorithms have to run fast (e.g., crypto)

Page 19: Learning Erlang (from a Prolog dropout's perspective)

26-APR-2008 1000speakers:4 by JJ1BDX: Creative Commons CC-BY 3.0 licensed

19

Appendix: lighter function means less parallelism gain

* 10,000 addresses / Core2Duo 2.33GHz(mean values of 5-time measurements)

*Faster implementationio_lib:format() -> primitive hex conversion

0.198 seconds(+53%)

0.129 secondsSMP (2 cores)(2 schedulers)

Armstrong’spmap()

lists:map()completion timefor test functions

Page 20: Learning Erlang (from a Prolog dropout's perspective)

26-APR-2008 1000speakers:4 by JJ1BDX: Creative Commons CC-BY 3.0 licensed

20

Page 21: Learning Erlang (from a Prolog dropout's perspective)

26-APR-2008 1000speakers:4 by JJ1BDX: Creative Commons CC-BY 3.0 licensed

21