Compare commits

...

4 Commits

Author SHA1 Message Date
129b842487 optimize rendering 2026-02-09 09:10:51 +09:00
b41cf78862 add bench 2026-02-09 09:10:51 +09:00
2cfee1d36c fix kouho selection. 2026-02-09 09:10:51 +09:00
b35b81ad3d vietnamese telex input 2026-02-09 09:10:44 +09:00
16 changed files with 430 additions and 2550 deletions

View File

@ -1,12 +1,12 @@
CC = 9c
LD = 9l
CFLAGS = -Wall -Wextra -O2
CFLAGS = -Wall -Wextra -O2 -g
PROG = strans
SRCS = $(wildcard *.c)
OBJS = $(SRCS:.c=.o)
all: $(PROG)
all: $(PROG) xim bench
$(PROG): $(OBJS)
$(LD) -o $@ $(OBJS) -lthread -lString -lbio -lxcb -lm
@ -15,5 +15,13 @@ $(OBJS): dat.h fn.h ipc.h
clean:
rm -f $(OBJS) $(PROG)
make -C xim/ clean
make -C bench/ clean
.PHONY: all clean
xim:
make -C xim/
bench:
make -C bench/
.PHONY: all clean xim bench

23
bench.sh Executable file
View File

@ -0,0 +1,23 @@
#!/bin/sh
pkill strans
pkill strans-xim
sleep 1
./strans map font &
sleep 1
# warm up glyph cache
./bench/bench bench/bench.keys 100
echo "cache warmed up"
STRANS_PID=$(pgrep -x strans)
perf record -g -o bench/perf.data -p "$STRANS_PID" &
PERF_PID=$!
sleep 1
./bench/bench bench/bench.keys 100
kill -INT $PERF_PID
wait $PERF_PID 2>/dev/null
pkill strans

8
bench/Makefile Normal file
View File

@ -0,0 +1,8 @@
CC = cc
CFLAGS = -Wall -O2
bench: main.c
$(CC) $(CFLAGS) -o $@ main.c
clean:
rm -f bench

BIN
bench/bench Executable file

Binary file not shown.

30
bench/bench.keys Normal file
View File

@ -0,0 +1,30 @@
^ntoukyounotenki^Ryoidesu^R
konpyuutanopuroguramu^B^B^Bgramu^R
nihongowobenkyoushiteimasu^R
sakuranoshanohana^B^B^B^Bhanagasaiteiru^R
kaigi^Ekyounokaigi^R
ashitahaiitenkininarudeshou^R
^sdkssudgktpdy^Rgksrmfekfqnxm^R
vmfhrrh^Btprtmxm^Rdufrlavldml^R
dmlrlwkekfwprtm^R
gksrnl^B^B^Bdlfp^R
answkd^Eanswkd^R
qkrtlwkf^B^B^Bwkfgkf^R
^thello world^R
the quick brown fox jumps over the lazy dog^R
programming is fun^R
^vVietNam^Rxinchaobancokhoekhong^R
toidilambaitap^B^B^Bbaitap^R
hoctiengviet^R
^nkaishanitsuutomeshiteimasu^R
tanaboraguukoudesu^B^B^B^B^Bkoudesu^R
^srhksrnrdlTkfgkwl^R
answkddkssud^B^Bdkssud^R
wnsdydgktpdy^R
^vtoidenlopcuahang^R
motngaymoiconduong^R
^thigh performance input method^R
^nnyuuryokuhouhou^R
tesutonosuuretsu^R
^sxkfldzmsdml^R
qhfmfjxm^R

168
bench/main.c Normal file
View File

