Skip to content

Commit b8a9862

Browse files
committed
Use StringData for Nix language value documentation too
This probably isn't much of a perf gain, but it does mean we are completely rid of non-length prefixed strings. That allows us to delete some code.
1 parent 3a76ba2 commit b8a9862

File tree

11 files changed

+242
-273
lines changed

11 files changed

+242
-273
lines changed

src/libcmd/repl.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -656,7 +656,7 @@ ProcessLineResult NixRepl::processLine(std::string line)
656656
+ "\n\n";
657657
}
658658

659-
markdown += stripIndentation(doc->doc);
659+
markdown += stripIndentation(doc->doc.view());
660660

661661
logger->cout(trim(renderMarkdownToTerminal(markdown)));
662662
} else if (fallbackPos) {

src/libexpr-c/nix_api_value.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ PrimOp * nix_alloc_primop(
134134
.name = name,
135135
.args = {},
136136
.arity = (size_t) arity,
137-
.doc = doc,
137+
.doc = &nix::StringData::make(doc),
138138
.fun = std::bind(nix_c_primop_wrapper, fun, user_data, _1, _2, _3, _4)};
139139
if (args)
140140
for (size_t i = 0; args[i]; i++)

src/libexpr/eval.cc

Lines changed: 3 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -51,36 +51,6 @@ using json = nlohmann::json;
5151

5252
namespace nix {
5353

54-
/**
55-
* Just for doc strings. Not for regular string values.
56-
*/
57-
static char * allocString(size_t size)
58-
{
59-
char * t;
60-
t = (char *) GC_MALLOC_ATOMIC(size);
61-
if (!t)
62-
throw std::bad_alloc();
63-
return t;
64-
}
65-
66-
// When there's no need to write to the string, we can optimize away empty
67-
// string allocations.
68-
// This function handles makeImmutableString(std::string_view()) by returning
69-
// the empty string.
70-
/**
71-
* Just for doc strings. Not for regular string values.
72-
*/
73-
static const char * makeImmutableString(std::string_view s)
74-
{
75-
const size_t size = s.size();
76-
if (size == 0)
77-
return "";
78-
auto t = allocString(size + 1);
79-
memcpy(t, s.data(), size);
80-
t[size] = '\0';
81-
return t;
82-
}
83-
8454
StringData & StringData::alloc(size_t size)
8555
{
8656
void * t = GC_MALLOC_ATOMIC(sizeof(StringData) + size + 1);
@@ -571,7 +541,7 @@ std::optional<EvalState::Doc> EvalState::getDoc(Value & v)
571541
.name = v2->primOp()->name,
572542
.arity = v2->primOp()->arity,
573543
.args = v2->primOp()->args,
574-
.doc = doc,
544+
.doc = *doc,
575545
};
576546
}
577547
if (v.isLambda()) {
@@ -613,9 +583,8 @@ std::optional<EvalState::Doc> EvalState::getDoc(Value & v)
613583
.name = name,
614584
.arity = 0, // FIXME: figure out how deep by syntax only? It's not semantically useful though...
615585
.args = {},
616-
/* N.B. Can't use StringData here, because that would lead to an interior pointer.
617-
NOTE: memory leak when compiled without GC. */
618-
.doc = makeImmutableString(s.view()),
586+
/* NOTE: memory leak when compiled without GC. */
587+
.doc = StringData::make(s.view()),
619588
};
620589
}
621590
if (isFunctor(v)) {

src/libexpr/include/nix/expr/eval.hh

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ struct PrimOp
108108
/**
109109
* Optional free-form documentation about the primop.
110110
*/
111-
const char * doc = nullptr;
111+
const StringData * doc = nullptr;
112112

113113
/**
114114
* Add a trace item, while calling the `<name>` builtin.
@@ -156,7 +156,7 @@ struct Constant
156156
/**
157157
* Optional free-form documentation about the constant.
158158
*/
159-
const char * doc = nullptr;
159+
const StringData * doc = nullptr;
160160

161161
/**
162162
* Whether the constant is impure, and not available in pure mode.
@@ -836,11 +836,7 @@ public:
836836
std::optional<std::string> name;
837837
size_t arity;
838838
std::vector<std::string> args;
839-
/**
840-
* Unlike the other `doc` fields in this file, this one should never be
841-
* `null`.
842-
*/
843-
const char * doc;
839+
const StringData & doc;
844840
};
845841

846842
/**

0 commit comments

Comments
 (0)