Top Banner
Rewriting LuaJIT: Why and How? Anton Soldatov, IPONWEB Lua Workshop Kaunas, 06.09.2018
50

Kaunas, 06.09.2018 Lua Workshop · Kaunas, 06.09.2018. About IPONWEB Building platforms for real-time advertising Workloads up to 6M requests per second Lua is used for more than

Aug 05, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Kaunas, 06.09.2018 Lua Workshop · Kaunas, 06.09.2018. About IPONWEB Building platforms for real-time advertising Workloads up to 6M requests per second Lua is used for more than

Rewriting LuaJIT: Why and How?Anton Soldatov, IPONWEB

Lua WorkshopKaunas, 06.09.2018

Page 2: Kaunas, 06.09.2018 Lua Workshop · Kaunas, 06.09.2018. About IPONWEB Building platforms for real-time advertising Workloads up to 6M requests per second Lua is used for more than

About IPONWEB

● Building platforms for real-time advertising

● Workloads up to 6M requests per second

● Lua is used for more than 10 years for implementing

business logic in our projects

2

Page 3: Kaunas, 06.09.2018 Lua Workshop · Kaunas, 06.09.2018. About IPONWEB Building platforms for real-time advertising Workloads up to 6M requests per second Lua is used for more than

Application server core (C++):multithreading, networking, coroutine

management, etc.

Business logic (Lua, sandboxed)

Lua in IPONWEB

3

Libraries (Lua) Libraries (C/C++)

Page 4: Kaunas, 06.09.2018 Lua Workshop · Kaunas, 06.09.2018. About IPONWEB Building platforms for real-time advertising Workloads up to 6M requests per second Lua is used for more than

● Experience with LuaJIT 1.1 and 2.0

LuaJIT in IPONWEB

4

Page 5: Kaunas, 06.09.2018 Lua Workshop · Kaunas, 06.09.2018. About IPONWEB Building platforms for real-time advertising Workloads up to 6M requests per second Lua is used for more than

● Experience with LuaJIT 1.1 and 2.0

● Definitely benefit from the faster interpreter

LuaJIT in IPONWEB

5

Page 6: Kaunas, 06.09.2018 Lua Workshop · Kaunas, 06.09.2018. About IPONWEB Building platforms for real-time advertising Workloads up to 6M requests per second Lua is used for more than

LuaJIT in IPONWEB

● Experience with LuaJIT 1.1 and 2.0

● Definitely benefit from the faster interpreter

● Generally benefit from the JIT compiler

6

Page 7: Kaunas, 06.09.2018 Lua Workshop · Kaunas, 06.09.2018. About IPONWEB Building platforms for real-time advertising Workloads up to 6M requests per second Lua is used for more than

LuaJIT in IPONWEB

● Experience with LuaJIT 1.1 and 2.0

● Definitely benefit from the faster interpreter

● Generally benefit from the JIT compiler

○ ...but we do not avoid pairs at all costs

7

Page 8: Kaunas, 06.09.2018 Lua Workshop · Kaunas, 06.09.2018. About IPONWEB Building platforms for real-time advertising Workloads up to 6M requests per second Lua is used for more than

LuaJIT in IPONWEB

● Experience with LuaJIT 1.1 and 2.0

● Definitely benefit from the faster interpreter

● Generally benefit from the JIT compiler

○ ...but we do not avoid pairs at all costs

● Sandboxing partly reduces Lua/LuaJIT incompatibility tension

8

Page 9: Kaunas, 06.09.2018 Lua Workshop · Kaunas, 06.09.2018. About IPONWEB Building platforms for real-time advertising Workloads up to 6M requests per second Lua is used for more than

LuaJIT in IPONWEB

● Experience with LuaJIT 1.1 and 2.0

● Definitely benefit from the faster interpreter

● Generally benefit from the JIT compiler

○ ...but we do not avoid pairs at all costs

● Sandboxing partly reduces Lua/LuaJIT incompatibility tension

● Limited experience with FFI9

Page 10: Kaunas, 06.09.2018 Lua Workshop · Kaunas, 06.09.2018. About IPONWEB Building platforms for real-time advertising Workloads up to 6M requests per second Lua is used for more than

Data feeds

● Inventory lists

● Rules for decision making

10

Page 11: Kaunas, 06.09.2018 Lua Workshop · Kaunas, 06.09.2018. About IPONWEB Building platforms for real-time advertising Workloads up to 6M requests per second Lua is used for more than

