Skip to content

Commit

Permalink
Refactor buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
sniper00 committed Apr 11, 2024
1 parent 2550ba3 commit 4319bdf
Show file tree
Hide file tree
Showing 32 changed files with 430 additions and 463 deletions.
Empty file modified build.sh
100644 → 100755
Empty file.
88 changes: 28 additions & 60 deletions common/buffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,17 +144,14 @@ namespace moon
return *this;
}

compressed_pair(size_t cap, uint16_t head)
:headreserved(head)
compressed_pair(size_t cap)
{
prepare(cap + head);
readpos = writepos = headreserved;
prepare(cap);
readpos = writepos = 0;
}

compressed_pair(compressed_pair&& other) noexcept
: flag(std::exchange(other.flag, 0))
, headreserved(std::exchange(other.headreserved, 0))
, capacity(std::exchange(other.capacity, 0))
: capacity(std::exchange(other.capacity, 0))
, readpos(std::exchange(other.readpos, 0))
, writepos(std::exchange(other.writepos, 0))
, data(std::exchange(other.data, nullptr))
Expand All @@ -169,8 +166,6 @@ namespace moon
{
if(nullptr != data)
first().deallocate(data, capacity);
flag = std::exchange(other.flag, 0);
headreserved = std::exchange(other.headreserved, 0);
capacity = std::exchange(other.capacity, 0);
readpos = std::exchange(other.readpos, 0);
writepos = std::exchange(other.writepos, 0);
Expand Down Expand Up @@ -210,7 +205,7 @@ namespace moon
return std::pair{ data + writepos, need };
}

if (writeable + readpos < need + headreserved)
if (writeable + readpos < need)
{
auto required_size = writepos + need;
required_size = next_pow2(required_size);
Expand All @@ -228,21 +223,16 @@ namespace moon
size_t readable = writepos - readpos;
if (readable != 0)
{
assert(readpos >= headreserved);
std::memmove(data + headreserved, data + readpos, readable);
std::memmove(data , data + readpos, readable);
}
readpos = headreserved;
readpos = 0;
writepos = readpos + readable;
}
return std::pair{data + writepos, need };
}

uint16_t flag = 0;
uint16_t headreserved = 0;
size_t capacity = 0;
//read position
size_t readpos = 0;
//write position
size_t writepos = 0;
pointer data = nullptr;
};
Expand All @@ -254,9 +244,7 @@ namespace moon
using pointer = typename iterator::pointer;
using const_pointer = typename const_iterator::pointer;

//websocket header max len 14 bytes.
constexpr static uint16_t DEFAULT_HEAD_RESERVE = 16;
constexpr static size_t DEFAULT_RESERVE = 128 - DEFAULT_HEAD_RESERVE;
static constexpr size_t DEFAULT_CAPACITY = 128;

enum class seek_origin
{
Expand All @@ -265,17 +253,12 @@ namespace moon
};

base_buffer()
:pair_(DEFAULT_RESERVE, DEFAULT_HEAD_RESERVE)
:pair_(DEFAULT_CAPACITY)
{
}

base_buffer(size_t reserve)
:pair_(reserve, DEFAULT_HEAD_RESERVE)
{
}

base_buffer(size_t reserve, uint16_t head_reserve)
:pair_(reserve, head_reserve)
base_buffer(size_t capacity)
:pair_(capacity)
{
}

Expand All @@ -298,8 +281,7 @@ namespace moon
base_buffer& operator=(base_buffer&& other) = default;

base_buffer clone() {
base_buffer b{ pair_.capacity , pair_.headreserved };
b.set_flag(pair_.flag);
base_buffer b{ pair_.capacity };
b.write_back(data(), size());
return b;
}
Expand Down Expand Up @@ -338,6 +320,7 @@ namespace moon

if (n > pair_.readpos)
{
assert(false);
return false;
}

Expand Down Expand Up @@ -423,26 +406,7 @@ namespace moon

void clear() noexcept
{
pair_.flag = 0;
pair_.writepos = pair_.readpos = pair_.headreserved;
}

template<typename ValueType>
void set_flag(ValueType v) noexcept
{
pair_.flag |= static_cast<uint16_t>(v);
}

template<typename ValueType>
bool has_flag(ValueType v) const noexcept
{
return ((pair_.flag & static_cast<uint16_t>(v)) != 0);
}

template<typename ValueType>
void clear_flag(ValueType v) noexcept
{
pair_.flag &= ~static_cast<uint16_t>(v);
pair_.writepos = pair_.readpos = 0;
}

void commit(std::size_t n) noexcept
Expand All @@ -455,10 +419,17 @@ namespace moon
}
}

std::pair<pointer, size_t> prepare(size_t need) {
std::pair<pointer, size_t> prepare(size_t need)
{
return pair_.prepare(need);
}

std::pair<pointer, size_t> writeable() const noexcept
{
size_t writeable = pair_.capacity - pair_.writepos;
return std::pair{ pair_.data + pair_.writepos, writeable };
}

pointer revert(size_t n) noexcept
{
assert(pair_.writepos >= (pair_.readpos+n));
Expand Down Expand Up @@ -509,25 +480,22 @@ namespace moon
{
return pair_.capacity;
}

size_t reserved() const noexcept
{
return pair_.headreserved;
}
private:
compressed_pair pair_;
};
};

#ifdef MOON_ENABLE_MIMALLOC
#include "mimalloc.h"
#endif

