Skip to content

Commit 42969ba

Browse files
authored
A bit of starlark micro-optimization (#4468)
**What type of PR is this?** Cleanup/perf improvement **What does this PR do? Why is it needed?** **Which issues(s) does this PR fix?** Fixes # **Other notes for review**
1 parent 7ef8fb1 commit 42969ba

File tree

4 files changed

+16
-27
lines changed

4 files changed

+16
-27
lines changed

go/private/common.bzl

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -155,16 +155,6 @@ def get_versioned_shared_lib_extension(path):
155155

156156
MINIMUM_BAZEL_VERSION = "6.5.0"
157157

158-
def as_list(v):
159-
"""Returns a list, tuple, or depset as a list."""
160-
if type(v) == "list":
161-
return v
162-
if type(v) == "tuple":
163-
return list(v)
164-
if type(v) == "depset":
165-
return v.to_list()
166-
fail("as_list failed on {}".format(v))
167-
168158
def as_iterable(v):
169159
"""Returns a list, tuple, or depset as something iterable."""
170160
if type(v) == "list":

go/private/context.bzl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,7 @@ def new_go_info(
316316
srcs = attr_srcs + generated_srcs
317317
embedsrcs = [f for t in getattr(attr, "embedsrcs", []) for f in as_iterable(t.files)]
318318
deps = [get_archive(dep) for dep in getattr(attr, "deps", [])]
319+
data = getattr(attr, "data", [])
319320

320321
go_info = {
321322
"name": go.label.name if not name else name,
@@ -333,7 +334,7 @@ def new_go_info(
333334
"x_defs": {},
334335
"deps": deps,
335336
"gc_goopts": _expand_opts(go, "gc_goopts", getattr(attr, "gc_goopts", [])),
336-
"runfiles": _collect_runfiles(go, getattr(attr, "data", []), deps),
337+
"runfiles": _collect_runfiles(go, data, deps),
337338
"cgo": getattr(attr, "cgo", False),
338339
"cdeps": getattr(attr, "cdeps", []),
339340
"cppopts": _expand_opts(go, "cppopts", getattr(attr, "cppopts", [])),
@@ -351,11 +352,13 @@ def new_go_info(
351352
x_defs = go_info["x_defs"]
352353

353354
for k, v in getattr(attr, "x_defs", {}).items():
354-
v = _expand_location(go, attr, v)
355+
if "$" in v:
356+
v = go._ctx.expand_location(v, data)
355357
if "." not in k:
356-
k = "{}.{}".format(importmap, k)
358+
k = "%s.%s" % (importmap, k)
357359
x_defs[k] = v
358360
go_info["x_defs"] = x_defs
361+
359362
if not go_info["cgo"]:
360363
for k in ("cdeps", "cppopts", "copts", "cxxopts", "clinkopts"):
361364
if getattr(attr, k, None):
@@ -1070,6 +1073,3 @@ go_config = rule(
10701073

10711074
def _expand_opts(go, attribute_name, opts):
10721075
return [go._ctx.expand_make_variables(attribute_name, opt, {}) for opt in opts]
1073-
1074-
def _expand_location(go, attr, s):
1075-
return go._ctx.expand_location(s, getattr(attr, "data", []))

go/private/rules/binary.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ def _go_binary_impl(ctx):
171171
if go.mode.linkmode in LINKMODES_EXECUTABLE:
172172
env = {}
173173
for k, v in ctx.attr.env.items():
174-
env[k] = ctx.expand_location(v, ctx.attr.data)
174+
env[k] = ctx.expand_location(v, ctx.attr.data) if "$" in v else v
175175
providers.append(RunEnvironmentInfo(environment = env))
176176

177177
# The executable is automatically added to the runfiles.

go/private/rules/test.bzl

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ load(
2121
"GO_TOOLCHAIN",
2222
"GO_TOOLCHAIN_LABEL",
2323
"SUPPORTS_PATH_MAPPING_REQUIREMENT",
24-
"as_list",
2524
"asm_exts",
2625
"cgo_exts",
2726
"go_exts",
@@ -202,7 +201,7 @@ def _go_test_impl(ctx):
202201
"GO_TEST_RUN_FROM_BAZEL": "1",
203202
}
204203
for k, v in ctx.attr.env.items():
205-
env[k] = ctx.expand_location(v, ctx.attr.data)
204+
env[k] = ctx.expand_location(v, ctx.attr.data) if "$" in v else v
206205

207206
run_environment_info = RunEnvironmentInfo(env, ctx.attr.env_inherit)
208207

@@ -708,19 +707,19 @@ def _recompile_external_deps(go, external_go_info, internal_archive, library_lab
708707
testfilter = None,
709708
is_main = False,
710709
mode = go.mode,
711-
srcs = as_list(arc_data.srcs),
710+
srcs = list(arc_data.srcs),
712711
cover = arc_data._cover,
713-
embedsrcs = as_list(arc_data._embedsrcs),
712+
embedsrcs = list(arc_data._embedsrcs),
714713
x_defs = dict(arc_data._x_defs),
715714
deps = deps,
716-
gc_goopts = as_list(arc_data._gc_goopts),
715+
gc_goopts = list(arc_data._gc_goopts),
717716
runfiles = arc_data.runfiles,
718717
cgo = arc_data._cgo,
719-
cdeps = as_list(arc_data._cdeps),
720-
cppopts = as_list(arc_data._cppopts),
721-
copts = as_list(arc_data._copts),
722-
cxxopts = as_list(arc_data._cxxopts),
723-
clinkopts = as_list(arc_data._clinkopts),
718+
cdeps = list(arc_data._cdeps),
719+
cppopts = list(arc_data._cppopts),
720+
copts = list(arc_data._copts),
721+
cxxopts = list(arc_data._cxxopts),
722+
clinkopts = list(arc_data._clinkopts),
724723
)
725724

726725
# If this archive needs to be recompiled, use go.archive.

0 commit comments

Comments
 (0)