Data feeds: memory consumption

11

Page 12: Kaunas, 06.09.2018 Lua Workshop · Kaunas, 06.09.2018. About IPONWEB Building platforms for real-time advertising Workloads up to 6M requests per second Lua is used for more than

Our main issue with LuaJIT 2.0

We have eventually hit the memory limit on x86-64:

void *ptr = mmap((void *)MMAP_REGION_START, size, MMAP_PROT, MAP_32BIT|MMAP_FLAGS, -1, 0);

12

Page 13: Kaunas, 06.09.2018 Lua Workshop · Kaunas, 06.09.2018. About IPONWEB Building platforms for real-time advertising Workloads up to 6M requests per second Lua is used for more than

Tried a work-around with FFI

● Decompose feeds into simpler data structures

● Map into native memory

● Build accessors with FFI

13

Page 14: Kaunas, 06.09.2018 Lua Workshop · Kaunas, 06.09.2018. About IPONWEB Building platforms for real-time advertising Workloads up to 6M requests per second Lua is used for more than

Tried a work-around with FFI

● Decompose feeds into simpler data structures

● Map into native memory

● Build accessors with FFI

● Performance has degraded

14

Page 15: Kaunas, 06.09.2018 Lua Workshop · Kaunas, 06.09.2018. About IPONWEB Building platforms for real-time advertising Workloads up to 6M requests per second Lua is used for more than

Possible solutions

● Migrate to PUC-Rio Lua

15

Page 16: Kaunas, 06.09.2018 Lua Workshop · Kaunas, 06.09.2018. About IPONWEB Building platforms for real-time advertising Workloads up to 6M requests per second Lua is used for more than

Possible solutions

● Migrate to PUC-Rio Lua

● Migrate to LuaJIT 2.1

16

Page 17: Kaunas, 06.09.2018 Lua Workshop · Kaunas, 06.09.2018. About IPONWEB Building platforms for real-time advertising Workloads up to 6M requests per second Lua is used for more than

Possible solutions

● Migrate to PUC-Rio Lua

● Migrate to LuaJIT 2.1

● Start an own implementation

17

Page 18: Kaunas, 06.09.2018 Lua Workshop · Kaunas, 06.09.2018. About IPONWEB Building platforms for real-time advertising Workloads up to 6M requests per second Lua is used for more than

Implementation requirements

● Fix the memory limit

● Become not slower than LuaJIT 2.0

● Target only Linux x86-64

18

Page 19: Kaunas, 06.09.2018 Lua Workshop · Kaunas, 06.09.2018. About IPONWEB Building platforms for real-time advertising Workloads up to 6M requests per second Lua is used for more than

Implementation requirements

● Fix the memory limit

● Become not slower than LuaJIT 2.0

● Target only Linux x86-64

● No changes to the language

● Stay as close to Lua 5.1 as LuaJIT 2.019

Page 20: Kaunas, 06.09.2018 Lua Workshop · Kaunas, 06.09.2018. About IPONWEB Building platforms for real-time advertising Workloads up to 6M requests per second Lua is used for more than

● TValue is 16 bytes (uint64_t + uint64_t)

Fixing the memory limit

20

Page 21: Kaunas, 06.09.2018 Lua Workshop · Kaunas, 06.09.2018. About IPONWEB Building platforms for real-time advertising Workloads up to 6M requests per second Lua is used for more than

● TValue is 16 bytes (uint64_t + uint64_t)

● Support for true 64-bit pointers in both VM and JIT

Fixing the memory limit

21

Page 22: Kaunas, 06.09.2018 Lua Workshop · Kaunas, 06.09.2018. About IPONWEB Building platforms for real-time advertising Workloads up to 6M requests per second Lua is used for more than

● TValue is 16 bytes (uint64_t + uint64_t)

● Support for true 64-bit pointers in both VM and JIT

● LJ_FR2 trick not needed

Fixing the memory limit

22

Page 23: Kaunas, 06.09.2018 Lua Workshop · Kaunas, 06.09.2018. About IPONWEB Building platforms for real-time advertising Workloads up to 6M requests per second Lua is used for more than

● TValue is 16 bytes (uint64_t + uint64_t)

● Support for true 64-bit pointers in both VM and JIT

● LJ_FR2 trick not needed

● A multiplier of 2 was "baked" into the byte code to regain

the SIB mode benefits