@ -0,0 +1,168 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <sys/socket.h>
#include <sys/un.h>
enum {
Kback = 0xf008,
Kret = 0xf00d,
Kesc = 0xf01b,
Mctrl = 1<<2,
};
typedef struct Key Key;
struct Key {
int k;
int mod;
};
static Key *keys;
static int nkeys;
static int fd;
static void
die(char *msg)
{
perror(msg);
exit(1);
}
static void
addkey(int k, int mod)
{
static int cap;
if(nkeys >= cap){
cap = cap ? cap * 2 : 256;
keys = realloc(keys, cap * sizeof(Key));
if(!keys)
die("realloc");
}
keys[nkeys].k = k;
keys[nkeys].mod = mod;
nkeys++;
}
static void
loadkeys(char *file)
{
FILE *f;
int c;
f = fopen(file, "r");
if(!f)
die(file);
while((c = fgetc(f)) != EOF){
if(c == '\n' || c == '\r')
continue;
if(c != '^'){
addkey(c, 0);
continue;
}
c = fgetc(f);
if(c == EOF)
break;
switch(c){
case 'B':
addkey(Kback, 0);
break;
case 'R':
addkey(Kret, 0);
break;
case 'E':
addkey(Kesc, 0);
break;
default:
addkey(c, Mctrl);
break;
}
}
fclose(f);
}
static void
dial(void)
{
struct sockaddr_un addr;
fd = socket(AF_UNIX, SOCK_STREAM, 0);
if(fd < 0)
die("socket");
memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_UNIX;
snprintf(addr.sun_path, sizeof(addr.sun_path),
"/tmp/strans.%d", getuid());
if(connect(fd, (struct sockaddr*)&addr, sizeof(addr)) < 0)
die("connect");
}
static void
sendkey(int key, int mod)
{
unsigned char req[4];
req[0] = 0;
req[1] = mod;
req[2] = key & 0xff;
req[3] = (key >> 8) & 0xff;
if(write(fd, req, 4) != 4)
die("write");
}
static int
readresp(void)
{
unsigned char buf[256];
int n;
if(read(fd, buf, 2) != 2)
return -1;
n = buf[1];
if(n > 0 && read(fd, buf + 2, n) != n)
return -1;
return 0;
}
static double
now(void)
{
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return ts.tv_sec + ts.tv_nsec * 1e-9;
}
int
main(int argc, char **argv)
{
int i, j, niter;
double t0, t1, dt;
if(argc < 2){
fprintf(stderr, "usage: bench file [niter]\n");
exit(1);
}
loadkeys(argv[1]);
niter = argc > 2 ? atoi(argv[2]) : 1000;
dial();
t0 = now();
for(i = 0; i < niter; i++)
for(j = 0; j < nkeys; j++){
sendkey(keys[j].k, keys[j].mod);
if(readresp() < 0){
fprintf(stderr, "failed at iter %d key %d\n", i, j);
exit(1);
}
}
t1 = now();
dt = t1 - t0;
printf("%d iters x %d keys = %d keys\n", niter, nkeys, niter * nkeys);
printf("%.3f ms total, %.3f us/key, %.3f us/iter\n",
dt * 1000, dt * 1e6 / (niter * nkeys), dt * 1e6 / niter);
close(fd);
return 0;
}

BIN
bench/perf.data Normal file

Binary file not shown.

BIN
bench/perf.data.old Normal file

Binary file not shown.

2
dat.h
View File

@ -117,7 +117,7 @@ typedef struct Drawcmd Drawcmd;
struct Drawcmd
{
Str pre;
Str kouho[Maxkouho];
Str kouho[Maxdisp];
int nkouho;
int sel;
};

1
fn.h
View File

@ -28,6 +28,7 @@ void drawthread(void*);
void imthread(void*);
Emit transmap(Im*, Rune);
Emit transko(Im*, Rune);
Emit transvi(Im*, Rune);
void backko(Im*);
void dictsend(Im*, Str*);

22
font.c
View File

