From 5bf695823a1720cfb7e9dfbecc08a5a6bfb40bee Mon Sep 17 00:00:00 2001 From: dosworld Date: Thu, 29 Sep 2022 22:52:00 +0300 Subject: [PATCH] Speed optimization --- DBB.PAS | 52 ++++++-------------------- DBM.PAS | 104 ++++++++++++++++++---------------------------------- EXCACHE.PAS | 6 +-- RC4.PAS | 6 ++- 4 files changed, 54 insertions(+), 114 deletions(-) diff --git a/DBB.PAS b/DBB.PAS index e170a64..452b477 100644 --- a/DBB.PAS +++ b/DBB.PAS @@ -42,9 +42,6 @@ END; DBBFile = RECORD f : BFILE; header : DBB_HDR; - crecno : LONGINT; - needupdate : BOOLEAN; - data : PCHAR; END; PROCEDURE dbb_reset(VAR f : DBBFile; fname : STRING; blk_size : WORD); @@ -72,23 +69,12 @@ CONST DBB_SIGN = $4244; DBB_HDR_SIZE = SizeOf(DBB_HDR); -PROCEDURE dbb_update(VAR f : DBBFile); -BEGIN - IF (f.needupdate) AND (f.crecno <> 0) THEN BEGIN - Seek(f.f, DBB_HDR_SIZE + (f.crecno - 1) * f.header.size); - BlockWrite(f.f, f.data^, f.header.size); - END; - f.needupdate := FALSE; -END; - -PROCEDURE dbb_go(VAR f : DBBFile; recno : LONGINT); +FUNCTION dbb_go(VAR f : DBBFile; recno : LONGINT) : LONGINT; +VAR r : LONGINT; BEGIN - IF recno = f.crecno THEN EXIT; - dbb_update(f); - { FillChar(f.data^, f.header.size, #0); } - f.crecno := recno; + r := DBB_HDR_SIZE + (recno - 1) * f.header.size; Seek(f.f, DBB_HDR_SIZE + (recno - 1) * f.header.size); - BlockRead(f.f, f.data^, f.header.size); + dbb_go := r; END; PROCEDURE dbb_reset(VAR f : DBBFile; fname : STRING; blk_size : WORD); @@ -105,10 +91,7 @@ BEGIN dbb_rewrite(f, fname, blk_size); EXIT; END; - IF (f.header.sign = DBB_SIGN) AND (f.header.size = blk_size) THEN BEGIN - GetMem(f.data, f.header.size); - FillChar(f.data^, f.header.size, #0); - END ELSE BEGIN + IF (f.header.sign <> DBB_SIGN) OR (f.header.size <> blk_size) THEN BEGIN Close(f.f); dbb_rewrite(f, fname, blk_size); END; @@ -122,10 +105,6 @@ BEGIN f.header.records := 0; f.header.root := 0; f.header.size := blk_size; - f.crecno := 0; - f.needupdate := FALSE; - GetMem(f.data, f.header.size); - FillChar(f.data^, f.header.size, #0); Seek(f.f, 0); BlockWrite(f.f, f.header, DBB_HDR_SIZE); END; @@ -154,22 +133,17 @@ END; PROCEDURE dbb_close(VAR f : DBBFile); BEGIN IF NOT IsOpen(f.f) THEN EXIT; - dbb_update(f); Seek(f.f, 0); BlockWrite(f.f, f.header, DBB_HDR_SIZE); Close(f.f); - IF f.data <> NIL THEN FreeMem(f.data, f.header.size); FillChar(f, SizeOf(DBBFile), #0); END; PROCEDURE dbb_free(VAR f : DBBFile; rec : LONGINT); BEGIN IF (NOT IsOpen(f.f)) OR (rec = 0) THEN EXIT; - dbb_update(f); - f.crecno := rec; - FillChar(f.data^, f.header.size, #0); - PLONGINT(f.data)^ := f.header.next; - f.needupdate := TRUE; + dbb_go(f, rec); + WriteDWord(f.f, f.header.next); f.header.next := rec; END; @@ -179,12 +153,10 @@ BEGIN IF f.header.next <> 0 THEN BEGIN r := f.header.next; dbb_go(f, r); - f.header.next := PLONGINT(f.data)^; + f.header.next := ReadDWord(f.f); END ELSE BEGIN Inc(f.header.records); r := f.header.records; - dbb_go(f, r); - f.needupdate := TRUE; END; dbb_alloc := r; END; @@ -193,7 +165,7 @@ PROCEDURE dbb_get(VAR f : DBBFile; rec : LONGINT; VAR b); BEGIN IF (NOT IsOpen(f.f)) OR (rec = 0) THEN EXIT; dbb_go(f, rec); - Move(f.data^, b, f.header.size); + BlockRead(f.f, b, f.header.size); END; FUNCTION dbb_add(VAR f : DBBFile; VAR b) : LONGINT; @@ -203,8 +175,7 @@ BEGIN IF IsOpen(f.f) THEN BEGIN res := dbb_alloc(f); dbb_go(f, res); - Move(b, f.data^, f.header.size); - f.needupdate := TRUE; + BlockWrite(f.f, b, f.header.size); END; dbb_add := res; END; @@ -213,8 +184,7 @@ PROCEDURE dbb_put(VAR f : DBBFile; rec : LONGINT; VAR b); BEGIN IF (NOT IsOpen(f.f)) OR (rec = 0) THEN EXIT; dbb_go(f, rec); - Move(b, f.data^, f.header.size); - f.needupdate := TRUE; + BlockWrite(f.f, b, f.header.size); END; FUNCTION dbb_getroot(VAR f : DBBFile):LONGINT; diff --git a/DBM.PAS b/DBM.PAS index 32985e0..1dea13d 100644 --- a/DBM.PAS +++ b/DBM.PAS @@ -38,19 +38,11 @@ DBM_HDR = RECORD root : LONGINT; size : WORD; END; -DBM_REC = RECORD - next : LONGINT; - size : WORD; -END; DBMFile = RECORD f : BFILE; header : DBM_HDR; - crec : DBM_REC; - crecno : LONGINT; - needupdate : BOOLEAN; rblk_size : WORD; - data : PCHAR; END; PROCEDURE dbm_reset(VAR f : DBMFile; fname : STRING; blk_size : WORD); @@ -75,36 +67,22 @@ IMPLEMENTATION CONST DBM_SIGN = $4D44; +ADDED_DATA = SizeOf(LONGINT) + SizeOf(WORD); FUNCTION norm_blk_size(blk_size : WORD) : WORD; BEGIN - blk_size := blk_size + SizeOf(DBM_REC); + blk_size := blk_size + ADDED_DATA; IF blk_size < SizeOf(DBM_HDR) THEN blk_size := SizeOf(DBM_HDR); IF (blk_size AND 3) <> 0 THEN blk_size := ((blk_size SHR 2) + 1) SHL 2; norm_blk_size := blk_size; END; -PROCEDURE dbm_update(VAR f : DBMFile); -BEGIN - IF (f.needupdate) AND (f.crecno <> 0) THEN BEGIN - Seek(f.f, f.crecno * f.header.size); - BlockWrite(f.f, f.crec, SizeOf(DBM_REC)); - BlockWrite(f.f, f.data^, f.rblk_size); - END; - f.needupdate := FALSE; -END; - -PROCEDURE dbm_go(VAR f : DBMFile; recno : LONGINT); +FUNCTION dbm_go(VAR f : DBMFile; recno : LONGINT) : LONGINT; +VAR r : LONGINT; BEGIN - IF recno = f.crecno THEN EXIT; - dbm_update(f); - { FillChar(f.data^, f.rblk_size, #0); } - f.crec.next := 0; - f.crec.size := 0; - f.crecno := recno; - Seek(f.f, recno * f.header.size); - BlockRead(f.f, f.crec, SizeOf(DBM_REC)); - BlockRead(f.f, f.data^, f.rblk_size); + r := recno * f.header.size; + Seek(f.f, r); + dbm_go := r; END; PROCEDURE dbm_reset(VAR f : DBMFile; fname : STRING; blk_size : WORD); @@ -119,9 +97,7 @@ BEGIN Close(f.f); dbm_rewrite(f, fname, blk_size); END ELSE IF (f.header.sign = DBM_SIGN) AND (f.header.size = rblk_size) THEN BEGIN - f.rblk_size := f.header.size - SizeOf(DBM_REC); - GetMem(f.data, f.rblk_size); - FillChar(f.data^, f.rblk_size, #0); + f.rblk_size := f.header.size - ADDED_DATA; END ELSE BEGIN Close(f.f); dbm_rewrite(f, fname, blk_size); @@ -129,20 +105,16 @@ BEGIN END; PROCEDURE _dbm_rewrite(VAR f : DBMFile); +VAR i : INTEGER; BEGIN IF IsOpen(f.f) THEN BEGIN f.header.sign := DBM_SIGN; f.header.next := 0; f.header.records := 0; f.header.root := 0; - f.crecno := 0; - f.needupdate := FALSE; - FillChar(f.crec, SizeOf(DBM_REC), #0); - GetMem(f.data, f.rblk_size); - FillChar(f.data^, f.rblk_size, #0); Seek(f.f, 0); BlockWrite(f.f, f.header, SizeOf(DBM_HDR)); - BlockWrite(f.f, f.data^, f.header.size - SizeOf(DBM_HDR)); + FOR i := 1 TO f.header.size - SizeOf(DBM_HDR) DO WriteByte(f.f, 0); END; END; @@ -152,7 +124,7 @@ BEGIN Assign(f.f, fname); ReWrite(f.f); f.header.size := norm_blk_size(blk_size); - f.rblk_size := f.header.size - SizeOf(DBM_REC); + f.rblk_size := f.header.size - ADDED_DATA; _dbm_rewrite(f); END; @@ -161,7 +133,7 @@ BEGIN FillChar(f, SizeOf(DBMFile), #0); ReWriteTemp(f.f); f.header.size := norm_blk_size(blk_size); - f.rblk_size := f.header.size - SizeOf(DBM_REC); + f.rblk_size := f.header.size - ADDED_DATA; _dbm_rewrite(f); END; @@ -173,11 +145,9 @@ END; PROCEDURE dbm_close(VAR f : DBMFile); BEGIN IF NOT IsOpen(f.f) THEN EXIT; - dbm_update(f); Seek(f.f, 0); BlockWrite(f.f, f.header, SizeOf(DBM_HDR)); Close(f.f); - IF f.data <> NIL THEN FreeMem(f.data, f.rblk_size); FillChar(f, SizeOf(DBMFile), #0); END; @@ -187,12 +157,11 @@ BEGIN IF NOT IsOpen(f.f) THEN EXIT; WHILE rec <> 0 DO BEGIN dbm_go(f, rec); - n := f.crec.next; - f.crec.next := f.header.next; - f.crec.size := 0; - f.needupdate := TRUE; + n := ReadDWord(f.f); + dbm_go(f, rec); + WriteDWord(f.f, f.header.next); + WriteWord(f.f, 0); f.header.next := rec; - FillChar(f.data^, f.rblk_size, #0); rec := n; END; END; @@ -203,15 +172,12 @@ BEGIN IF f.header.next <> 0 THEN BEGIN r := f.header.next; dbm_go(f, r); - f.header.next := f.crec.next; + f.header.next := ReadDWord(f.f); END ELSE BEGIN Inc(f.header.records); r := f.header.records; END; dbm_go(f, r); - f.crec.next := 0; - f.crec.size := 0; - f.needupdate := TRUE; dbm_alloc := r; END; @@ -222,8 +188,8 @@ BEGIN IF IsOpen(f.f) THEN BEGIN WHILE rec <> 0 DO BEGIN dbm_go(f, rec); - Inc(r, f.crec.size); - rec := f.crec.next; + rec := ReadDWord(f.f); + Inc(r, ReadWord(f.f)); END; END; dbm_size := r; @@ -237,12 +203,12 @@ BEGIN p := @b; WHILE (size <> 0) AND (rec <> 0) DO BEGIN dbm_go(f, rec); - g := f.crec.size; + rec := ReadDWord(f.f); + g := ReadWord(f.f); IF g > size THEN g := size; - Move(f.data^, p^, g); + BlockRead(f.f, p^, g); Dec(size, g); Inc(p, g); - rec := f.crec.next; END; END; @@ -250,6 +216,7 @@ FUNCTION dbm_add(VAR f : DBMFile; VAR b; size : WORD) : LONGINT; VAR root, prec, crec : LONGINT; p : PCHAR; csize : WORD; + i : INTEGER; BEGIN root := 0; IF IsOpen(f.f) THEN BEGIN @@ -260,18 +227,20 @@ BEGIN IF root = 0 THEN root := crec; IF prec <> 0 THEN BEGIN dbm_go(f, prec); - f.crec.next := crec; - f.needupdate := TRUE; + WriteDWord(f.f, crec); END; - prec := crec; - dbm_go(f, crec); csize := f.rblk_size; IF csize > size THEN csize := size; - Move(p^, f.data^, csize); - f.crec.size := csize; - f.needupdate := TRUE; + dbm_go(f, crec); + WriteDWord(f.f, 0); + WriteWord(f.f, csize); + BlockWrite(f.f, p^, csize); + IF f.rblk_size > csize THEN BEGIN + FOR i := 1 TO f.rblk_size - csize DO WriteByte(f.f, 0); + END; Dec(size, csize); Inc(p, csize); + prec := crec; END; END; dbm_add := root; @@ -282,12 +251,11 @@ VAR crec : LONGINT; BEGIN IF NOT IsOpen(f.f) THEN EXIT; dbm_go(f, rec); - crec := f.crec.next; - f.crec.next := 0; - f.crec.size := 0; - f.needupdate := TRUE; - dbm_free(f, crec); + crec := ReadDWord(f.f); dbm_go(f, rec); + WriteDWord(f.f, 0); + WriteWord(f.f, 0); + dbm_free(f, crec); dbm_free(f, rec); dbm_add(f, b, size); END; diff --git a/EXCACHE.PAS b/EXCACHE.PAS index 0ca3732..218c2f6 100644 --- a/EXCACHE.PAS +++ b/EXCACHE.PAS @@ -120,7 +120,7 @@ BEGIN lookup_recno := r; END; -FUNCTION lookup_free(c : PEXCACHE) : INTEGER; +PROCEDURE lookup_free(c : PEXCACHE); VAR i, r : INTEGER; BEGIN r := c^.size; @@ -132,7 +132,7 @@ BEGIN END; Inc(i); END; - lookup_free := r; + make_top(c, r); END; FUNCTION excache_get(c : PEXCACHE; recno : LONGINT; VAR b; VAR count : WORD) : BOOLEAN; @@ -155,7 +155,7 @@ BEGIN IF c = NIL THEN excache_put := FALSE ELSE IF c^.h = 0 THEN excache_put := FALSE ELSE BEGIN - IF NOT lookup_recno(c, recno) THEN make_top(c, lookup_free(c)); + IF NOT lookup_recno(c, recno) THEN lookup_free(c); c^.items[1].recno := recno; c^.items[1].count := count; c^.items[1].is_busy := TRUE; diff --git a/RC4.PAS b/RC4.PAS index eb96ccf..66d5460 100644 --- a/RC4.PAS +++ b/RC4.PAS @@ -29,7 +29,7 @@ TYPE RC4_SEED = ARRAY[0..255] OF BYTE; PROCEDURE rc4_init(VAR seed : RC4_SEED; password : STRING); -PROCEDURE rc4_crypt(VAR seed : RC4_SEED; src : PCHAR; size : WORD); +PROCEDURE rc4_crypt(VAR seed : RC4_SEED; VAR buf; size : WORD); IMPLEMENTATION @@ -49,12 +49,14 @@ BEGIN END; END; -PROCEDURE rc4_crypt(VAR seed : RC4_SEED; src : PCHAR; size : WORD); +PROCEDURE rc4_crypt(VAR seed : RC4_SEED; VAR buf; size : WORD); VAR i, j : INTEGER; x : BYTE; + src : PCHAR; BEGIN i := 0; j := 0; + src := @buf; WHILE size <> 0 DO BEGIN Inc(i); i := i AND $FF;