Fixing the memory limit

23

Page 24: Kaunas, 06.09.2018 Lua Workshop · Kaunas, 06.09.2018. About IPONWEB Building platforms for real-time advertising Workloads up to 6M requests per second Lua is used for more than

Fixing the memory limit: results

● About 30% faster than the FFI work-around for data feeds

● Approximately the same performance in most of other

cases

24

Page 25: Kaunas, 06.09.2018 Lua Workshop · Kaunas, 06.09.2018. About IPONWEB Building platforms for real-time advertising Workloads up to 6M requests per second Lua is used for more than

Fixing the memory limit: timeline

● Q2 2015 – Decision to build a new implementation

● 2015-2016 – Development, stabilisation and validation;

pilot migrations

● Q1 2017 – More than 95% production servers moved to

the new implementation25

Page 26: Kaunas, 06.09.2018 Lua Workshop · Kaunas, 06.09.2018. About IPONWEB Building platforms for real-time advertising Workloads up to 6M requests per second Lua is used for more than

Testing: What we started with

● Integrational tests with the application server

● Test stands

26

Page 27: Kaunas, 06.09.2018 Lua Workshop · Kaunas, 06.09.2018. About IPONWEB Building platforms for real-time advertising Workloads up to 6M requests per second Lua is used for more than

● Language compliance tests

Testing: What was missing

27

Page 28: Kaunas, 06.09.2018 Lua Workshop · Kaunas, 06.09.2018. About IPONWEB Building platforms for real-time advertising Workloads up to 6M requests per second Lua is used for more than

Testing: What was missing

● Language compliance tests

● There are test suites for some implementations, but they

are scattered around

28

Page 29: Kaunas, 06.09.2018 Lua Workshop · Kaunas, 06.09.2018. About IPONWEB Building platforms for real-time advertising Workloads up to 6M requests per second Lua is used for more than

Testing: What was missing

● Language compliance tests

● There are test suites for some implementations, but they

are scattered around

● Functional tests for the implementation

29

Page 30: Kaunas, 06.09.2018 Lua Workshop · Kaunas, 06.09.2018. About IPONWEB Building platforms for real-time advertising Workloads up to 6M requests per second Lua is used for more than

● Continuously write own tests

Testing: results

30

Page 31: Kaunas, 06.09.2018 Lua Workshop · Kaunas, 06.09.2018. About IPONWEB Building platforms for real-time advertising Workloads up to 6M requests per second Lua is used for more than

Testing: results

● Continuously write own tests

● Integrate third party suites into the source tree:

○ Lua 5.1 official test suite

○ Mike Pall's test suite for LuaJIT

○ François Perrad's test suite shipped with lua-TestMore

31

Page 32: Kaunas, 06.09.2018 Lua Workshop · Kaunas, 06.09.2018. About IPONWEB Building platforms for real-time advertising Workloads up to 6M requests per second Lua is used for more than

Testing: results

● Continuously write own tests

● Integrate third party suites into the source tree:

○ Lua 5.1 official test suite

○ Mike Pall's test suite for LuaJIT

○ François Perrad's test suite shipped with lua-TestMore

○ Part of Laurent Deniau's test suite from the MAD project32

Page 33: Kaunas, 06.09.2018 Lua Workshop · Kaunas, 06.09.2018. About IPONWEB Building platforms for real-time advertising Workloads up to 6M requests per second Lua is used for more than

Extending the implementation

33

Page 34: Kaunas, 06.09.2018 Lua Workshop · Kaunas, 06.09.2018. About IPONWEB Building platforms for real-time advertising Workloads up to 6M requests per second Lua is used for more than

Data feeds: memory consumption

34

Page 35: Kaunas, 06.09.2018 Lua Workshop · Kaunas, 06.09.2018. About IPONWEB Building platforms for real-time advertising Workloads up to 6M requests per second Lua is used for more than

Objects from a data feed in memory

35

Page 36: Kaunas, 06.09.2018 Lua Workshop · Kaunas, 06.09.2018. About IPONWEB Building platforms for real-time advertising Workloads up to 6M requests per second Lua is used for more than

ujit.seal(data)

36

Page 37: Kaunas, 06.09.2018 Lua Workshop · Kaunas, 06.09.2018. About IPONWEB Building platforms for real-time advertising Workloads up to 6M requests per second Lua is used for more than

Properties of sealed objects

● GC traverses objects until the first sealed object