@ -99,7 +99,8 @@ void
putfont(u32int *buf, int w, int h, int px, int py, Rune r)
{
Glyph *g;
int i, j, x, y, a, sel, f;
int i, j, a, sel, f;
int y0, j0, j1, x0, i0, i1;
u32int *p;
if(r >= Nglyphs)
@ -116,17 +117,18 @@ putfont(u32int *buf, int w, int h, int px, int py, Rune r)
if(g->bmp == nil)
return;
}
for(j = 0; j < g->h; j++){
y = py + j + g->oy + Fontsz - Fontbase;
if(y < 0 || y >= h)
continue;
for(i = 0; i < g->w; i++){
x = px + i + g->ox;
if(x < 0 || x >= w)
continue;
y0 = py + g->oy + Fontsz - Fontbase;
j0 = y0 < 0 ? -y0 : 0;
j1 = y0 + g->h > h ? h - y0 : g->h;
x0 = px + g->ox;
i0 = x0 < 0 ? -x0 : 0;
i1 = x0 + g->w > w ? w - x0 : g->w;
for(j = j0; j < j1; j++){
for(i = i0; i < i1; i++){
a = g->bmp[j * g->w + i];
if(a > 0){
p = &buf[y * w + x];
p = &buf[(y0 + j) * w + x0 + i];
sel = (*p == Colsel) ? 1 : 0;
*p = blendtab[sel][a];
}

2
main.c
View File

@ -61,7 +61,7 @@ threadmain(int argc, char **argv)
usage();
fontdir = argv[2];
drawc = chancreate(sizeof(Drawcmd), 0);
drawc = chancreate(sizeof(Drawcmd), 4);
keyc = chancreate(sizeof(Keyreq), 0);
dictreqc = chancreate(sizeof(Dictreq), 4);
dictresc = chancreate(sizeof(Dictres), 0);

File diff suppressed because it is too large Load Diff

View File

@ -16,7 +16,7 @@ Lang langs[] = {
{LangJPK, "kata", "kanji", transmap, backmap, dictqmap, nil, nil},
{LangKO, "hangul", nil, transko, backko, dictqmap, nil, nil},
{LangEMOJI, "emoji", "emoji", transmap, backmap, dictqmap, nil, nil},
{LangVI, "telex", nil, transmap, backmap, dictqmap, nil, nil},
{LangVI, "telex", nil, transvi, backmap, dictqmap, nil, nil},
};
int nlang = nelem(langs);
@ -24,7 +24,7 @@ static void
clearkouho(void)
{
im.nkouho = 0;
im.sel = 0;
im.sel = -1;
}
static void
@ -43,15 +43,18 @@ static void
show(void)
{
Drawcmd dc;
int i;
int i, first, n;
sclear(&dc.pre);
if(!mapget(im.l->map, &im.pre, &dc.pre))
dc.pre = im.pre;
dc.nkouho = im.nkouho;
dc.sel = im.sel;
for(i = 0; i < dc.nkouho; i++)
dc.kouho[i] = im.kouho[i];
first = im.sel >= Maxdisp ? im.sel - Maxdisp + 1 : 0;
n = im.nkouho - first;
if(n > Maxdisp) n = Maxdisp;
dc.nkouho = n;
dc.sel = im.sel >= 0 ? im.sel - first : -1;
for(i = 0; i < n; i++)
dc.kouho[i] = im.kouho[first + i];
chansend(drawc, &dc);
}
@ -200,7 +203,7 @@ keystroke(u32int ks, u32int mod, Str *com)
return 0;
if(ks == Kdown && im.sel < im.nkouho - 1)
im.sel++;
if(ks == Kup && im.sel > 0)
if(ks == Kup && im.sel >= 0)
im.sel--;
show();
return 1;
@ -257,6 +260,11 @@ keystroke(u32int ks, u32int mod, Str *com)
return 1;
return 0;
}
if(ks == '0' && im.nkouho > 0){
commit(com);
reset();
return 1;
}
if(ks > 0x7f || ks == ' '){
commit(com);
sputr(com, ks);

148
vi.c Normal file
View File

@ -0,0 +1,148 @@
#include "dat.h"
#include "fn.h"
static struct {
Rune base;
Rune tone[5]; /* s f r x j */
} vitone[] = {
{L'a', {L'á', L'à', L'', L'ã', L''}},
{L'â', {L'', L'', L'', L'', L''}},
{L'ă', {L'', L'', L'', L'', L''}},
{L'e', {L'é', L'è', L'', L'', L''}},
{L'ê', {L'ế', L'', L'', L'', L''}},
{L'i', {L'í', L'ì', L'', L'ĩ', L''}},
{L'o', {L'ó', L'ò', L'', L'õ', L''}},
{L'ô', {L'', L'', L'', L'', L''}},
{L'ơ', {L'', L'', L'', L'', L''}},
{L'u', {L'ú', L'ù', L'', L'ũ', L''}},
{L'ư', {L'', L'', L'', L'', L''}},
{L'y', {L'ý', L'', L'', L'', L''}},
{L'A', {L'Á', L'À', L'', L'Ã', L''}},
{L'Â', {L'', L'', L'', L'', L''}},
{L'Ă', {L'', L'', L'', L'', L''}},
{L'E', {L'É', L'È', L'', L'', L''}},
{L'Ê', {L'', L'', L'', L'', L''}},
{L'I', {L'Í', L'Ì', L'', L'Ĩ', L''}},
{L'O', {L'Ó', L'Ò', L'', L'Õ', L''}},
{L'Ô', {L'', L'', L'', L'', L''}},
{L'Ơ', {L'', L'', L'', L'', L''}},
{L'U', {L'Ú', L'Ù', L'', L'Ũ', L''}},
{L'Ư', {L'', L'', L'', L'', L''}},
{L'Y', {L'Ý', L'', L'', L'', L''}},
};
static int tonetab[128] = {
['s'] = 1, ['f'] = 2, ['r'] = 3, ['x'] = 4, ['j'] = 5,
};
#define Istone(c) ((c) < 128 && tonetab[(c)] > 0)
#define Toneidx(c) (tonetab[(c)] - 1)
static int
isvowel(Rune c)
{
int i;
for(i = 0; i < nelem(vitone); i++)
if(vitone[i].base == c)
return 1;
return 0;
}
static Rune
removetone(Rune c)
{
int i, j;
for(i = 0; i < nelem(vitone); i++){
if(vitone[i].base == c)
return c;
for(j = 0; j < 5; j++)
if(vitone[i].tone[j] == c)
return vitone[i].base;
}
return c;
}
static Rune
applytone(Rune c, int tidx)
{
int i;
Rune base;
base = removetone(c);
for(i = 0; i < nelem(vitone); i++)
if(vitone[i].base == base)
return vitone[i].tone[tidx];
return c;
}
Emit
transvi(Im *im, Rune c)
{
Emit e;
Str mapped, pre;
int i, tidx, vi, last, penult;
Rune v, b1, b2;
if(!Istone(c) && c != 'z')
return transmap(im, c);
memset(&e, 0, sizeof e);
if(im->pre.n == 0){
sputr(&e.s, c);
return e;
}
if(im->pre.r[im->pre.n - 1] == '\\'){
pre = im->pre;
pre.n--;
if(!mapget(im->l->map, &pre, &mapped))
mapped = pre;
sputr(&mapped, c);
e.eat = 1;
e.s = mapped;
return e;
}
if(!mapget(im->l->map, &im->pre, &mapped))
mapped = im->pre;
last = -1;
penult = -1;
for(i = 0; i < mapped.n; i++){
v = removetone(mapped.r[i]);
if(isvowel(v)){
penult = last;
last = i;
}
}
vi = -1;
if(last >= 0){
if(last < mapped.n - 1 || penult < 0)
vi = last;
else{
b1 = removetone(mapped.r[penult]);
b2 = removetone(mapped.r[last]);
if((b1 == 'o' || b1 == 'O') && (b2 == 'a' || b2 == 'A' || b2 == 'e' || b2 == 'E'))
vi = last;
else if((b1 == 'u' || b1 == 'U') && (b2 == 'y' || b2 == 'Y'))
vi = last;
else
vi = penult;
}
}
if(vi < 0){
e.eat = 1;
e.s = mapped;
sputr(&e.s, c);
return e;
}
if(c == 'z')
mapped.r[vi] = removetone(mapped.r[vi]);
else{
tidx = Toneidx(c);
mapped.r[vi] = applytone(mapped.r[vi], tidx);
}
e.eat = 1;
e.next = mapped;
return e;
}

15
win.c
View File

@ -72,8 +72,10 @@ drawkouho(Drawcmd *dc, int first, int n, int w, int h)
memset(img, (uchar)Colbg, w * h * sizeof(u32int));
drawstr(img, 0, 0, dc->pre.r, dc->pre.n, w, h);
sely = Fontsz + (dc->sel - first) * Fontsz;
if(dc->sel >= 0){
sely = Fontsz + dc->sel * Fontsz;
memset(img + sely * w, (uchar)Colsel, Fontsz * w * sizeof(u32int));
}
for(i = 0, y = Fontsz; i < n; i++, y += Fontsz){
s = &dc->kouho[first+i];
putfont(img, w, h, 0, y, '1' + i + Asciitofull);
@ -99,17 +101,16 @@ winhide(void)
static void
winshow(Drawcmd *dc)
{
int px, py, w, h, i, n, first, maxw;
int px, py, w, h, i, n, maxw;
u32int vals[4];
xcb_query_pointer_reply_t *ptr;
xcb_query_pointer_cookie_t cookie;
cookie = xcb_query_pointer(conn, scr->root);
first = dc->sel >= Maxdisp ? dc->sel - Maxdisp + 1 : 0;
n = min(dc->nkouho - first, Maxdisp);
n = dc->nkouho;
maxw = dc->pre.n;
for(i = 0; i < n; i++)
maxw = max(maxw, dc->kouho[first+i].n);
maxw = max(maxw, dc->kouho[i].n);
ptr = xcb_query_pointer_reply(conn, cookie, nil);
if(ptr == nil)
die("xcb_query_pointer");
@ -125,7 +126,7 @@ winshow(Drawcmd *dc)
XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
vals);
xcb_map_window(conn, win);
drawkouho(dc, first, n, w, h);
drawkouho(dc, 0, n, w, h);
putimage(w, h);
}
@ -137,6 +138,8 @@ drawthread(void*)
threadsetname("draw");
wininit();
while(chanrecv(drawc, &dc) > 0){
while(channbrecv(drawc, &dc) > 0)
;
if(dc.nkouho == 0 && dc.pre.n == 0)
winhide();
else