namespace moon
{
#ifdef MOON_ENABLE_MIMALLOC
using buffer = base_buffer<mi_stl_allocator<char>>;
}
#else
namespace moon
{
using buffer = base_buffer<std::allocator<char>>;
}
#endif
constexpr size_t BUFFER_OPTION_CHEAP_PREPEND = 16;
}

94 changes: 94 additions & 0 deletions example/example_pgsql.lua
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,98 @@ $$ LANGUAGE plpgsql;
-- select * from public.userdata where key='level' AND (value)::int = 100;
end

local function test_sql_driver()
local sqldriver = require("service.sqldriver")
local db = moon.new_service {
unique = true,
name = "db_game",
file = "../service/sqldriver.lua",
provider = "moon.db.pg",
threadid = 2,
poolsize = 5,
opts = db_config
}

assert(db>0)

local fn_sql = [[
CREATE OR REPLACE FUNCTION update_userdata(pk integer, VARIADIC key_values text[]) RETURNS void AS $$
DECLARE
i integer;
BEGIN
FOR i IN 1..array_length(key_values, 1) BY 2 LOOP
INSERT INTO userdata(uid, key, value) VALUES (pk, key_values[i], key_values[i+1]::json) ON CONFLICT (uid, key) DO UPDATE SET value = excluded.value::json;
END LOOP;
END;
$$ LANGUAGE plpgsql;
]]

local sql = string.format([[
--create userdata table
drop table if exists userdata;
create table userdata (
uid bigint,
key text,
value jsonb,
CONSTRAINT pk_userdata PRIMARY KEY (uid, key)
);
]])

print(1)
sqldriver.query(db, sql)
print(2)

local res = sqldriver.query(db, fn_sql)
assert(not res.code, res.message)

---@class User
---@field name string
---@field age number
---@field level number
---@field exp number
local user = {
name = "hello",
age = 10,
level = 99,
exp = 11,
info1 = simple_json_field,
info2 = simple_json_field,
}

local key_value_model = require "key_value_model"

---@type User Description
local user = key_value_model.new("userdata", 233, user)

user.age = 101
user.level = 100
user.info1.cc = 200
user.info2.cc = 300
user.name = "hello world"

local sql = key_value_model.modifyed(user)

-- print(sql)
local bt = moon.clock()

local res
for m = 1, 100 do
res = sqldriver.query(db, sql)
assert(not res.code, res.message)
end

print("test_key_value_table_function insert cost", (moon.clock() - bt)/100)

sql = string.format([[
--select userdata
select key, value from userdata where uid = %d;
]], 233)
local res = sqldriver.query(db, sql)
local data = res.data

moon.send("lua", db, "save_then_quit")
end

moon.async(function()
test_json_query()
test_big_json_value()
Expand All @@ -352,6 +444,8 @@ moon.async(function()

test_key_value_table_function()

test_sql_driver()


moon.exit(-1)
end)
4 changes: 2 additions & 2 deletions example/example_tobeclosed.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ moon.async(function()
end)
moon.async(function()
local a<close> = new_test("session_id_coroutine_call")
moon.call("lua", moon.id)
local _ = moon.call("lua", moon.id)
end)
moon.async(function()
moon.call("lua", moon.id)
local _ = moon.call("lua", moon.id)
end)
moon.sleep(100)
moon.async(function()
Expand Down
26 changes: 4 additions & 22 deletions lualib-src/lua_buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,23 +241,6 @@ static int prepare(lua_State* L)
return 0;
}

static int has_flag(lua_State* L)
{
auto buf = get_pointer(L, 1);
auto flag = static_cast<int>(luaL_checkinteger(L, 2));
bool res = buf->has_flag(flag);
lua_pushboolean(L, res ? 1 : 0);
return 1;
}

static int set_flag(lua_State* L)
{
auto buf = get_pointer(L, 1);
auto flag = static_cast<int>(luaL_checkinteger(L, 2));
buf->set_flag(flag);
return 0;
}

static int unsafe_delete(lua_State* L)
{
auto buf = get_pointer(L, 1);
Expand All @@ -267,9 +250,8 @@ static int unsafe_delete(lua_State* L)

static int unsafe_new(lua_State* L)
{
size_t capacity = static_cast<size_t>(luaL_optinteger(L, 1, buffer::DEFAULT_RESERVE));
uint16_t headreserved = static_cast<uint16_t>(luaL_optinteger(L, 2, buffer::DEFAULT_HEAD_RESERVE));
buffer* buf = new buffer(capacity, headreserved);
size_t capacity = static_cast<size_t>(luaL_optinteger(L, 1, buffer::DEFAULT_CAPACITY));
buffer* buf = new buffer{capacity};
lua_pushlightuserdata(L, buf);
return 1;
}
Expand All @@ -282,11 +264,13 @@ static int concat(lua_State* L)
return 0;
}
auto buf = new buffer{};
buf->commit(BUFFER_OPTION_CHEAP_PREPEND);
try
{
for (int i = 1; i <= n; i++) {
concat_one(L, buf, i, 0);
}
buf->seek(BUFFER_OPTION_CHEAP_PREPEND);
lua_pushlightuserdata(L, buf);
return 1;
}
Expand Down Expand Up @@ -335,8 +319,6 @@ extern "C" {
, {"seek", seek}
, {"commit", commit}
, {"prepare", prepare}
, {"has_flag", has_flag}
, {"set_flag", set_flag}
, {"concat",concat }
, {"concat_string",concat_string }
, {NULL, NULL}
Expand Down
Loading

0 comments on commit 4319bdf

Please sign in to comment.