Calcuate savefile paths only once; avoid excessive rehashing when reading track databases
parent
222df8116d
commit
88d6d9e971
|
@ -14,8 +14,11 @@
|
||||||
;; Note: do NOT change *server-port* and *server* at debug time
|
;; Note: do NOT change *server-port* and *server* at debug time
|
||||||
(defparameter *server-port* nil)
|
(defparameter *server-port* nil)
|
||||||
(defparameter *server* nil)
|
(defparameter *server* nil)
|
||||||
|
;; Note: please call (ywatds::update-savefile-paths) if you change the world path at runtime
|
||||||
|
(defparameter *ildb-path* nil)
|
||||||
|
(defparameter *nodedb-path* nil)
|
||||||
|
|
||||||
#+sbcl(declaim (sb-ext:always-bound *ildb* *trackdb*
|
#+sbcl(declaim (sb-ext:always-bound *ildb* *trackdb* *ildb-path* *nodedb-path*
|
||||||
*debugp* *gcp* *world-path* *server-port* *server*))
|
*debugp* *gcp* *world-path* *server-port* *server*))
|
||||||
|
|
||||||
(defmacro ensure-world-path (path)
|
(defmacro ensure-world-path (path)
|
||||||
|
@ -27,9 +30,13 @@
|
||||||
(concatenate 'string "advtrains_" name)
|
(concatenate 'string "advtrains_" name)
|
||||||
`(concatenate 'string "advtrains_" ,name))))
|
`(concatenate 'string "advtrains_" ,name))))
|
||||||
|
|
||||||
|
(defun update-savefile-paths ()
|
||||||
|
(psetf *ildb-path* (savefilepath "interlocking.ls")
|
||||||
|
*nodedb-path* (savefilepath "ndb4.ls")))
|
||||||
|
|
||||||
(defun load-data ()
|
(defun load-data ()
|
||||||
(let* ((ildb (atil:load-ildb (savefilepath "interlocking.ls")))
|
(let* ((ildb (atil:load-ildb *ildb-path*))
|
||||||
(tdb (tracks:load-trackdb (savefilepath "ndb4.ls"))))
|
(tdb (tracks:load-trackdb *nodedb-path*)))
|
||||||
(psetf *ildb* ildb *trackdb* tdb)
|
(psetf *ildb* ildb *trackdb* tdb)
|
||||||
(when *gcp*
|
(when *gcp*
|
||||||
#+sbcl(sb-ext:gc :full t))
|
#+sbcl(sb-ext:gc :full t))
|
||||||
|
@ -158,6 +165,7 @@
|
||||||
|
|
||||||
(defun start-server ()
|
(defun start-server ()
|
||||||
(tracks:init-tracks)
|
(tracks:init-tracks)
|
||||||
|
(update-savefile-paths)
|
||||||
(if *debugp* (break))
|
(if *debugp* (break))
|
||||||
(load-data)
|
(load-data)
|
||||||
(hunchentoot:start *server*)
|
(hunchentoot:start *server*)
|
||||||
|
|
|
@ -11,9 +11,6 @@
|
||||||
(connects (make-empty-connects) :type (vector list 16))
|
(connects (make-empty-connects) :type (vector list 16))
|
||||||
(special nil :type list))
|
(special nil :type list))
|
||||||
|
|
||||||
(defmacro make-track-database ()
|
|
||||||
`(make-hash-table :test #'equalp))
|
|
||||||
|
|
||||||
(defmacro get-track (trackdb pos)
|
(defmacro get-track (trackdb pos)
|
||||||
`(gethash ,pos ,trackdb))
|
`(gethash ,pos ,trackdb))
|
||||||
|
|
||||||
|
@ -70,8 +67,10 @@
|
||||||
;;; written based on https://gigamonkeys.com/book/practical-parsing-binary-files.html
|
;;; written based on https://gigamonkeys.com/book/practical-parsing-binary-files.html
|
||||||
;;; note that this implementation only reads nodes that are known to be tracks
|
;;; note that this implementation only reads nodes that are known to be tracks
|
||||||
(defun read-tracks-from-nodedb (fn)
|
(defun read-tracks-from-nodedb (fn)
|
||||||
(let ((tmpdb (make-hash-table :test #'equalp))
|
;; The 1000000 is chosen from a large dataset with ~800k tracks. The idea is to avoid
|
||||||
(nodes (make-hash-table :test #'eql)))
|
;; excessive rehashing for large databases.
|
||||||
|
(let ((tmpdb (make-hash-table :test #'equalp :size 1000000))
|
||||||
|
(nodes (make-hash-table :test #'eql :size (hash-table-count *track->conns*))))
|
||||||
(with-open-file (stream fn :element-type '(unsigned-byte 8))
|
(with-open-file (stream fn :element-type '(unsigned-byte 8))
|
||||||
(labels ((read-u8 () (read-byte stream nil nil))
|
(labels ((read-u8 () (read-byte stream nil nil))
|
||||||
(read-u16 () (let ((msb (read-u8)) (lsb (read-u8)))
|
(read-u16 () (let ((msb (read-u8)) (lsb (read-u8)))
|
||||||
|
@ -98,8 +97,8 @@
|
||||||
tmpdb))))
|
tmpdb))))
|
||||||
|
|
||||||
(defun load-trackdb (fn)
|
(defun load-trackdb (fn)
|
||||||
(let ((tdb (make-track-database))
|
(let* ((tmpdb (read-tracks-from-nodedb fn))
|
||||||
(tmpdb (read-tracks-from-nodedb fn)))
|
(tdb (make-hash-table :test #'equalp :size (hash-table-count tmpdb))))
|
||||||
(loop for pos being the hash-keys of tmpdb using (hash-value clist)
|
(loop for pos being the hash-keys of tmpdb using (hash-value clist)
|
||||||
for dirs = (conns-list-dirs clist)
|
for dirs = (conns-list-dirs clist)
|
||||||
for connects = (make-empty-connects) do
|
for connects = (make-empty-connects) do
|
||||||
|
|
Loading…
Reference in New Issue