Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: support pass table to send_query #68

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

yanxurui
Copy link

problem

Luajit is poor at creating a lot of similar strings due to hash collisions. The discussion can be seen in Github: Reduce string hash collisions.
So it consumes a lot cpu when construct sql statements dynamically.
This problem has already been asked on openresty-cn's google group:

solution

Fortunately, cosocket's send method can receive a table of strings and it will handle it in C land. A temporary solution is to avoid string creation in lua and pass the table of sql segments into cosocket's send method just like resty-redis does.

result

In my application, the performance(QPS) improves 50%.

The flame graph proves that the lj_new_str is no longger the bottleneck:
Before:
before
After:
after
I hopes this can help others.

return #a
end
local n = 0
for i, v in pairs(a) do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yanxurui Typo? Maybe you should use ipairs() here instead? The pairs() loop cannot be JIT compiled.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I thought pairs can include non-numeric indexed items in a table but it's wrong because concat or cosocket:send will ignore non-numeric items.

local function _send_packet(self, req, size)
local sock = self.sock

self.packet_no = self.packet_no + 1

-- print("packet no: ", self.packet_no)

local packet = _set_byte3(size) .. strchar(band(self.packet_no, 255)) .. req
local packet = {_set_byte3(size), strchar(band(self.packet_no, 255)), req}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One sad thing is this extra allocation of a new Lua temp table. Maybe we can reuse this table across different function calls?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right.
I put the table in the module level instead of instance because multiple instances can share it.
This looks dirty but I can's find a better way.


local bytes, err = _send_packet(self, cmd_packet, packet_len)
local bytes, err = _send_packet(self, {strchar(COM_QUERY), query}, packet_len)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto.

local function _concat_rec(t, sep)
for i, v in ipairs(t) do
if type(v) == "table" then
t[i] = _concat_rec(v, sep)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can avoid this recursive concat call?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you mean change recursive to iterative? I don'k know which performs well but surely recursive method is more intuitive and requires less code.

I think this function doesn't matter because it's only called when debug.

@agentzh
Copy link
Member

agentzh commented Nov 10, 2017

@yanxurui Are you using recent versions of OpenResty? Recent versions of OpenResty use our own branch of LuaJIT with special optimizations which significantly reduce the hash collisions in Lua string table on Intel CPUs with SSE 4.2 support. I suggest you try it out, for example, the latest 1.11.3.6.1 RC1 release below:

https://openresty.org/download/openresty-1.13.6.1rc1.tar.gz

But sure, reducing the number of lj_str_new() calls here will definitely help more.

@yanxurui
Copy link
Author

@agentzh
You are totally right.
I tried 1.11.3.6.1 RC1 and it works very well. Flame graph shows lj_str_new is not the bottleneck.
lua flame graph

It seems there is no need to avoid string creation in lua land. So this pull request may be not necessary.
Nonetheless I still fix the problems you pointed out above and I wish you to decide what to do with this pull request.

Thank you very much for your help.

@agentzh
Copy link
Member

agentzh commented Nov 11, 2017

@yanxurui I'm glad that the latest OpenResty addresses your performance issue. I think I'd like to leave this PR as is for now. We do have plans to rewrite this library (as well as other lua-resty-* libraries talking to postgres, memcached, redis, dns, and etc) for maximum performance. Our wirelang DSL compiler will emit highly optimized C code and the Lua wrapper and glue automatically from wire protocol specs. Let the machine do the coding for us humans :)

@yanxurui
Copy link
Author

Thank you for your awesome job in openresty community.
Automatically generate code for lua-resty-*??? It sounds incredible!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants