Skip to content

Commit

Permalink
perf: remove use of paths.replace_extension (#234)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbedard authored Feb 8, 2024
1 parent bfe6967 commit 971d049
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions swc/private/swc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ If non-empty, there should be one for each entry in srcs."""),
_SUPPORTED_EXTENSIONS = [".ts", ".mts", ".cts", ".tsx", ".jsx", ".mjs", ".cjs", ".js"]

def _is_supported_src(src):
return paths.split_extension(src)[-1] in _SUPPORTED_EXTENSIONS
i = src.rfind(".")
return i > 0 and src[i:] in _SUPPORTED_EXTENSIONS

# TODO: aspect_bazel_lib should provide this?
def _relative_to_package(path, ctx):
Expand Down Expand Up @@ -106,6 +107,14 @@ def _replace_ext(f, ext_map):
return new_ext
return None

def _replace_extension(f, ext):
i = f.rfind(".")

This comment has been minimized.

Copy link
@DavidZbarsky-at

DavidZbarsky-at Feb 13, 2024

this can be _remove_extension(f) + ext

This comment has been minimized.

Copy link
@jbedard

jbedard Feb 14, 2024

Author Member

Yeah I thought of that, but was partially trying to make these methods as dumb as possible. Part of the reason the perf issues happened is because we were using pretty util methods everywhere, not realizing that those utils were not meant to be run hundreds of times (in my use case it was 5000^2 times).

Maybe with these 2 methods side-by-side it's fine though 🤷

return f if i <= 0 else f[:-(len(f) - i)] + ext

def _remove_extension(f):
i = f.rfind(".")
return f if i <= 0 else f[:-(len(f) - i)]

This comment has been minimized.

Copy link
@DavidZbarsky-at

DavidZbarsky-at Feb 13, 2024

this can be return f if i <= 0 else f[:i]


def _calculate_js_out(src, out_dir = None, root_dir = None, js_outs = []):
if not _is_supported_src(src):
return None
Expand All @@ -117,16 +126,16 @@ def _calculate_js_out(src, out_dir = None, root_dir = None, js_outs = []):
".cjs": ".cjs",
".cts": ".cjs",
}
js_out = paths.replace_extension(src, _replace_ext(src, exts))
js_out = _replace_extension(src, _replace_ext(src, exts))
if root_dir:
js_out = _strip_root_dir(js_out, root_dir)
if out_dir:
js_out = paths.join(out_dir, js_out)

# Check if a custom out was requested with a potentially different extension
no_ext = _remove_extension(js_out)
for maybe_out in js_outs:
no_ext = paths.replace_extension(js_out, "")
if no_ext == paths.replace_extension(maybe_out, ""):
if no_ext == _remove_extension(maybe_out):
js_out = maybe_out
break
return js_out
Expand Down Expand Up @@ -159,7 +168,7 @@ def _calculate_map_out(src, source_maps, out_dir = None, root_dir = None):
".mjs": ".mjs.map",
".cjs": ".cjs.map",
}
map_out = paths.replace_extension(src, _replace_ext(src, exts))
map_out = _replace_extension(src, _replace_ext(src, exts))
if root_dir:
map_out = _strip_root_dir(map_out, root_dir)
if out_dir:
Expand Down

0 comments on commit 971d049

Please sign in to comment.