Skip to content

Commit 7356d05

Browse files
committed
Optimize memory management.
Don't keep track of finalized statements.
1 parent 999fd41 commit 7356d05

File tree

4 files changed

+2676
-2672
lines changed

4 files changed

+2676
-2672
lines changed

coffee/api.coffee

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ class Statement
227227
'free': ->
228228
@freemem()
229229
res = sqlite3_finalize(@stmt) is SQLite.OK
230+
delete @db.statements[@stmt]
230231
@stmt = NULL
231232
return res
232233

@@ -240,7 +241,7 @@ class Database
240241
if data? then FS.createDataFile '/', @filename, data, true, true
241242
@handleError sqlite3_open @filename, apiTemp
242243
@db = getValue(apiTemp, 'i32')
243-
@statements = [] # A list of all prepared statements of the database
244+
@statements = {} # A list of all prepared statements of the database
244245

245246
### Execute an SQL query, ignoring the rows it returns.
246247
@@ -351,6 +352,7 @@ class Database
351352
stmt = @['prepare'] sql, params
352353
while stmt['step']()
353354
callback stmt['getAsObject']()
355+
stmt['free']()
354356
if typeof done is 'function' then done()
355357

356358
### Prepare an SQL statement
@@ -366,7 +368,7 @@ class Database
366368
if pStmt is NULL then throw 'Nothing to prepare'
367369
stmt = new Statement pStmt, this
368370
if params? then stmt.bind params
369-
@statements.push stmt
371+
@statements[pStmt] = stmt
370372
return stmt
371373

372374
### Exports the contents of the database to a binary array
@@ -386,7 +388,7 @@ class Database
386388
memory consumption will grow forever
387389
###
388390
'close': ->
389-
stmt['free']() for stmt in @statements
391+
stmt['free']() for _,stmt of @statements
390392
@handleError sqlite3_close_v2 @db
391393
FS.unlink '/' + @filename
392394
@db = null

0 commit comments

Comments
 (0)