From: José Fonseca Date: Thu, 25 Nov 2010 09:36:04 +0000 (+0000) Subject: Compress names better. X-Git-Url: https://git.cworth.org/git?a=commitdiff_plain;h=fa922145e14de53625b24349c4c36d4a8d326b86;p=apitrace Compress names better. --- diff --git a/log.cpp b/log.cpp index 11d0993..e373f69 100644 --- a/log.cpp +++ b/log.cpp @@ -30,6 +30,8 @@ #include #include +#include + #include #include "os.hpp" @@ -148,6 +150,22 @@ WriteString(const char *str) { Write(str, len); } +typedef std::map namemap; +static namemap names; + +static inline void +WriteName(const char *name) { + namemap::iterator it = names.find(name); + if (it == names.end()) { + size_t name_id = names.size(); + WriteUInt(name_id); + WriteString(name); + names[name] = name_id; + } else { + WriteUInt(it->second); + } +} + void Open(void) { if (!g_gzFile) { _Open("trace"); @@ -163,7 +181,7 @@ void BeginCall(const char *function) { OS::AcquireMutex(); Open(); ++reentrancy; - WriteString(function); + WriteName(function); } void EndCall(void) { @@ -176,7 +194,7 @@ void EndCall(void) { void BeginArg(unsigned index, const char *name) { WriteByte(Trace::CALL_ARG); WriteUInt(index); - WriteString(name); + WriteName(name); } void BeginReturn(void) { @@ -188,17 +206,13 @@ void BeginArray(size_t length) { WriteUInt(length); } -void BeginStruct(const char *name) { +void BeginStruct(size_t length) { WriteByte(Trace::TYPE_STRUCT); - (void)name; -} - -void EndStruct(void) { - WriteString(""); + WriteUInt(length); } void BeginMember(const char *name) { - WriteString(name); + WriteName(name); } void BeginBitmask(void) { @@ -280,7 +294,7 @@ void LiteralBlob(const void *data, size_t size) { void LiteralNamedConstant(const char *name, long long value) { WriteByte(Trace::TYPE_CONST); - WriteString(name); + WriteName(name); LiteralSInt(value); } diff --git a/log.hpp b/log.hpp index b6fc2b3..c012d78 100644 --- a/log.hpp +++ b/log.hpp @@ -46,8 +46,8 @@ namespace Log { inline void BeginElement(void) {} inline void EndElement(void) {} - void BeginStruct(const char *name); - void EndStruct(void); + void BeginStruct(size_t length); + inline void EndStruct(void) {} void BeginMember(const char *name); inline void EndMember(void) {} diff --git a/trace.py b/trace.py index b50d0a6..ab55555 100644 --- a/trace.py +++ b/trace.py @@ -51,7 +51,7 @@ class DumpDeclarator(base.OnceVisitor): for type, name in struct.members: self.visit(type) print 'static void __traceStruct%s(const %s &value) {' % (struct.id, struct.expr) - print ' Log::BeginStruct("%s");' % struct.name + print ' Log::BeginStruct(%u);' % len(struct.members) for type, name in struct.members: print ' Log::BeginMember("%s");' % (name,) dump_instance(type, 'value.%s' % (name,)) diff --git a/trace_parser.hpp b/trace_parser.hpp index 97dcf65..39fcfda 100644 --- a/trace_parser.hpp +++ b/trace_parser.hpp @@ -30,6 +30,8 @@ #include #include +#include +#include #include @@ -44,6 +46,10 @@ class Parser { protected: gzFile file; + + typedef std::map namemap; + namemap names; + public: Parser() { file = NULL; @@ -79,7 +85,7 @@ public: Call *parse_call(void) { Call *call = new Call; - call->name = read_string(); + call->name = read_name(); do { int c = read_byte(); switch(c) { @@ -104,7 +110,7 @@ public: void parse_arg(Call *call) { unsigned index = read_uint(); - std::string name = read_string(); + std::string name = read_name(); Value *value = parse_value(); if (index >= call->args.size()) { call->args.resize(index + 1); @@ -176,7 +182,7 @@ public: } Value *parse_const() { - std::string name = read_string(); + std::string name = read_name(); Value *value = parse_value(); return new Const(name, value); } @@ -194,7 +200,7 @@ public: value |= read_uint(); break; case Trace::TYPE_CONST: - read_string(); + read_name(); break; case Trace::TYPE_NULL: goto done; @@ -227,13 +233,12 @@ done: } Value *parse_struct() { - std::string name; + size_t length = read_uint(); /* XXX */ - name = read_string(); - while(name.length()) { + for (size_t i; i < length; ++i) { + std::string name = read_name(); Value *value = parse_value(); std::cout << " " << name << " = " << value << "\n"; - name = read_string(); } return NULL; } @@ -244,6 +249,18 @@ done: /* XXX */ return new UInt(addr); } + + std::string read_name(void) { + std::string name; + size_t id = read_uint(); + if (id >= names.size()) { + name = read_string(); + names[id] = name; + return name; + } else { + return names[id]; + } + } std::string read_string(void) { size_t len = read_uint();