From 358aee203eb7dfdcb633f2deea5323810ed32173 Mon Sep 17 00:00:00 2001 From: Markus Armbruster Date: Thu, 31 Dec 2009 10:30:54 +0100 Subject: [PATCH] Store sequence numbers more compactly Store them and ef_type in bit-fields. Allocate eight bits for ef_type. Values range from 0 to EF_GAME (currently 16), so this is plenty. Allocate twelve bits for sequence numbers. Sequence number mismatches are now missed when the numbers are equal modulo 2^12. Still sufficiently improbable. Common machines store the bit-fields in a 32 bit word. There are twelve bits left in that word for future use. Total savings 16 bits, which is exactly what the previous commit spent on wider uids on common machines. --- include/commodity.h | 4 ++-- include/empobj.h | 5 ++--- include/file.h | 4 ++-- include/game.h | 4 ++-- include/land.h | 4 ++-- include/loan.h | 4 ++-- include/lost.h | 4 ++-- include/nat.h | 8 ++++---- include/nuke.h | 4 ++-- include/plane.h | 4 ++-- include/sect.h | 4 ++-- include/ship.h | 4 ++-- include/trade.h | 4 ++-- include/treaty.h | 4 ++-- src/lib/subs/unitsub.c | 6 +++--- 15 files changed, 33 insertions(+), 34 deletions(-) diff --git a/include/commodity.h b/include/commodity.h index a36d9b45d..1f03fa78e 100644 --- a/include/commodity.h +++ b/include/commodity.h @@ -42,9 +42,9 @@ struct comstr { /* initial part must match struct empobj */ - short ef_type; + signed ef_type: 8; + unsigned com_seqno: 12; int com_uid; - unsigned com_seqno; time_t com_timestamp; natid com_owner; /* end of part matching struct empobj */ diff --git a/include/empobj.h b/include/empobj.h index 5ff65486f..9574073d0 100644 --- a/include/empobj.h +++ b/include/empobj.h @@ -55,9 +55,9 @@ struct empobj { * initial part must match struct emptypedstr * valid if EFF_TYPED is set in table's flags */ - short ef_type; + signed ef_type: 8; + unsigned seqno: 12; int uid; - unsigned seqno; time_t timestamp; /* end of part matching struct emptypedstr */ natid own; /* valid if EFF_OWNER is in table's flags */ @@ -76,7 +76,6 @@ struct empobj { }; union empobj_storage { - short ef_type; struct empobj gen; struct comstr comm; struct gamestr game; diff --git a/include/file.h b/include/file.h index e0a56cadb..4c0424b42 100644 --- a/include/file.h +++ b/include/file.h @@ -85,9 +85,9 @@ struct empfile { }; struct emptypedstr { - short ef_type; + signed ef_type: 8; + unsigned seqno: 12; int uid; - unsigned seqno; time_t timestamp; }; diff --git a/include/game.h b/include/game.h index a465d2ae1..a58d1bdb8 100644 --- a/include/game.h +++ b/include/game.h @@ -38,9 +38,9 @@ struct gamestr { /* initial part must match struct empobj */ - short ef_type; + signed ef_type: 8; + unsigned game_seqno: 12; int game_uid; - unsigned game_seqno; time_t game_timestamp; /* end of part matching struct empobj */ char game_upd_disable; /* updates disabled? */ diff --git a/include/land.h b/include/land.h index 91d9f01c3..5b1fb7079 100644 --- a/include/land.h +++ b/include/land.h @@ -49,9 +49,9 @@ struct lndstr { /* initial part must match struct empobj */ - short ef_type; + signed ef_type: 8; + unsigned lnd_seqno: 12; int lnd_uid; /* unit id (land #) */ - unsigned lnd_seqno; time_t lnd_timestamp; /* Last time this unit was touched */ natid lnd_own; /* owner's country num */ coord lnd_x; /* x location in abs coords */ diff --git a/include/loan.h b/include/loan.h index 1015acb4b..d4fae4c70 100644 --- a/include/loan.h +++ b/include/loan.h @@ -42,9 +42,9 @@ struct lonstr { /* initial part must match struct empobj */ - short ef_type; + signed ef_type: 8; + unsigned l_seqno: 12; int l_uid; - unsigned l_seqno; time_t l_timestamp; /* end of part matching struct empobj */ natid l_loner; /* loan shark */ diff --git a/include/lost.h b/include/lost.h index 63e68d8c5..0e40470c3 100644 --- a/include/lost.h +++ b/include/lost.h @@ -39,9 +39,9 @@ struct loststr { /* initial part must match struct empobj */ - short ef_type; + signed ef_type: 8; + unsigned lost_seqno: 12; int lost_uid; - unsigned lost_seqno; time_t lost_timestamp; /* When it was lost */ natid lost_owner; /* Who lost it */ /* end of part matching struct empobj */ diff --git a/include/nat.h b/include/nat.h index ce1390151..5b658bdbb 100644 --- a/include/nat.h +++ b/include/nat.h @@ -70,9 +70,9 @@ enum nat_status { */ struct realmstr { /* initial part must match struct empobj */ - short ef_type; + signed ef_type: 8; + unsigned r_seqno: 12; int r_uid; /* realm table index */ - unsigned r_seqno; time_t r_timestamp; /* Last time this realm was touched */ natid r_cnum; /* country number */ /* end of part matching struct empobj */ @@ -83,9 +83,9 @@ struct realmstr { struct natstr { /* initial part must match struct empobj */ - short ef_type; + signed ef_type: 8; + unsigned nat_seqno: 12; int nat_uid; /* equals nat_cnum */ - unsigned nat_seqno; time_t nat_timestamp; natid nat_cnum; /* our country number */ /* end of part matching struct empobj */ diff --git a/include/nuke.h b/include/nuke.h index d24a83edc..6a0db83ff 100644 --- a/include/nuke.h +++ b/include/nuke.h @@ -42,9 +42,9 @@ struct nukstr { /* initial part must match struct empobj */ - short ef_type; + signed ef_type: 8; + unsigned nuk_seqno: 12; int nuk_uid; /* unit id (nuke #) */ - unsigned nuk_seqno; time_t nuk_timestamp; /* Last time this nuke was touched */ natid nuk_own; coord nuk_x, nuk_y; /* current loc of device */ diff --git a/include/plane.h b/include/plane.h index fa3a3f5db..e77ee93ac 100644 --- a/include/plane.h +++ b/include/plane.h @@ -46,9 +46,9 @@ struct plnstr { /* initial part must match struct empobj */ - short ef_type; + signed ef_type: 8; + unsigned pln_seqno: 12; int pln_uid; /* unit id (plane #) */ - unsigned pln_seqno; time_t pln_timestamp; /* Last time this plane was touched */ natid pln_own; /* owning country */ coord pln_x; /* plane x-y */ diff --git a/include/sect.h b/include/sect.h index 01fda989d..9b6fe8d8e 100644 --- a/include/sect.h +++ b/include/sect.h @@ -44,9 +44,9 @@ struct sctstr { /* initial part must match struct empobj */ - short ef_type; + signed ef_type: 8; + unsigned sct_seqno: 12; int sct_uid; /* equals XYOFFSET(sct_x, sct_y) */ - unsigned sct_seqno; time_t sct_timestamp; /* Last time this sector was written to */ natid sct_own; /* owner's country num */ coord sct_x; /* x coord of sector */ diff --git a/include/ship.h b/include/ship.h index a0b325d38..23cefa1f7 100644 --- a/include/ship.h +++ b/include/ship.h @@ -63,9 +63,9 @@ struct shpstr { /* initial part must match struct empobj */ - short ef_type; + signed ef_type: 8; + unsigned shp_seqno: 12; int shp_uid; /* unit it (ship #) */ - unsigned shp_seqno; time_t shp_timestamp; /* Last time this ship was touched. */ natid shp_own; /* owner's country num */ coord shp_x; /* x location in abs coords */ diff --git a/include/trade.h b/include/trade.h index d40060c63..72ebdbbb9 100644 --- a/include/trade.h +++ b/include/trade.h @@ -42,9 +42,9 @@ struct trdstr { /* initial part must match struct empobj */ - short ef_type; + signed ef_type: 8; + unsigned trd_seqno: 12; int trd_uid; - unsigned trd_seqno; time_t trd_timestamp; natid trd_owner; /* end of part matching struct empobj */ diff --git a/include/treaty.h b/include/treaty.h index 0c6902f67..440d7f533 100644 --- a/include/treaty.h +++ b/include/treaty.h @@ -39,9 +39,9 @@ struct trtstr { /* initial part must match struct empobj */ - short ef_type; + signed ef_type: 8; + unsigned trt_seqno: 12; int trt_uid; - unsigned trt_seqno; time_t trt_timestamp; /* end of part matching struct empobj */ natid trt_cna; /* proposer */ diff --git a/src/lib/subs/unitsub.c b/src/lib/subs/unitsub.c index d91aae4b7..f907ff7b8 100644 --- a/src/lib/subs/unitsub.c +++ b/src/lib/subs/unitsub.c @@ -56,7 +56,7 @@ unit_list(struct emp_qelem *unit_list) return; qp = unit_list->q_back; ulp = (struct ulist *)qp; - type = ulp->unit.ef_type; + type = ulp->unit.gen.ef_type; if (CANT_HAPPEN(type != EF_LAND && type != EF_SHIP)) return; @@ -191,10 +191,10 @@ unit_view(struct emp_qelem *list) for (qp = list->q_back; qp != list; qp = next) { next = qp->q_back; ulp = (struct ulist *)qp; - if (CANT_HAPPEN(!(ef_flags(ulp->unit.ef_type) & EFF_XY))) + if (CANT_HAPPEN(!(ef_flags(ulp->unit.gen.ef_type) & EFF_XY))) continue; getsect(ulp->unit.gen.x, ulp->unit.gen.y, §); - if (ulp->unit.ef_type == EF_SHIP) { + if (ulp->unit.gen.ef_type == EF_SHIP) { if (((struct mchrstr *)ulp->chrp)->m_flags & M_FOOD) pr("[fert:%d] ", sect.sct_fertil); if (((struct mchrstr *)ulp->chrp)->m_flags & M_OIL) -- 2.43.0