● Thus, all sealed objects must be immutable

37

Page 38: Kaunas, 06.09.2018 Lua Workshop · Kaunas, 06.09.2018. About IPONWEB Building platforms for real-time advertising Workloads up to 6M requests per second Lua is used for more than

ujit.seal(data)

38

Page 39: Kaunas, 06.09.2018 Lua Workshop · Kaunas, 06.09.2018. About IPONWEB Building platforms for real-time advertising Workloads up to 6M requests per second Lua is used for more than

seal = "seal per se" + immutable

39

Page 40: Kaunas, 06.09.2018 Lua Workshop · Kaunas, 06.09.2018. About IPONWEB Building platforms for real-time advertising Workloads up to 6M requests per second Lua is used for more than

local t = {foo = "bar"} ujit.immutable(t)

Introducing immutable objects

40

Page 41: Kaunas, 06.09.2018 Lua Workshop · Kaunas, 06.09.2018. About IPONWEB Building platforms for real-time advertising Workloads up to 6M requests per second Lua is used for more than

local t = ujit.immutable({{foo = "bar"}})

-- All of the following throw:

t[1].new = "baz" -- Addt[1].foo = "baz" -- Modifyt[1].foo = nil -- Remove

immutable: Example 1

41

Page 42: Kaunas, 06.09.2018 Lua Workshop · Kaunas, 06.09.2018. About IPONWEB Building platforms for real-time advertising Workloads up to 6M requests per second Lua is used for more than

ujit.immutable(_G)

immutable: Example 2

42

Page 43: Kaunas, 06.09.2018 Lua Workshop · Kaunas, 06.09.2018. About IPONWEB Building platforms for real-time advertising Workloads up to 6M requests per second Lua is used for more than

● Sharing read-only data between instances of the VM

Going further: more features

43

Page 44: Kaunas, 06.09.2018 Lua Workshop · Kaunas, 06.09.2018. About IPONWEB Building platforms for real-time advertising Workloads up to 6M requests per second Lua is used for more than

Going further: more features

● Sharing read-only data between instances of the VM

● Interruptible coroutines (non-resumable and resumable)

44

Page 45: Kaunas, 06.09.2018 Lua Workshop · Kaunas, 06.09.2018. About IPONWEB Building platforms for real-time advertising Workloads up to 6M requests per second Lua is used for more than

Going further: more features

● Sharing read-only data between instances of the VM

● Interruptible coroutines (non-resumable and resumable)

● Extended Lua and C API for working with tables

45

Page 46: Kaunas, 06.09.2018 Lua Workshop · Kaunas, 06.09.2018. About IPONWEB Building platforms for real-time advertising Workloads up to 6M requests per second Lua is used for more than

Going further: more features

● Sharing read-only data between instances of the VM

● Interruptible coroutines (non-resumable and resumable)

● Extended Lua and C API for working with tables

● Tweaks in the compiler

46

Page 47: Kaunas, 06.09.2018 Lua Workshop · Kaunas, 06.09.2018. About IPONWEB Building platforms for real-time advertising Workloads up to 6M requests per second Lua is used for more than

Going further: tools

● Sampling profiler

● bit.ly/dumpanalyze – a tool for analyzing debugging info

produced by the JIT compiler (-jdump in LuaJIT)

47

Page 48: Kaunas, 06.09.2018 Lua Workshop · Kaunas, 06.09.2018. About IPONWEB Building platforms for real-time advertising Workloads up to 6M requests per second Lua is used for more than

Conclusions

● It is possible to build an implementation of Lua based on LuaJIT, but

your motivation should be strong enough

● Be prepared to multiple challenges (and fun) while reworking the core

● Be prepared to more challenges when it comes to testing and tools

48

Page 49: Kaunas, 06.09.2018 Lua Workshop · Kaunas, 06.09.2018. About IPONWEB Building platforms for real-time advertising Workloads up to 6M requests per second Lua is used for more than

Thank you! Questions?

49

Page 50: Kaunas, 06.09.2018 Lua Workshop · Kaunas, 06.09.2018. About IPONWEB Building platforms for real-time advertising Workloads up to 6M requests per second Lua is used for more than

Links

● bit.ly/dumpanalyze

● bit.ly/iow-lua-moscow-2017

● bit.ly/iow-hl-2016 (in Russian)

● bit.ly/iow-hl-2017 (in Russian)

[email protected]

● https://t.me/igelhaus

50